-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthentication.js
116 lines (110 loc) · 3.28 KB
/
authentication.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
import express from "express";
import bodyParser from "body-parser";
import env from "dotenv";
import { createTables,db } from "./dbmodel.js";
import bcrypt from "bcrypt";
import passport from "passport";
import { Strategy } from "passport-local";
import GoogleStrategy from "passport-google-oauth2";
app.post(
"/login",
passport.authenticate("local", {
successRedirect: "/secrets",
failureRedirect: "/login",
})
);
app.post("/register", async (req, res) => {
const {email,username,password} = req.body;
try {
const checkResult = await db.query("SELECT * FROM users WHERE email = $1", [
email,
]);
if (checkResult.rows.length > 0) {
req.redirect("/login");
} else {
bcrypt.hash(password, saltRounds, async (err, hash) => {
if (err) {
console.error("Error hashing password:", err);
} else {
const result = await db.query(
"INSERT INTO users (username,email, password) VALUES ($1, $2,$3) RETURNING *",
[username,email, hash]
);
const user = result.rows[0];
req.login(user, (err) => {
console.log("success");
res.redirect("/secrets");
});
}
});
}
} catch (err) {
console.log(err);
}
});
passport.use(
"local",
new Strategy(async function verify(username, password, cb) {
try {
const result = await db.query("SELECT * FROM users WHERE username = $1 ", [
username,
]);
if (result.rows.length > 0) {
const user = result.rows[0];
const storedHashedPassword = user.password;
bcrypt.compare(password, storedHashedPassword, (err, valid) => {
if (err) {
console.error("Error comparing passwords:", err);
return cb(err);
} else {
if (valid) {
return cb(null, user);
} else {
return cb(null, false);
}
}
});
} else {
return cb("User not found");
}
} catch (err) {
console.log(err);
}
})
);
passport.use(
"google",
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/google/secrets",
userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo",
},
async (accessToken, refreshToken, profile, cb) => {
try {
console.log(profile);
const result = await db.query("SELECT * FROM users WHERE email = $1", [
profile.email,
]);
if (result.rows.length === 0) {
const newUser = await db.query(
"INSERT INTO users (username,email, password) VALUES ($1, $2)",
[profile.email,profile.email, "google"]
);
return cb(null, newUser.rows[0]);
} else {
return cb(null, result.rows[0]);
}
} catch (err) {
return cb(err);
}
}
)
);
passport.serializeUser((user, cb) => {
cb(null, user);
});
passport.deserializeUser((user, cb) => {
cb(null, user);
});