Skip to content

Commit 6405bb5

Browse files
committed
Add support of google signin
1 parent 900141d commit 6405bb5

10 files changed

Lines changed: 47 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ Third-party integration api key settings
121121
---
122122
| service | file path | description |
123123
| ------- | --------- | ----------- |
124-
| facebook, twitter, github, gitlab, dropbox | `config.json` | for signin |
124+
| facebook, twitter, github, gitlab, dropbox, google | `config.json` | for signin |
125125
| imgur | `config.json` | for image upload |
126126
| google drive, dropbox | `public/js/common.js` | for export and import |
127127

app.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,19 @@ if (config.dropbox) {
317317
res.redirect(config.serverurl);
318318
});
319319
}
320+
//google auth
321+
if (config.google) {
322+
app.get('/auth/google',
323+
passport.authenticate('google', { scope: ['profile'] }));
324+
//google auth callback
325+
app.get('/auth/google/callback',
326+
passport.authenticate('google', {
327+
failureRedirect: config.serverurl
328+
}),
329+
function (req, res) {
330+
res.redirect(config.serverurl);
331+
});
332+
}
320333
//logout
321334
app.get('/logout', function (req, res) {
322335
if (config.debug && req.isAuthenticated())

config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
"clientID": "change this",
4242
"clientSecret": "change this"
4343
},
44+
"google": {
45+
"clientID": "change this",
46+
"clientSecret": "change this"
47+
},
4448
"imgur": {
4549
"clientID": "change this"
4650
}

lib/auth.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var TwitterStrategy = require('passport-twitter').Strategy;
66
var GithubStrategy = require('passport-github').Strategy;
77
var GitlabStrategy = require('passport-gitlab2').Strategy;
88
var DropboxStrategy = require('passport-dropbox-oauth2').Strategy;
9+
var GoogleStrategy = require('passport-google-oauth20').Strategy;
910

1011
//core
1112
var config = require('./config.js');
@@ -100,4 +101,12 @@ if (config.dropbox) {
100101
clientSecret: config.dropbox.clientSecret,
101102
callbackURL: config.serverurl + '/auth/dropbox/callback'
102103
}, callback));
104+
}
105+
//google
106+
if (config.google) {
107+
passport.use(new GoogleStrategy({
108+
clientID: config.google.clientID,
109+
clientSecret: config.google.clientSecret,
110+
callbackURL: config.serverurl + '/auth/google/callback'
111+
}, callback));
103112
}

lib/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ var twitter = config.twitter || false;
6161
var github = config.github || false;
6262
var gitlab = config.gitlab || false;
6363
var dropbox = config.dropbox || false;
64+
var google = config.google || false;
6465
var imgur = config.imgur || false;
6566

6667
function getserverurl() {
@@ -113,5 +114,6 @@ module.exports = {
113114
github: github,
114115
gitlab: gitlab,
115116
dropbox: dropbox,
117+
google: google,
116118
imgur: imgur
117119
};

lib/models/user.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ module.exports = function (sequelize, DataTypes) {
7676
//no image api provided, use gravatar
7777
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0].value);
7878
break;
79+
case "google":
80+
photo = profile.photos[0].value.replace(/(\?sz=)\d*$/i, '$196');
81+
break;
7982
}
8083
return photo;
8184
}

lib/response.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ function showIndex(req, res, next) {
9696
twitter: config.twitter,
9797
github: config.github,
9898
gitlab: config.gitlab,
99-
dropbox: config.dropbox
99+
dropbox: config.dropbox,
100+
google: config.google
100101
});
101102
res.write(content);
102103
res.end();
@@ -127,7 +128,8 @@ function responseHackMD(res, note) {
127128
twitter: config.twitter,
128129
github: config.github,
129130
gitlab: config.gitlab,
130-
dropbox: config.dropbox
131+
dropbox: config.dropbox,
132+
google: config.google
131133
});
132134
var buf = html;
133135
res.writeHead(200, {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"passport-facebook": "^2.1.0",
4141
"passport-github": "^1.1.0",
4242
"passport-gitlab2": "^2.2.0",
43+
"passport-google-oauth20": "^1.0.0",
4344
"passport-twitter": "^1.0.4",
4445
"passport.socketio": "^3.6.1",
4546
"pg": "^4.5.3",

public/views/index.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<p class="lead">
5959
Realtime collaborative markdown notes on all platforms.
6060
</p>
61-
<% if(facebook || twitter || github || gitlab || dropbox) { %>
61+
<% if(facebook || twitter || github || gitlab || dropbox || google) { %>
6262
<a type="button" class="btn btn-lg btn-success ui-signin" data-toggle="modal" data-target=".signin-modal" style="display:none;">Sign In</a>
6363
<div class="ui-or" style="display:none;">Or</div>
6464
<% }%>
@@ -72,7 +72,7 @@
7272
</div>
7373

7474
<div id="history" class="section" style="display:none;">
75-
<% if(facebook || twitter || github || gitlab || dropbox) { %>
75+
<% if(facebook || twitter || github || gitlab || dropbox || google) { %>
7676
<div class="ui-signin">
7777
<h4>
7878
<a type="button" class="btn btn-success" data-toggle="modal" data-target=".signin-modal">Sign In</a> to get own history!

public/views/signin-modal.ejs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@
2323
<i class="fa fa-github"></i> Sign in via GitHub
2424
</a>
2525
<% } %>
26+
<% if(gitlab) { %>
27+
<a href="<%- url %>/auth/gitlab" class="btn btn-lg btn-block btn-social btn-soundcloud">
28+
<i class="fa fa-gitlab"></i> Sign in via GitLab
29+
</a>
30+
<% } %>
2631
<% if(dropbox) { %>
2732
<a href="<%- url %>/auth/dropbox" class="btn btn-lg btn-block btn-social btn-dropbox">
2833
<i class="fa fa-dropbox"></i> Sign in via Dropbox
2934
</a>
3035
<% } %>
31-
<% if(gitlab) { %>
32-
<a href="<%- url %>/auth/gitlab" class="btn btn-lg btn-block btn-social btn-soundcloud">
33-
<i class="fa fa-gitlab"></i> Sign in via GitLab
36+
<% if(google) { %>
37+
<a href="<%- url %>/auth/google" class="btn btn-lg btn-block btn-social btn-google">
38+
<i class="fa fa-google"></i> Sign in via Google
3439
</a>
3540
<% } %>
3641
</div>

0 commit comments

Comments
 (0)