-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
165 lines (146 loc) · 5.04 KB
/
Copy pathplugin.js
File metadata and controls
165 lines (146 loc) · 5.04 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
/**
* Plugin.js file, set configs, routes, hooks and events here
*
* see http://wejs.org/docs/we/extend.plugin
*/
module.exports = function loadPlugin(projectPath, Plugin) {
const plugin = new Plugin(__dirname);
// set plugin configs
plugin.setConfigs({
passport: {
strategies: {
google: {
Strategy: require('passport-google-oauth').OAuth2Strategy,
// url to image icon
icon: '/public/plugin/we-plugin-passport-google/files/images/g-logo.png',
authUrl: '/auth/google',
// get a clientID and clientSecret in google api console,
// for docs see https://github.com/jaredhanson/passport-google-oauth
clientID: null,
clientSecret: null,
scope: null,
// redirect configurations:
redirectUrlAfterSuccess: '/',
redirectUrlAfterFailure: '/login',
// callbackURL is automaticaly set to we.config.hostname+'/auth/google/callback'
// but you can change it in config/local.js
callbackURL: null,
session: true,
findUser(token, tokenSecret, profile, done) {
const we = this.we;
we.log.verbose('profile from google:', {
profile,
});
// get email
let email;
for (let i = 0; i < profile.emails.length; i++) {
if (
profile.emails[i].type == 'account' ||
profile.emails[i].type == 'ACCOUNT'
) {
email = profile.emails[i].value;
break;
}
}
if (!email && profile.emails[0] && profile.emails[0].verified == true) {
email = profile.emails[0].value;
}
if (!email) {
return done('we-plugin-passport-goole:auth:email not found');
}
let query = {
where: { email: email },
defaults: {
displayName: profile.displayName,
fullName: profile.displayName,
acceptTerms: true,
active: true,
email: email,
googleId: profile.id,
confirmEmail: email
}
};
// save gender
if (profile.gender == 'male') {
query.defaults.gender = 'M';
} else if(profile.gender == 'female') {
query.defaults.gender = 'F';
}
we.db.models.user
.findOrCreate(query)
.spread( (user, created)=> {
if (created) {
we.log.info('New user from google oauth2', user.id);
} else if(user.blocked) {
we.log.info('G:User blocked trying to login:', user.id);
return done('user.blocked.cant.login', null);
}
// activate user with google auth...
if(!user.active) user.active = true;
// set google Id if not set:
if (!user.googleId) user.googleId = profile.id;
if (!user.displayName) user.displayName = profile.displayName;
if (!user.fullName) user.fullName = profile.displayName;
return user.save()
.then( ()=> {
plugin.saveUserAvatar(profile, user, we, (err, image)=> {
if (image) user.avatar = [image];
done(null, user);
});
});
})
.catch(done);
}
}
}
}
});
// ser plugin routes
plugin.setRoutes({
// Return a list of messages between authenticated user and :uid user
'get /auth/google': {
controller : 'passportGoogle',
action : 'page',
responseType : 'json'
},
'get /auth/google/callback': {
controller : 'passportGoogle',
action : 'callback',
responseType : 'json'
}
});
// use the bootstrap event to set default auth callback
plugin.events.on('we:after:load:express', function(we) {
if (
we.config.passport &&
we.config.passport.strategies &&
we.config.passport.strategies.google &&
!we.config.passport.strategies.google.callbackURL
) {
we.config.passport.strategies.google.callbackURL = we.config.hostname+'/auth/google/callback';
}
});
plugin.saveUserAvatar = function(profile, user, we, cb) {
const plf = we.plugins['we-plugin-file-local'];
if (!plf) return cb();
if (user && user.avatar && user.avatar.length) return cb();
if (!profile.photos && !profile.photos[0]) return cb();
const uU = plf.urlUploader;
const url = profile.photos[0].value
.replace('sz=50', 'sz=250')
.replace('s50', 's250');
uU.uploadFromUrl(url, we, (err, image)=> {
if (err) return cb(err);
image.setCreator(user.id)
.then( ()=> {
user.avatar = [image];
return user.save();
})
.then( ()=> {
we.log.verbose('new image:', image.get());
cb(null, image);
});
});
};
return plugin;
};