This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathgithub.js
47 lines (42 loc) · 1.68 KB
/
github.js
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
'use strict';
/**
* Module dependencies
*/
var passport = require('passport'),
GithubStrategy = require('passport-github').Strategy,
users = require('../../controllers/users.server.controller');
module.exports = function (config) {
// Use github strategy
passport.use(new GithubStrategy({
clientID: config.github.clientID,
clientSecret: config.github.clientSecret,
callbackURL: config.github.callbackURL,
passReqToCallback: true
},
function (req, accessToken, refreshToken, profile, done) {
// Set the provider data and include tokens
var providerData = profile._json;
providerData.accessToken = accessToken;
providerData.refreshToken = refreshToken;
// Create the user OAuth profile
var displayName = profile.displayName ? profile.displayName.trim() : profile.username.trim();
var iSpace = displayName.indexOf(' '); // index of the whitespace following the firstName
var firstName = iSpace !== -1 ? displayName.substring(0, iSpace) : displayName;
var lastName = iSpace !== -1 ? displayName.substring(iSpace + 1) : '';
var providerUserProfile = {
firstName: firstName,
lastName: lastName,
displayName: displayName,
email: (profile.emails && profile.emails.length) ? profile.emails[0].value : undefined,
username: profile.username,
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
profileImageURL: (providerData.avatar_url) ? providerData.avatar_url : undefined,
// jscs:enable
provider: 'github',
providerIdentifierField: 'id',
providerData: providerData
};
// Save the user OAuth profile
users.saveOAuthUserProfile(req, providerUserProfile, done);
}));
};