-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
84 lines (74 loc) · 1.97 KB
/
Copy pathapp.js
File metadata and controls
84 lines (74 loc) · 1.97 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
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
import cors from "cors";
import path from "path";
import dotenv from "dotenv";
import express from "express";
import jwt from "jsonwebtoken";
import passport from "passport";
import mongoose from "mongoose";
import session from "express-session";
import authRoutes from "./routes/auth.js";
import userRoutes from "./routes/user.js";
import corsOption from "./config/cors.js";
import { configureGoogleAuth } from "./utils/googleAuth.js";
import { currDir } from "./config/resPath.js";
const app = express();
const PORT = process.env.PORT || 3000;
dotenv.config();
try {
mongoose.connect(process.env.DATABASE);
console.log("db connected");
} catch (error) {
console.log("Error:", error);
}
app.use(cors(corsOption));
app.use(express.json());
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
})
);
//setup passport
app.use(passport.initialize());
app.use(passport.session());
configureGoogleAuth();
app.get("/login/success", async (req, res) => {
if (req.user) {
const token = jwt.sign({ id: req.user._id }, process.env.JWT_SECRET);
res.status(200).json({
message: "User Authenticated",
token,
user: {
id: req.user._id,
email: req.user.email,
displayName: req.user.displayName,
image: req.user.image,
},
});
} else {
res.status(400).json({
message: "User Not Authenticated",
});
}
});
app.get("/logout", (req, res) => {
req.logout(function (err) {
if (err) {
return next(err);
}
res.redirect("https://animaxx.vercel.app/");
});
});
app.use("/auth", authRoutes);
app.use("/user", userRoutes);
if (process.env.NODE_ENV === "production") {
const __dirname = path.resolve();
app.use(express.static(path.join(__dirname, "client", "dist")));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "dist", "index.html"));
});
}
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});