-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathserver.js
130 lines (105 loc) · 4.25 KB
/
server.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
123
124
125
126
127
128
129
130
/**
* @summary server use passport express mysql sequelize socket.io
*/
const express = require('express');
const app = express();
const bp = require('body-parser');
const passport = require('passport');
const async = require('async');
const session = require('express-session');
const { user } = require('./db');
const Investment = require('./db').investment;
const spending = require('./db').spendings;
const { reminder } = require('./db');
const response = require('./utils-module/response');
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
// Use the GoogleStrategy within Passport.
// Strategies in Passport require a `verify` function, which accept
// credentials (in this case, an accessToken, refreshToken, and Google
// profile), and invoke a callback with a user object.
passport.use(new GoogleStrategy({
clientID: 'CLIENT_ID',
clientSecret: 'CLIENT_SECRET',
callbackURL: "http://localhost:3100/google/callback"
},
function (accessToken, refreshToken, profile, done) {
user.findOne({ 'email': profile.email }).then(user => {
if (!user) {
let newUser = {
username: profile.displayName,
email: profile.emails[0].value,
};
// Creating a new user if not exist
user.create(newUser).then(user => {
return done(null, user);
}).catch(error => {
return done(error, null);
});
}
else {
return done(null, user);
}
});
}
));
// To locate the config folder by default it searches for .env here it is .env.example
// Sample ENV properties are loaded here in reallife scenario this would be populated by the environment property of machine running the app server
require('dotenv').config({ path: `${process.cwd()}/.env.example` });
app.use('/', express.static(`${__dirname}/Public_static`));
app.use(bp.urlencoded({ extended: true }));
app.use(bp.json());
app.use(
session({ secret: 'keyboard cat', resave: true, saveUninitialized: true })
); // session secret
// Passport Authentication Implementation
require('./config/passport.js');
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
// Sign up Implementation
app.post('/signup', (req, res, next) => {
passport.authenticate('local-signup', function (err, user, info) {
if (err) {
return response.responseWriter(res, 500, info);
}
if (!user) {
return response.responseWriter(res, 400, info);
}
return response.responseWriter(res, 200, user);
})(req, res, next);
});
// Login value is sent to
app.post('/login', (req, res, next) => {
passport.authenticate('local-signin', function (err, user, info) {
if (err) {
return response.responseWriter(res, 500, info);
}
if (!user) {
return response.responseWriter(res, 400, info);
}
return response.responseWriter(res, 200, user);
})(req, res, next);
});
// GET /auth/google
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in Google authentication will involve
// redirecting the user to google.com. After authorization, Google
// will redirect the user back to this application at /auth/google/callback
app.get('/google', (req, res, next) => {
passport.authenticate('google', { scope: ['profile', 'email'] })(req, res, next);
});
// GET /auth/google/callback
// Use passport.authenticate() as route middleware to authenticate the
// request. If authentication fails, the user will be redirected back to the
// login page. Otherwise, the primary route function function will be called,
// which, in this example, will redirect the user to the home page.
app.get('/google/callback', (req, res, next) => {
passport.authenticate('google', { failureRedirect: '/login' })(req, res, next)
},
function (req, res) {
// Redirecting to /main URL after successful lgon
res.redirect('http://localhost:3100/main.html');
});
app.use('/', require('./routes/index'));
app.listen(3100, function () {
console.log('Server started on http://localhost:3100');
});