-
Notifications
You must be signed in to change notification settings - Fork 74
/
auth.js
71 lines (64 loc) · 1.98 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
const passport = require('koa-passport')
const fetchUser = (() => {
// This is an example! Use password hashing in your project and avoid storing passwords in your code
const user = { id: 1, username: 'test', password: 'test' }
return async function() {
return user
}
})()
passport.serializeUser(function(user, done) {
done(null, user.id)
})
passport.deserializeUser(async function(id, done) {
try {
const user = await fetchUser()
done(null, user)
} catch(err) {
done(err)
}
})
const LocalStrategy = require('passport-local').Strategy
passport.use(new LocalStrategy(function(username, password, done) {
fetchUser()
.then(user => {
if (username === user.username && password === user.password) {
done(null, user)
} else {
done(null, false)
}
})
.catch(err => done(err))
}))
const FacebookStrategy = require('passport-facebook').Strategy
passport.use(new FacebookStrategy({
clientID: 'your-client-id',
clientSecret: 'your-secret',
callbackURL: 'http://localhost:' + (process.env.PORT || 3000) + '/auth/facebook/callback'
},
function(token, tokenSecret, profile, done) {
// retrieve user ...
fetchUser().then(user => done(null, user))
}
))
const TwitterStrategy = require('passport-twitter').Strategy
passport.use(new TwitterStrategy({
consumerKey: 'your-consumer-key',
consumerSecret: 'your-secret',
callbackURL: 'http://localhost:' + (process.env.PORT || 3000) + '/auth/twitter/callback'
},
function(token, tokenSecret, profile, done) {
// retrieve user ...
fetchUser().then(user => done(null, user))
}
))
const GoogleStrategy = require('passport-google-auth').Strategy
passport.use(new GoogleStrategy({
clientId: 'your-client-id',
clientSecret: 'your-secret',
callbackURL: 'http://localhost:' + (process.env.PORT || 3000) + '/auth/google/callback'
},
function(token, tokenSecret, profile, done) {
// retrieve user ...
fetchUser().then(user => done(null, user))
}
))