-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (102 loc) · 2.56 KB
/
index.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
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const bcrypt = require("bcrypt");
const session = require("express-session");
const MongoStore = require("connect-mongo");
const passport = require("passport");
const PORT = 8080;
const app = express();
app.use([
cors({
origin: process.env.FRONTEND_DOMAIN,
credentials: true,
methods: ["GET", "PUT", "PATCH", "PUT", "DELETE"],
}),
express.json(),
express.urlencoded({ extended: true }),
]);
const sessionStore = new MongoStore({
mongoUrl: process.env.MONGO_URL,
collectionName: "session",
});
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
store: sessionStore,
cookie: {
maxAge: 1000 * 60 * 60 * 24,
},
})
);
app.use(passport.initialize());
app.use(passport.session());
app.post("/register", async (req, res) => {
const { userName, email, password } = req.body;
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
const newAuth = new authModel({
userName: userName,
email: email,
password: hashedPassword,
});
try {
const user = await authModel.findOne({ email: email });
if (user) res.json("Already Registerd");
else {
const savedUser = await newAuth.save();
res.send(savedUser);
}
} catch (err) {
res.status(400).send(err);
}
});
//Google authentication using passport
app.get(
"/google",
passport.authenticate("google", { scope: ["profile", "email"] })
);
app.get(
"/google/callback",
passport.authenticate("google", {
failureRedirect: process.env.FRONTEND_DOMAIN,
successRedirect: `${process.env.FRONTEND_DOMAIN}/Home`,
})
);
//For Facebook Authentication
app.get("/facebook", passport.authenticate("facebook", { scope: ["email"] }));
app.get(
"/facebook/callback",
passport.authenticate("facebook", {
failureRedirect: process.env.FRONTEND_DOMAIN,
successRedirect: `${process.env.FRONTEND_DOMAIN}/Home`,
})
);
//Local Login
app.post(
"/login",
passport.authenticate("local", {
failureRedirect: process.env.FRONTEND_DOMAIN,
}),
(req, res) => {
res.json({ success: "successfully logged in" });
}
);
//logout
app.get("/logout", (req, res, next) => {
req.logOut((err) => {
if (err) res.send(err);
else res.json({ success: "logged out" });
});
});
app.get("/getUser", (req, res, next) => {
if (req.user) {
res.json(req.user);
}
});
app.listen(PORT, () => {
console.log(`Server Running On Port : ${PORT} `);
});
module.exports = app;