Skip to content

Commit

Permalink
feat(auth): add google oauth
Browse files Browse the repository at this point in the history
Close #17
  • Loading branch information
g-div committed Jan 5, 2018
1 parent ea4d4d2 commit 0a86479
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
4 changes: 4 additions & 0 deletions config.tpl.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"github": {
"client_id": "xxxxx",
"client_secret": "xxxxx"
},
"google": {
"client_id": "xxxxx",
"client_secret": "xxxxx"
}
},
"notify": {
Expand Down
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"moment": "^2.18.1",
"passport": "^0.4.0",
"passport-github2": "^0.1.11",
"passport-google-oauth": "^1.0.0",
"passport-twitter": "^1.0.4",
"pushover-notifications": "^0.2.4",
"request": "^2.83.0",
Expand Down
29 changes: 28 additions & 1 deletion src/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const session = require('express-session');
const SQLiteStore = require('connect-sqlite3')(session);
const TwitterStrategy = require('passport-twitter').Strategy;
const GitHubStrategy = require('passport-github2').Strategy;
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;

const queries = require('./db/queries');
const config = require('../config.json');
Expand All @@ -29,7 +30,7 @@ function init(app, db, domain) {
var trusted = config.trust &&
config.trust[user.provider] &&
config.trust[user.provider].indexOf(user.id) > -1 ? 1 : 0;
const c_args = [user.provider, user.id, user.displayName, user.username, trusted];
const c_args = [user.provider, user.id, user.displayName, user.username || user.displayName, trusted];
db.run(queries.create_user, c_args, (err, res) => {
if (err) return console.error(err);
db.get(queries.find_user, [user.provider, user.id], (err, row) => {
Expand Down Expand Up @@ -96,6 +97,32 @@ function init(app, db, domain) {
}
);
}

// google oauth
if (config.oauth.google) {
providers.push({ id: 'google', name: 'Google' });
passport.use(new GoogleStrategy({
clientID: config.oauth.google.client_id,
clientSecret: config.oauth.google.client_secret,
callbackURL: `${config.schnack_host}/auth/google/callback`
}, (accessToken, refreshToken, profile, done) => {
done(null, profile);
}));

app.get('/auth/google',
passport.authenticate('google', {
scope: ['https://www.googleapis.com/auth/plus.login']
})
);

app.get('/auth/google/callback',
passport.authenticate('google', {
failureRedirect: '/login'
}), (request, reply) => {
reply.redirect('/success');
}
);
}
}

function getAuthorUrl(comment) {
Expand Down

0 comments on commit 0a86479

Please sign in to comment.