-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
142 lines (127 loc) · 3.94 KB
/
auth.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const app = require('APP'), {env} = app
const debug = require('debug')(`${app.name}:auth`)
const passport = require('passport')
const User = require('APP/db/models/user')
const OAuth = require('APP/db/models/oauth')
const auth = require('express').Router()
/*************************
* Auth strategies
*
* The OAuth model knows how to configure Passport middleware.
* To enable an auth strategy, ensure that the appropriate
* environment variables are set.
*
* You can do it on the command line:
*
* FACEBOOK_CLIENT_ID=abcd FACEBOOK_CLIENT_SECRET=1234 npm start
*
* Or, better, you can create a ~/.$your_app_name.env.json file in
* your home directory, and set them in there:
*
* {
* FACEBOOK_CLIENT_ID: 'abcd',
* FACEBOOK_CLIENT_SECRET: '1234',
* }
*
* Concentrating your secrets this way will make it less likely that you
* accidentally push them to Github, for example.
*
* When you deploy to production, you'll need to set up these environment
* variables with your hosting provider.
**/
// Facebook needs the FACEBOOK_CLIENT_ID and FACEBOOK_CLIENT_SECRET
// environment variables.
OAuth.setupStrategy({
provider: 'facebook',
strategy: require('passport-facebook').Strategy,
config: {
clientID: env.FACEBOOK_CLIENT_ID,
clientSecret: env.FACEBOOK_CLIENT_SECRET,
callbackURL: `${app.baseUrl}/api/auth/facebook/login`,
},
passport
})
// Google needs the GOOGLE_CONSUMER_SECRET AND GOOGLE_CONSUMER_KEY
// environment variables.
OAuth.setupStrategy({
provider: 'google',
strategy: require('passport-google-oauth20').Strategy,
config: {
// consumerKey: env.GOOGLE_CONSUMER_KEY,
// consumerSecret: env.GOOGLE_CONSUMER_SECRET,
clientID: '444169114220-18uoi6t7bukvh0pk3e4ojck4s4cjhuq7.apps.googleusercontent.com',
clientSecret: 'G06vM16MeGHZnvwAVflx9-UZ',
callbackURL: `${app.baseUrl}/api/auth/google/login`,
},
passport
})
// Github needs the GITHUB_CLIENT_ID AND GITHUB_CLIENT_SECRET
// environment variables.
OAuth.setupStrategy({
provider: 'github',
strategy: require('passport-github2').Strategy,
config: {
clientID: env.GITHUB_CLIENT_ID,
clientSecrets: env.GITHUB_CLIENT_SECRET,
callbackURL: `${app.baseUrl}/api/auth/login/github`,
},
passport
})
// Other passport configuration:
passport.serializeUser((user, done) => {
done(null, user.id)
})
passport.deserializeUser(
(id, done) => {
// debug('will deserialize user.id=%d', id)
User.findById(id)
.then(user => {
// debug('deserialize did ok user.id=%d', user.id)
done(null, user)
})
.catch(err => {
debug('deserialize did fail err=%s', err)
done(err)
})
}
)
passport.use(new (require('passport-local').Strategy) (
// passport.use(new (require('passport-google-oauth').Strategy) (
(email, password, done) => {
debug('will authenticate user(email: "%s")', email)
User.findOne({where: {email}})
.then(user => {
if (!user) {
debug('authenticate user(email: "%s") did fail: no such user', email)
return done(null, false, { message: 'Login incorrect' })
}
return user.authenticate(password)
.then(ok => {
if (!ok) {
debug('authenticate user(email: "%s") did fail: bad password')
return done(null, false, { message: 'Login incorrect' })
}
debug('authenticate user(email: "%s") did ok: user.id=%d', user.id)
done(null, user)
})
})
.catch(done)
}
))
auth.get('/whoami', (req, res) => res.send(req.user))
auth.get('/:strategy/login', (req, res, next) =>
passport.authenticate(req.params.strategy, {
scope: 'email',
successRedirect: '/'
})(req, res, next)
)
auth.post('/:strategy/login', (req, res, next) =>
passport.authenticate(req.params.strategy, {
successRedirect: '/'
})(req, res, next)
)
auth.post('/logout', (req, res, next) => {
req.logout()
res.redirect('/api/auth/whoami')
})
module.exports = auth