-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassport-setup.js
More file actions
44 lines (36 loc) · 1.34 KB
/
passport-setup.js
File metadata and controls
44 lines (36 loc) · 1.34 KB
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
const passport = require('passport');
const { pool } = require('./db');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
// Your Google OAuth credentials
const GOOGLE_CLIENT_ID = process.env.GOOGLE_CLIENT_ID_AUTH;
const GOOGLE_CLIENT_SECRET = process.env.GOOGLE_CLIENT_SECRET_AUTH;
passport.use(new GoogleStrategy({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: '/auth/google/callback'
},
async (accessToken, refreshToken, profile, done) => {
try {
const user = {
googleId: profile.id,
name: profile.displayName,
email: profile.emails[0].value
};
const password_hash = 'GGAcount: '+ user.googleId;
const { rows } = await pool.query(`
INSERT INTO users (email, password_hash)
VALUES ($1, $2)
ON CONFLICT (email) DO NOTHING
RETURNING id, email, password_hash
`, [user.email, password_hash]);
if (rows.length === 0) console.log('User already exists:', user.email);
// Send the user object to passport
done(null, { id: rows[0]?.id, email: user.email });
} catch (err) {
done(err, null);
}
}
));
// For sessions
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user));