-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
72 lines (59 loc) · 1.81 KB
/
app.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
import express from 'express'
import bodyParser from 'body-parser'
import ejsLayouts from 'express-ejs-layouts'
import path from 'path'
import dotenv from 'dotenv'
import session from 'express-session'
import passport from 'passport'
import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
import { connectUsingMongoose } from './config/mongodb.js'
import router from "./routes/routes.js"
import authrouter from "./routes/authRoutes.js"
//CONFIG env
dotenv.config()
const app = express()
//SESSION
app.use(session({ secret: 'SecretKey', resave: false, saveUninitialized: true, cookie: { secure: false } }));
//MIDDLEWARE
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//Passport
app.use(passport.initialize());
app.use(passport.session());
passport.use(
new GoogleStrategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "https://node-authentication-gpu4.onrender.com/auth/google/callback",
scope: ["profile", "email"],
},
function (accessToken, refreshToken, profile, callback) {
// console.log('Profile Data')
// console.log(profile)
callback(null, profile);
}
)
);
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
// Set Templates
app.set("view engine", "ejs"); // Define template engine
app.use(ejsLayouts); // Use base template
app.set("views", path.join(path.resolve(), "views")); // Define template directory
//connect DB
connectUsingMongoose()
//ROUTES
app.get('/', (req, res) => {
res.send('Hey Ninja ! Go to /user/signin to see the magic.')
})
app.use('/user', router)
app.use('/auth', authrouter)
//LISTEN
app.listen(process.env.PORT, () => {
console.log(`Server is running on port ${process.env.PORT}`)
});