Skip to content

Commit

Permalink
Ft 164489784 Implemented login with github
Browse files Browse the repository at this point in the history
  • Loading branch information
jnkindi committed Apr 9, 2019
1 parent e2ecaee commit d5e3853
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 22 deletions.
4 changes: 4 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ TWITTER_CONSUMER_KEY=
TWITTER_CONSUMER_SECRET=
TWITTER_CALLBACK=

GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
GITHUB_CALLBACK=

SECRETKEY =
2 changes: 2 additions & 0 deletions middlewares/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import serializePassportUser from './serialize';
import facebookStrategy from './passportStrategy/facebook';
import GoogleStrategy from './passportStrategy/google';
import TwitterStrategy from './passportStrategy/twitter';
import GithubStrategy from './passportStrategy/github';
import models from '../models/index';

const { user } = models;
Expand All @@ -22,6 +23,7 @@ class Strategy {
this.facebookStrategy = passport.use(facebookStrategy);
this.GoogleStrategy = passport.use(GoogleStrategy);
this.TwitterStrategy = passport.use(TwitterStrategy);
this.GithubStrategy = passport.use(GithubStrategy);
this.serializePassportUser = serializePassportUser(passport, user);
}
}
Expand Down
14 changes: 5 additions & 9 deletions middlewares/callbackHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,22 @@ const GetSocial = async (accessToken, refreshToken, profile, done) => {
* @param {*} done
* @returns { Object } user
*/
const GetSocialTwitter = async (token, tokenSecret, profile, done) => {
/**
* get unique formatted username
*/
const GetSocialTwitterGithub = async (token, tokenSecret, profile, done) => {
const { _json } = profile;
// _json.profile_image_url.
const image = usernamestring.generateLargeTwitterProfile(_json.profile_image_url);
const image = _json.avatar_url || usernamestring.largeTwitterImage(_json.profile_image_url);
const names = usernamestring.removeSpecialCharacters(_json.name);

const SocialUser = {
username: usernamestring.getUsername(profile.username),
firstname: names,
image,
bio: _json.description,
provider: _json.provider,
bio: _json.description || _json.bio,
provider: _json.provider || profile.provider,
provideruserid: _json.id.toString()
};
done(null, SocialUser);
};
export {
GetSocial,
GetSocialTwitter
GetSocialTwitterGithub
};
19 changes: 19 additions & 0 deletions middlewares/passportStrategy/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import GithubStrategy from 'passport-github';
import { GetSocialTwitterGithub } from '../callbackHandler';
/**
* @author Jacques Nyilinkindi
* @returns Github strategy
*/
const Github = new GithubStrategy(
{
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: process.env.GITHUB_CALLBACK
},
async (accessToken, refreshToken, profile, done) => {
GetSocialTwitterGithub(
accessToken, refreshToken, profile, done
);
}
);
export default Github;
24 changes: 13 additions & 11 deletions middlewares/passportStrategy/twitter.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import TwitterStrategy from 'passport-twitter';
import { GetSocialTwitter } from '../callbackHandler';
import { GetSocialTwitterGithub } from '../callbackHandler';
/**
* @author Jacques Nyilinkindi
* @returns Twitter strategy
*/
const Twitter = new TwitterStrategy({
consumerKey: process.env.TWITTER_CONSUMER_KEY,
consumerSecret: process.env.TWITTER_CONSUMER_SECRET,
callbackURL: process.env.TWITTER_CALLBACK,
},
async (token, tokenSecret, profile, done) => {
GetSocialTwitter(
token, tokenSecret, profile, done
);
});
const Twitter = new TwitterStrategy(
{
consumerKey: process.env.TWITTER_CONSUMER_KEY,
consumerSecret: process.env.TWITTER_CONSUMER_SECRET,
callbackURL: process.env.TWITTER_CALLBACK,
},
async (token, tokenSecret, profile, done) => {
GetSocialTwitterGithub(
token, tokenSecret, profile, done
);
}
);
export default Twitter;
2 changes: 1 addition & 1 deletion middlewares/uniquestring.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Generate {
* @param { String } image
* @returns { String } string
*/
generateLargeTwitterProfile(image) {
largeTwitterImage(image) {
return image.replace('_normal', '');
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"morgan": "^1.9.1",
"passport": "^0.4.0",
"passport-facebook": "^3.0.0",
"passport-github": "^1.1.0",
"passport-google-oauth20": "^2.0.0",
"passport-linkedin": "^1.0.0",
"passport-twitter": "^1.0.4",
Expand Down
3 changes: 3 additions & 0 deletions routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ route.get('/auth/facebook/callback', passport.authenticate('facebook', { session
// passport.authenticate twitter
route.get('/auth/twitter', passport.authenticate('twitter'));
route.get('/auth/twitter/callback', passport.authenticate('twitter', { failureRedirect: '/auth/twitter' }), user.socialLogin);
// passport.authenticate github
route.get('/auth/github', passport.authenticate('github'));
route.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/auth/github' }), user.socialLogin);

export default route;
2 changes: 1 addition & 1 deletion test/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('/ Should remove special character from strings', () => {
*/
describe('/ Should make twitter image large', () => {
it('it should remove _normal from the image URL', (done) => {
const result = uniusername.generateLargeTwitterProfile('_normalthisisimage.jpg');
const result = uniusername.largeTwitterImage('_normalthisisimage.jpg');
result.should.be.eql('thisisimage.jpg');
done();
});
Expand Down

0 comments on commit d5e3853

Please sign in to comment.