-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 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
87
import createHttpError from "http-errors";
import express from "express";
import cookieParser from "cookie-parser";
import session from 'express-session';
import passport from 'passport'
import {Strategy} from 'passport-google-oauth2'
import logger from "morgan";
import dotenv from "dotenv";
import fs from "fs";
dotenv.config();
const refreshTokenProps = {accessType: 'offline',prompt: 'consent'}
passport.use("oauth2-1",new Strategy({
...refreshTokenProps,
clientSecret:process.env.CLIENT_SECRET,
authorizationURL:"https://accounts.google.com/o/oauth2/auth",
tokenURL:"https://accounts.google.com/o/oauth2/token",
clientID:process.env.CLIENT_ID,
callbackURL:`http://localhost:3000${process.env.CALLBACK_URL}`,
scope: ["https://www.googleapis.com/auth/userinfo.email","https://www.googleapis.com/auth/userinfo.profile"]//,"https://www.googleapis.com/auth/calendar","https://www.googleapis.com/auth/calendar.events"]
}
, function (accessToken, refreshToken,params, profile, cb){
console.log("write")
fs.writeFileSync('token.json',JSON.stringify({accessToken,refreshToken,params:JSON.stringify(params),profile:JSON.stringify(profile)}));
return cb(null, profile);
}
));
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
const app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({ secret: 'cats', resave: false, saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());
app.get('/',
passport.authenticate("oauth2-1",{
...refreshTokenProps
}));
app.get(
process.env.CALLBACK_URL,
passport.authenticate("oauth2-1", { failureRedirect: "/auth/google/failure" ,...refreshTokenProps}),
function (req, res) {
console.log("CALLBACK")
res.redirect("/auth/google/success");
}
);
app.get('/auth/google/failure', (req, res) => {
res.send('Failed to authenticate..');
});
app.get('/auth/google/success', (req, res) => {
res.send('Success authentication');
});
app.get('/logout', (req, res) => {
req.logout();
req.session.destroy();
res.send('Goodbye!');
});
app.listen(
3000,
() => console.log(`Server is running on port ${3000}`)
)
export default app;