Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39550
1
2
###############################################################################
3
#
4
# CoCalc: Collaborative Calculation in the Cloud
5
#
6
# Copyright (C) 2016, Sagemath Inc.
7
#
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#
21
###############################################################################
22
23
24
############################################################
25
# Account Settings
26
############################################################
27
28
{webapp_client} = require('./webapp_client')
29
{alert_message} = require('./alerts')
30
account_page = require('./account_page')
31
32
misc = require("misc")
33
{redux} = require('./smc-react')
34
35
################################################
36
# Account creation
37
################################################
38
39
first_login = true
40
41
# load more of the app now that user is logged in.
42
load_app = (cb) ->
43
require.ensure [], ->
44
require('./r_account.cjsx') # initialize react-related account page
45
require('./projects.cjsx') # initialize project listing
46
require('./file_use.cjsx') # initialize file_use notifications
47
cb()
48
49
signed_in = (mesg) ->
50
{analytics_event} = require('./misc_page')
51
analytics_event('account', 'signed_in') # user signed in
52
# the has_remember_me cookie is for usability: After a sign in we "mark" this client as being "known"
53
# next time the main landing page is visited, haproxy or hub will redirect to the client
54
# note: similar code is in redux_account.coffee → AccountActions::sign_out
55
{APP_BASE_URL} = require('./misc_page')
56
exp = misc.server_days_ago(-30).toGMTString()
57
document.cookie = "#{APP_BASE_URL}has_remember_me=true; expires=#{exp} ;path=/"
58
# Record which hub we're connected to.
59
redux.getActions('account').setState(hub: mesg.hub)
60
load_file = window.smc_target and window.smc_target != 'login'
61
if first_login
62
first_login = false
63
if not load_file
64
load_app ->
65
require('./history').load_target('projects')
66
67
if load_file
68
# wait until account settings get loaded, then show target page
69
# HACK: This is hackish!, and will all go away with a more global use of React (and routing).
70
# The underscore below should make it clear that this is hackish.
71
redux.getTable('account')._table.once 'connected', ->
72
load_app ->
73
require('./history').load_target(window.smc_target)
74
window.smc_target = ''
75
76
77
# Listen for pushed sign_in events from the server. This is one way that
78
# the sign_in function above can be activated, but not the only way.
79
webapp_client.on("signed_in", signed_in)
80
81
################################################
82
# Automatically log in
83
################################################
84
remember_me = webapp_client.remember_me_key()
85
if misc.get_local_storage(remember_me)
86
redux.getActions('account').setState(remember_me: true)
87
# just in case, always show manual login screen after 45s.
88
setTimeout (->
89
redux.getActions('account').setState(remember_me: false)
90
), 45000
91
webapp_client.on "remember_me_failed", () ->
92
redux.getActions('account').setState(remember_me: false)
93
if redux.getStore('account')?.is_logged_in() # if we thought user was logged in, but the cookie was invalid, force them to sign in again
94
f = ->
95
if not misc.get_local_storage(remember_me)
96
alert_message(type:'info', message:'You might have to sign in again.', timeout:1000000)
97
setTimeout(f, 15000) # give it time to possibly resolve itself. SMELL: confused about what is going on here...
98
99
# check if user has a has_remember_me cookie (regardless if it is valid or not)
100
# the "remember_me" is set to be http-only and hence not accessible from javascript (security)
101
{get_cookie, APP_BASE_URL} = require('./misc_page')
102
# for the initial month after the rebranding, we always set this to true to emphasize the sign in bar at the top
103
# TODO the following is disabled -- https://github.com/sagemathinc/cocalc/issues/2051
104
if false # misc.server_weeks_ago(4) > new Date("2017-05-20")
105
redux.getActions('account').setState(has_remember_me : get_cookie("#{APP_BASE_URL}has_remember_me") == 'true')
106
else
107
redux.getActions('account').setState(has_remember_me : true)
108
109
# Return a default filename with the given ext (or not extension if ext not given)
110
# FUTURE: make this configurable with different schemas.
111
exports.default_filename = (ext, is_folder) ->
112
return default_filename_iso(ext)
113
#return default_filename_mac(ext)
114
115
default_filename_iso = (ext, is_folder) ->
116
base = misc.to_iso(new Date()).replace('T','-').replace(/:/g,'')
117
if ext
118
base += '.' + ext
119
return base
120
121
# This isn't used yet -- will not a config option in account settings.
122
default_filename_mac = (ext, is_folder) ->
123
switch ext
124
when 'zip'
125
return 'Archive.zip'
126
else
127
return 'untitled ' + (if is_folder then 'folder' else 'file')
128
129