-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.mjs
51 lines (42 loc) · 1.21 KB
/
passport.mjs
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
import passport from 'passport';
import passportGithub from 'passport-github2';
import dotenv from 'dotenv';
import db from './models/index.mjs';
dotenv.config({ path: './.env' });
export default function runPassportSetup() {
const GitHubStrategy = passportGithub.Strategy;
passport.use(new GitHubStrategy({
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: process.env.APP_CALLBACK_URL,
},
(async (accessToken, refreshToken, profile, done) => {
// create or find user
const user = await db.User.findOrCreate({
where: { githubUsername: profile.username },
defaults: {
name: profile.displayName,
email: profile.email,
},
});
// save access token
await db.OAuthToken.create({
userId: user.id,
accessToken,
});
user.accessToken = accessToken;
done(null, user);
})));
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser(async (user, done) => {
done(null, user);
// try {
// const user = await db.User.findOne({ where: { linkedinId: id } });
// done(null, user);
// } catch (err) {
// done(err);
// }
});
}