Auth.js
9.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
define(function(require) {
'use strict';
var module = require('module');
var $ = require('jquery');
var _ = require('underscore');
var Backbone = require('backbone');
function bindMethods() {
var args = _.toArray(arguments);
var self = args.shift();
_.each(args, function(methodName) {
self[methodName] = _.bind(self[methodName], self);
});
}
function attachStart(self, callback) {
var start = $.Deferred();
self.start = function() {
start.resolve();
return this;
};
return start.done(_.bind(callback, self));
}
function attachReady(self) {
var deferred = $.Deferred().done(function() {
self.set('isReady', true);
});
var promise = deferred.promise();
self.ready = function() {
return promise;
};
return deferred;
}
var FacebookProvider = Backbone.Model.extend({
initialize: function(data, globalOptions) {
// early facebook javascript load
require(['facebook']);
FacebookProvider.__super__.initialize.apply(this, arguments);
var self = this;
var fbLoaded = $.Deferred();
var ready = attachReady(this);
attachStart(this, function() {
require(['facebook'], function(FB) {
FB.init({
appId: globalOptions.appId,
status: true,
});
FB.getLoginStatus(function(response) {
self.set({
'status': response.status,
'authResponse': response.authResponse,
});
fbLoaded.resolve(FB);
});
}, function() {
/* global console:false */
console.log('error loading facebook javascript');
fbLoaded.reject();
});
});
fbLoaded.always(function() {
ready.resolve();
});
// log helper
this.on('change:status', function() {
/* global console:false */
switch (this.get('status')) {
case 'connected':
console.log('FB connected', this.get('authResponse'));
break;
case 'not_authorized':
console.log('FB not authorized');
break;
default:
console.log('FB not logged in');
break;
}
}, this);
this.login = function() {
fbLoaded.done(function(FB) {
FB.login(function(response) {
self.set({
'status': response.status,
'authResponse': response.authResponse,
});
self.trigger('provider:logged-in');
});
});
return self;
};
this.logout = function() {
fbLoaded.done(function(FB) {
if (self.get('authResponse')) {
FB.logout(function(response) {
self.set({
'status': response.status,
'authResponse': null,
});
self.trigger('provider:logged-out');
});
} else {
self.trigger('provider:logged-out');
}
});
return self;
};
this.attachProviderData = function(data) {
var authResponse = this.get('authResponse') || {};
_.extend(data, {
facebookAccessToken: authResponse.accessToken,
facebookUserId: authResponse.userID,
});
};
bindMethods(this, 'attachProviderData');
},
});
var FormProvider = Backbone.Model.extend({
initialize: function(data, globalOptions) {
FormProvider.__super__.initialize.apply(this, arguments);
var ready = attachReady(this);
attachStart(this, function() {
ready.resolve();
});
this.login = function() {
// TODO: possibly fetch USERNAME/PASSWORD from browser.localStorage
this.trigger('provider:logged-in');
return this;
};
this.logout = function() {
this.trigger('provider:logged-out');
return this;
};
this.attachProviderData = function(data) {
_.extend(data, {
USERNAME: this.get('USERNAME'),
PASSWORD: this.get('PASSWORD'),
});
};
bindMethods(this, 'login', 'logout', 'attachProviderData');
},
});
var Auth = Backbone.Model.extend({
defaults: function() {
return {
isLoggedIn: false,
permissions: new Backbone.Model(),
user: new Backbone.Model(),
providers: new Backbone.Model(),
};
},
initialize: function(data, globalOptions) {
Auth.__super__.initialize.apply(this, arguments);
(function(self, cookieName) {
if (!cookieName) {
return;
}
var sessionId = self.get('sessionId');
if (sessionId) {
$.cookie('cookieName', sessionId);
} else {
self.set('sessionId', $.cookie(cookieName));
}
self.on('change:sessionId', function() {
var sessionId = this.get('sessionId');
if (sessionId) {
$.cookie(cookieName, sessionId);
} else {
$.removeCookie(cookieName);
}
}, self);
})(this, globalOptions.cookieName);
var ready = attachReady(this);
attachStart(this, function() {
_.invoke(this.get('providers').values(), 'start');
});
var self = this;
function applyLoginResults(resultData) {
var userData = resultData.user;
var user = self.get('user');
self.set('isLoggedIn', !!userData);
if (userData) {
user.set(userData);
} else {
user.clear();
}
self.set('sessionId', resultData.sessionId);
var permissions = self.get('permissions');
var toErase = {}, toSet = {};
_.each(permissions.keys(), function(key) {
toErase[key] = false;
});
_.each(resultData.permissions, function(key) {
delete toErase[key];
toSet[key] = true;
});
permissions.set(toErase, {unset: true}).set(toSet);
}
function api(apiName, data) {
var apiHandler = globalOptions.api || function(apiName, data) {};
return apiHandler(apiName, data, {sessionId: self.get('sessionId')}).done(applyLoginResults);
}
var addProviderData = _.bind(function addProviderData() {
var args = _.toArray(arguments);
var providerToApply, data;
if (typeof args[0] === 'object') {
data = args.shift();
}
if (typeof args[0] === 'string') {
providerToApply = args[0];
}
data = _.extend({}, data);
var providers = this.get('providers');
_.each(providerToApply ? [providerToApply] : providers.keys(), function(providerKey) {
providers.get(providerKey).attachProviderData(data);
});
return data;
}, this);
var providerReadies = _.map(this.get('providers').values(), function(provider) {
return provider.ready();
});
$.when.apply($, providerReadies).done(_.bind(function() {
api('auth:startVisit', addProviderData({
userAgent: navigator.userAgent,
})).always(function() {
ready.resolve();
});
var providers = this.get('providers');
_.each(providers.keys(), function(providerKey) {
providers.get(providerKey).on('provider:logged-in', function() {
api('auth:login:' + providerKey, addProviderData(providerKey));
});
});
}, this));
this.login = function(how) {
var provider = how ? this.get('providers').get(how) : null;
if (provider) {
provider.login();
}
};
this.logout = function() {
_.invoke(this.get('providers').values(), 'logout');
api('auth:logout');
};
bindMethods(this, 'login', 'logout');
},
}, {
FacebookProvider: FacebookProvider,
FormProvider: FormProvider,
});
return Auth;
});