-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
142 lines (125 loc) · 4.01 KB
/
server.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const express = require("express");
const connectDB = require("./config/database");
const cors = require("cors");
const passport = require("passport");
const JwtStrategy = require("passport-jwt").Strategy;
const ExtractJwt = require("passport-jwt").ExtractJwt;
const User = require("./src/models/User");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const jwt = require("jsonwebtoken");
const crypto = require("crypto");
// Require dotenv and load the .env file
require("dotenv").config();
// Use environment variables in your code
const clientID = process.env.GOOGLE_CLIENT_ID;
const clientSecret = process.env.GOOGLE_CLIENT_SECRET;
const app = express();
// Connect to MongoDB
connectDB().catch((err) => console.log(err));
const PORT = process.env.PORT || 3000;
// Middleware: Body parsing middleware to handle JSON data
app.use(express.json());
// Enable CORS for all routes
app.use(cors());
// Generate a secret key for JWT
const secretKey = crypto.randomBytes(64).toString("hex");
// Passport JWT Strategy (for JWT authentication)
const opts = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: secretKey,
};
passport.use(
new JwtStrategy(opts, async (jwt_payload, done) => {
try {
const user = await User.findById(jwt_payload.sub);
if (user) {
return done(null, user);
}
return done(null, false);
} catch (error) {
return done(error, false);
}
})
);
// Google OAuth Strategy
passport.use(
new GoogleStrategy(
{
clientID: clientID,
clientSecret: clientSecret,
callbackURL: "/auth/google/callback",
scope: ["profile", "https://www.googleapis.com/auth/gmail.readonly"],
},
async (accessToken, refreshToken, profile, done) => {
try {
// Handle Google authentication logic and user creation if needed
// Example: Check if the user exists or create a new one
let user = await User.findOne({ googleId: profile.id });
if (!user) {
user = await User.create({
googleId: profile.id,
name: profile.displayName,
picture: profile.photos[0].value,
// Add other necessary user details
});
}
return done(null, user);
} catch (error) {
return done(error, false);
}
}
)
);
// Initialize Passport
app.use(passport.initialize());
// trigger auth
app.get(
"/auth/google",
passport.authenticate("google", { scope: ["profile"] })
);
// Google OAuth Callback Route
app.get(
"/auth/google/callback",
passport.authenticate("google", {
failureRedirect: "/login-failure",
session: false,
}),
(req, res) => {
// Assuming 'req.user' contains the authenticated user after Google authentication
const { _id, googleId, name } = req.user;
// Create a payload for the JWT token
const payload = {
sub: _id,
googleId: googleId,
name: name,
// Add other necessary data to be included in the token
};
// Sign the JWT token with the secret key
jwt.sign(payload, secretKey, { expiresIn: "1h" }, (err, token) => {
if (err) {
// Handle error while signing the token
res.status(500).json({ error: "Failed to generate token" });
} else {
/* old method didnt work(cuz cant fetch(CORS) from front end, and takes to google api) Send the JWT token back to the client
res.json({ token, name }); */
// Redirect the user to the desired frontend page with the token as a query parameter
res.redirect(
`http://localhost:8080/todoapp.html?token=${encodeURIComponent(
token
)}`
);
}
});
}
);
app.get("/logout", (req, res) => {
// Respond with a success message or appropriate response
res.json({ message: "Logged out successfully" });
});
// API Routes (Tasks, Users, etc.)
const tasksRoutes = require("./src/routes/tasks");
app.use("/tasks", tasksRoutes);
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});