-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
41 lines (37 loc) · 1.38 KB
/
Copy pathauth.js
File metadata and controls
41 lines (37 loc) · 1.38 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
'use strict';
var model = require('./model'),
everyauth = require('everyauth');
function auth() {
//Everyauth
everyauth.google.appId(process.env.GOOGLE_APPID)
.appSecret(process.env.GOOGLE_SECRET)
.callbackPath('/auth/google/callback')
.scope('https://www.googleapis.com/auth/userinfo.profile')
.findOrCreateUser(function(session, accessToken, extra, googleUser) {
googleUser.refreshToken = extra.refresh_token;
googleUser.expiresIn = extra.expires_in;
var promise = new this.Promise();
model.User.findBySourceId('google', googleUser.id, function(err, doc) {
if (err) promise.fail(err);
else if (doc) {
session.userId = doc._id;
session.save();
promise.fulfill(doc);
} else {
model.User.addGoogleUser(googleUser, function(err, doc) {
if (err) promise.fail(err);
else {
session.userId = doc._id;
session.save();
promise.fulfill(doc);
}
});
}
});
return promise;
}).redirectPath('/');
return {
middleware: everyauth.middleware
};
}
module.exports = auth;