1
+ //common
2
+ var domain = ''; // domain name
3
+ var urlpath = ''; // sub url path, like: www.example.com/<urlpath>
4
+ //settings
5
+ var debug = false;
6
+
7
+ var GOOGLE_API_KEY = '';
8
+ var GOOGLE_CLIENT_ID = '';
9
+
10
+ var DROPBOX_APP_KEY = '';
11
+
12
+ var port = window.location.port;
13
+ var serverurl = window.location.protocol + '//' + (domain ? domain : window.location.hostname) + (port ? ':' + port : '') + (urlpath ? '/' + urlpath : '');
14
+ var noteid = urlpath ? window.location.pathname.slice(urlpath.length + 1, window.location.pathname.length).split('/')[1] : window.location.pathname.split('/')[1];
15
+ var noteurl = serverurl + '/' + noteid;
16
+
17
+ var version = '0.4.4';
18
+
19
+ var checkAuth = false;
20
+ var profile = null;
21
+ var lastLoginState = getLoginState();
22
+ var lastUserId = getUserId();
23
+ var loginStateChangeEvent = null;
24
+
25
+ function resetCheckAuth() {
26
+ checkAuth = false;
27
+ }
28
+
29
+ function setLoginState(bool, id) {
30
+ Cookies.set('loginstate', bool, {
31
+ expires: 365
32
+ });
33
+ if (id) {
34
+ Cookies.set('userid', id, {
35
+ expires: 365
36
+ });
37
+ } else {
38
+ Cookies.remove('userid');
39
+ }
40
+ lastLoginState = bool;
41
+ lastUserId = id;
42
+ checkLoginStateChanged();
43
+ }
44
+
45
+ function checkLoginStateChanged() {
46
+ if (getLoginState() != lastLoginState || getUserId() != lastUserId) {
47
+ if(loginStateChangeEvent)
48
+ loginStateChangeEvent();
49
+ return true;
50
+ } else {
51
+ return false;
52
+ }
53
+ }
54
+
55
+ function getLoginState() {
56
+ var state = Cookies.get('loginstate');
57
+ return state === "true" || state === true;
58
+ }
59
+
60
+ function getUserId() {
61
+ return Cookies.get('userid');
62
+ }
63
+
64
+ function clearLoginState() {
65
+ Cookies.remove('loginstate');
66
+ }
67
+
68
+ function checkIfAuth(yesCallback, noCallback) {
69
+ var cookieLoginState = getLoginState();
70
+ if (checkLoginStateChanged())
71
+ checkAuth = false;
72
+ if (!checkAuth || typeof cookieLoginState == 'undefined') {
73
+ $.get(serverurl + '/me')
74
+ .done(function (data) {
75
+ if (data && data.status == 'ok') {
76
+ profile = data;
77
+ yesCallback(profile);
78
+ setLoginState(true, data.id);
79
+ } else {
80
+ noCallback();
81
+ setLoginState(false);
82
+ }
83
+ })
84
+ .fail(function () {
85
+ noCallback();
86
+ })
87
+ .always(function () {
88
+ checkAuth = true;
89
+ });
90
+ } else if (cookieLoginState) {
91
+ yesCallback(profile);
92
+ } else {
93
+ noCallback();
94
+ }
95
+ }
0 commit comments