-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
122 lines (99 loc) · 3.02 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
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
require('dotenv').config();
const express = require('express');
const app = express();
const bodyParser = require('body-parser')
const morgan = require('morgan')
const ejs = require('ejs')
const mongoose = require('mongoose')
const passport = require('passport')
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const session = require('express-session')
app.use(express.static('public'));
app.use(morgan('dev'));
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.set('view engine', 'ejs')
function getTime(mon,day,hr){
return (((mon*30)+day)*24+hr) * 3600 * 1000;
}
app.use(session({
secret: 'my name is manish',
resave: false,
saveUninitialized: false,
cookie: {
maxAge: getTime(1,0,0)
}
}))
app.use(passport.initialize())
app.use(passport.session())
const User = require('./api/models/user');
passport.use(User.createStrategy())
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "/auth/google/budgety"
},
function(accessToken, refreshToken, profile, cb) {
// console.log(profile)
let object = {
googleId: profile.id,
name: profile.displayName,
imgUrl: profile.photos[0].value,
username: profile.emails[0].value
}
User.findOrCreate(object, function (err, user) {
return cb(err, user);
});
}
));
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
app.get('/auth/google',
passport.authenticate('google', { scope: ['profile', 'email'] }));
app.get('/auth/google/budgety',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
});
mongoose.connect(`mongodb+srv://manish:${process.env.MONGO_ATLAS_PASSWORD}@cluster0-rp3y8.mongodb.net/budgetyDB`, {useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: true});
const loginRoutes = require('./api/routes/login')
const signupRoutes = require('./api/routes/signup')
const dataRoutes = require('./api/routes/data')
app.use('/login', loginRoutes)
app.use('/signup', signupRoutes)
app.use('/data', dataRoutes)
const checkAuth = (req, res, next) => {
if(req.isAuthenticated()){
next();
}
else{
res.redirect('/login')
}
}
app.get('/', checkAuth, (req, res) => {
console.log(req.user);
const options = {
title: 'Home',
css: 'styles',
user: {
name: req.user.name,
email: req.user.username,
imgUrl: req.user.imgUrl
}
}
res.render('index', options)
})
app.get('/logout', checkAuth, (req, res) => {
req.logOut();
res.redirect('/login')
})
app.listen(process.env.PORT || 3000, () => {
console.log(`server is running at localhost:${process.env.PORT | 3000}`);
});