Skip to content

Commit 9bc4bfa

Browse files
author
Ilya Radchenko
committed
Throw error if github email missing
1 parent 57f81f5 commit 9bc4bfa

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

lib/webapp.js

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,24 @@ module.exports = {
5858
},
5959

6060
auth: function (passport, context) {
61-
var config = this.appConfig
61+
var config = this.appConfig;
62+
6263
if (!config.appId || !config.appSecret || !config.hostname) {
63-
throw new Error('Github plugin misconfigured! Need appId and appSecret and hostname')
64+
throw new Error('Github plugin misconfigured! Need `appId`, `appSecret` and `hostname`.');
6465
}
65-
var callbackURL = config.hostname + "/auth/github/callback";
66+
67+
var callbackURL = config.hostname + '/auth/github/callback';
68+
6669
passport.use(new GithubStrategy({
67-
clientID : config.appId
68-
, clientSecret: config.appSecret
69-
, callbackURL : callbackURL
70-
, authorizationURL: config.apiDomain + '/login/oauth/authorize'
71-
, tokenURL: config.apiDomain + '/login/oauth/access_token'
72-
, userProfileURL: config.apiEndpoint + '/user'
73-
, scope: ['repo']
74-
, passReqToCallback: true
75-
}, validateAuth));
70+
clientID : config.appId
71+
clientSecret: config.appSecret
72+
callbackURL : callbackURL
73+
authorizationURL: config.apiDomain + '/login/oauth/authorize'
74+
tokenURL: config.apiDomain + '/login/oauth/access_token'
75+
userProfileURL: config.apiEndpoint + '/user'
76+
scope: ['repo']
77+
passReqToCallback: true
78+
}, validateAuth));
7679
},
7780

7881
setupRepo: function (account, config, project, done) {
@@ -139,23 +142,30 @@ module.exports = {
139142

140143
function validateAuth(req, accessToken, refreshToken, profile, done){
141144
if (!req.user){
142-
console.warn('Github OAuth but no logged-in user')
143-
req.flash('account', "Cannot link a github account if you aren't logged in")
144-
return done()
145+
console.warn('Github OAuth but no logged-in user');
146+
req.flash('account', 'Cannot link a github account if you aren\'t logged in');
147+
return done();
145148
}
146-
var account = req.user.account('github', profile.id)
149+
150+
var account = req.user.account('github', profile.id);
151+
147152
if (account) {
148-
console.warn("Trying to attach a github account that's already attached...")
149-
req.flash('account', 'That github account is already linked. <a target="_blank" href="https://github.com/logout">Sign out of github</a> before you click "Add Account"')
150-
return done(null, req.user)
153+
console.warn('Trying to attach a github account that\'s already attached...');
154+
req.flash('account', 'That github account is already linked. <a target="_blank" href="https://github.com/logout">Sign out of github</a> before you click "Add Account"');
155+
return done(null, req.user);
151156
}
152-
req.user.accounts.push(makeAccount(accessToken, profile))
157+
158+
req.user.accounts.push(makeAccount(accessToken, profile));
153159
req.user.save(function (err) {
154160
done(err, req.user);
155-
})
161+
});
156162
}
157163

158164
function makeAccount(accessToken, profile) {
165+
if (!profile.emails || !profile.emails.length) {
166+
throw new Error('You need to enter your email address into Github.');
167+
}
168+
159169
return {
160170
provider: 'github',
161171
id: profile.id,

0 commit comments

Comments
 (0)