-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (68 loc) · 2.31 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
const express = require('express');
const passport = require('passport');
const session = require('express-session');
const crypto = require('crypto');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const passportSetup = require('./passport-setup');
const authRoutes = require('./routes/auth');
const multer = require('multer');
const app = express();
app.use(express.json());
app.use(bodyParser.json());
const generateSessionSecret = () => {
return crypto.randomBytes(32).toString('hex');
};
const sessionSecret = generateSessionSecret();
// console.log('Session Secret:', sessionSecret);
app.use(session({
secret: sessionSecret,
resave: true,
saveUninitialized: true,
}));
mongoose.connect('mongodb://127.0.0.1:27017/skintales')
.then(() => {
console.log('Connected to MongoDB');
})
.catch((error) => {
console.error('Error connecting to MongoDB:', error.message);
});
const User = require('./model/user');
const Post = require('./model/post');
const userRoutes = require('./routes/user');
const postRoutes = require('./routes/post');
const uploadRoutes = require('./routes/upload');
app.use(userRoutes);
app.use(postRoutes);
app.use(uploadRoutes);
app.use(passport.initialize());
app.use(passport.session());
passportSetup(passport);
//google oauth route
app.get(
"/auth/google",
passport.authenticate("google", {
successRedirect: "/welcomeOauth", // Redirect to a success page
failureRedirect: "/error", // Redirect to the home page or login page on failure
})
);
// Handle the Google OAuth callback route
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/error' }),
(req, res) => {
// Successful authentication, redirect to the welcome page or your desired route
res.redirect('/welcomeOauth');
}
);
// Handle the success redirect route
app.get('/welcomeOauth', (req, res) => {
res.send('Welcome! Google OAuth login successful.');
});
// Handle the failure redirect route
app.get('/error', (req, res) => {
res.send('Oops! Something went wrong with Google OAuth login.');
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});