-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
126 lines (104 loc) · 2.87 KB
/
index.ts
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
// import dependencies
import express, { Express, Request, Response, NextFunction } from 'express';
import { connectDB } from './database/connection'
import { config } from "dotenv"
import passport from "passport"
import cors from 'cors';
import session from 'express-session'
import { githubAuth, googleAuth } from './utils/oauth'
import User from './model/User';
import { IMongoDBUser } from './types/types';
// read environment variables
config({path: './.env'})
// initializing our express app
const app: Express = express();
// connect to Database
connectDB()
//options for cors midddleware
const options: cors.CorsOptions = {
allowedHeaders: [
'Origin',
'X-Requested-With',
'Content-Type',
'Accept',
'X-Access-Token'
],
credentials: true,
methods: 'GET,HEAD,OPTIONS,PUT,PATCH,POST,DELETE',
origin: 'https://oauth-frontend.netlify.app' ,
preflightContinue: false,
};
// app middlewares
app.use(express.json())
app.use(cors(options))
app.set("trust proxy", 1)
app.use(
session({
secret: process.env.SESSION_SECRET_KEY || 'John is a cat Man' ,
resave: true,
saveUninitialized: true,
cookie: {
sameSite: "none",
secure: true,
maxAge: 1000 * 60 *60 * 24 * 7
}
})
)
app.use(passport.initialize())
app.use(passport.session())
// serialize user
passport.serializeUser((user: any, done: any) => {
return done(null, user._id)
})
// deserialize user
passport.deserializeUser((id: string, done: any) => {
User.findById(id, (err: Error , doc: IMongoDBUser ) => {
return done(null, doc);
})
})
// user routes
app.get('/', (req: Request, res: Response) => {
res.send('Hello World');
});
app.get('/getuser', (req: Request, res: Response) => {
res.send(req.user)
})
app.get('/auth/logout', (req: Request, res: Response, next: NextFunction) => {
if (req.user) {
req.logout(function(err) {
if (err) {
return next(err); }
})
res.send('Done')
}
})
// google authenciation function
googleAuth()
// github routes
app.get('/auth/google',
passport.authenticate('google', { scope: ['profile'] })
);
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect( 'https://oauth-frontend.netlify.app' );
});
// github authenciation function
githubAuth()
// github routes
app.get('/auth/github',
passport.authenticate('github')
);
app.get('/auth/github/callback',
passport.authenticate('github', { failureRedirect: 'https://oauth-frontend.netlify.app/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect( 'https://oauth-frontend.netlify.app' );
}
);
// Server setup
const port = process.env.PORT || 4000;
app.listen(port, () => {
console.log(`⚡️ [server]: Server is running at port ${port}....`);
})