summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCheng-Han, Wu2016-05-21 22:48:00 +0800
committerCheng-Han, Wu2016-05-21 22:48:00 +0800
commit6405bb505695a5592efe8aa8d5bde9e723fadfc0 (patch)
tree882c74f212d4892ae0013d5ab394404f096b693b
parent900141daff05feeeaae5f537d2f53862b7200651 (diff)
Add support of google signin
-rw-r--r--README.md2
-rw-r--r--app.js13
-rw-r--r--config.json4
-rw-r--r--lib/auth.js9
-rw-r--r--lib/config.js2
-rw-r--r--lib/models/user.js3
-rw-r--r--lib/response.js6
-rw-r--r--package.json1
-rw-r--r--public/views/index.ejs4
-rw-r--r--public/views/signin-modal.ejs11
10 files changed, 47 insertions, 8 deletions
diff --git a/README.md b/README.md
index a1e93ea1..84f621a4 100644
--- a/README.md
+++ b/README.md
@@ -121,7 +121,7 @@ Third-party integration api key settings
---
| service | file path | description |
| ------- | --------- | ----------- |
-| facebook, twitter, github, gitlab, dropbox | `config.json` | for signin |
+| facebook, twitter, github, gitlab, dropbox, google | `config.json` | for signin |
| imgur | `config.json` | for image upload |
| google drive, dropbox | `public/js/common.js` | for export and import |
diff --git a/app.js b/app.js
index 5e75ac59..4b4e1766 100644
--- a/app.js
+++ b/app.js
@@ -317,6 +317,19 @@ if (config.dropbox) {
res.redirect(config.serverurl);
});
}
+//google auth
+if (config.google) {
+ app.get('/auth/google',
+ passport.authenticate('google', { scope: ['profile'] }));
+ //google auth callback
+ app.get('/auth/google/callback',
+ passport.authenticate('google', {
+ failureRedirect: config.serverurl
+ }),
+ function (req, res) {
+ res.redirect(config.serverurl);
+ });
+}
//logout
app.get('/logout', function (req, res) {
if (config.debug && req.isAuthenticated())
diff --git a/config.json b/config.json
index c65e9c7c..35efcd25 100644
--- a/config.json
+++ b/config.json
@@ -41,6 +41,10 @@
"clientID": "change this",
"clientSecret": "change this"
},
+ "google": {
+ "clientID": "change this",
+ "clientSecret": "change this"
+ },
"imgur": {
"clientID": "change this"
}
diff --git a/lib/auth.js b/lib/auth.js
index f4a0ec8f..76a962f5 100644
--- a/lib/auth.js
+++ b/lib/auth.js
@@ -6,6 +6,7 @@ var TwitterStrategy = require('passport-twitter').Strategy;
var GithubStrategy = require('passport-github').Strategy;
var GitlabStrategy = require('passport-gitlab2').Strategy;
var DropboxStrategy = require('passport-dropbox-oauth2').Strategy;
+var GoogleStrategy = require('passport-google-oauth20').Strategy;
//core
var config = require('./config.js');
@@ -100,4 +101,12 @@ if (config.dropbox) {
clientSecret: config.dropbox.clientSecret,
callbackURL: config.serverurl + '/auth/dropbox/callback'
}, callback));
+}
+//google
+if (config.google) {
+ passport.use(new GoogleStrategy({
+ clientID: config.google.clientID,
+ clientSecret: config.google.clientSecret,
+ callbackURL: config.serverurl + '/auth/google/callback'
+ }, callback));
} \ No newline at end of file
diff --git a/lib/config.js b/lib/config.js
index cdaec31c..1f824d0a 100644
--- a/lib/config.js
+++ b/lib/config.js
@@ -61,6 +61,7 @@ var twitter = config.twitter || false;
var github = config.github || false;
var gitlab = config.gitlab || false;
var dropbox = config.dropbox || false;
+var google = config.google || false;
var imgur = config.imgur || false;
function getserverurl() {
@@ -113,5 +114,6 @@ module.exports = {
github: github,
gitlab: gitlab,
dropbox: dropbox,
+ google: google,
imgur: imgur
};
diff --git a/lib/models/user.js b/lib/models/user.js
index 237532c1..3142a53d 100644
--- a/lib/models/user.js
+++ b/lib/models/user.js
@@ -76,6 +76,9 @@ module.exports = function (sequelize, DataTypes) {
//no image api provided, use gravatar
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0].value);
break;
+ case "google":
+ photo = profile.photos[0].value.replace(/(\?sz=)\d*$/i, '$196');
+ break;
}
return photo;
}
diff --git a/lib/response.js b/lib/response.js
index 9c710751..062bc5db 100644
--- a/lib/response.js
+++ b/lib/response.js
@@ -96,7 +96,8 @@ function showIndex(req, res, next) {
twitter: config.twitter,
github: config.github,
gitlab: config.gitlab,
- dropbox: config.dropbox
+ dropbox: config.dropbox,
+ google: config.google
});
res.write(content);
res.end();
@@ -127,7 +128,8 @@ function responseHackMD(res, note) {
twitter: config.twitter,
github: config.github,
gitlab: config.gitlab,
- dropbox: config.dropbox
+ dropbox: config.dropbox,
+ google: config.google
});
var buf = html;
res.writeHead(200, {
diff --git a/package.json b/package.json
index bacd2336..85d00072 100644
--- a/package.json
+++ b/package.json
@@ -40,6 +40,7 @@
"passport-facebook": "^2.1.0",
"passport-github": "^1.1.0",
"passport-gitlab2": "^2.2.0",
+ "passport-google-oauth20": "^1.0.0",
"passport-twitter": "^1.0.4",
"passport.socketio": "^3.6.1",
"pg": "^4.5.3",
diff --git a/public/views/index.ejs b/public/views/index.ejs
index 7d16428c..4aa6ae0a 100644
--- a/public/views/index.ejs
+++ b/public/views/index.ejs
@@ -58,7 +58,7 @@
<p class="lead">
Realtime collaborative markdown notes on all platforms.
</p>
- <% if(facebook || twitter || github || gitlab || dropbox) { %>
+ <% if(facebook || twitter || github || gitlab || dropbox || google) { %>
<a type="button" class="btn btn-lg btn-success ui-signin" data-toggle="modal" data-target=".signin-modal" style="display:none;">Sign In</a>
<div class="ui-or" style="display:none;">Or</div>
<% }%>
@@ -72,7 +72,7 @@
</div>
<div id="history" class="section" style="display:none;">
- <% if(facebook || twitter || github || gitlab || dropbox) { %>
+ <% if(facebook || twitter || github || gitlab || dropbox || google) { %>
<div class="ui-signin">
<h4>
<a type="button" class="btn btn-success" data-toggle="modal" data-target=".signin-modal">Sign In</a> to get own history!
diff --git a/public/views/signin-modal.ejs b/public/views/signin-modal.ejs
index 4eb33bf3..293480b9 100644
--- a/public/views/signin-modal.ejs
+++ b/public/views/signin-modal.ejs
@@ -23,14 +23,19 @@
<i class="fa fa-github"></i> Sign in via GitHub
</a>
<% } %>
+ <% if(gitlab) { %>
+ <a href="<%- url %>/auth/gitlab" class="btn btn-lg btn-block btn-social btn-soundcloud">
+ <i class="fa fa-gitlab"></i> Sign in via GitLab
+ </a>
+ <% } %>
<% if(dropbox) { %>
<a href="<%- url %>/auth/dropbox" class="btn btn-lg btn-block btn-social btn-dropbox">
<i class="fa fa-dropbox"></i> Sign in via Dropbox
</a>
<% } %>
- <% if(gitlab) { %>
- <a href="<%- url %>/auth/gitlab" class="btn btn-lg btn-block btn-social btn-soundcloud">
- <i class="fa fa-gitlab"></i> Sign in via GitLab
+ <% if(google) { %>
+ <a href="<%- url %>/auth/google" class="btn btn-lg btn-block btn-social btn-google">
+ <i class="fa fa-google"></i> Sign in via Google
</a>
<% } %>
</div>