forked from CodingForProduct/tap_rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
142 lines (125 loc) · 4.16 KB
/
routes.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
131
132
133
134
135
136
137
138
139
140
141
142
var router = require('express').Router();
var low = require('lowdb');
var path = require('path');
var uuid = require('uuid');
var authService = require('./services/authService');
var passport = require('passport');
authService.configurePassport(passport)
// connect to database
// path.join will take the parameters and create a path using the
// right type of slashes (\ vs /) based on the operatin system
var db = low(path.join('data', 'riderData.json'));
//==========================
// root route
//==========================
// display - landing page
router.get('/', function (req, res) {
res.render('welcome');
});
//==========================
// reward routes
//==========================
// display dashboard - with rider data and rewards
router.get('/dashboard', function (req, res) {
// pass data to template
var rider = db.get('riders').find({ username: 'Alice' }).value()
var reward = db.get('rewards').value()
res.render('dashboard', {rider: rider, reward: reward })
});
// Select reward from form, update balance
// Redirect to redeem page to display redemption and new balance message
router.post('/dashboard', function(req, res) {
console.log(req.body)
// get data from form/dashboard page
var pointsMinus = req.body.pointsRequired;
var currentBalance = req.body.pointBalance;
var id = req.body.pointID;
//update user balance
db.get('riders')
.find({ username: 'Alice' })
.assign({ pointBalance: currentBalance - pointsMinus })
.write()
// redirect
res.redirect('redeem' + '/' + id)
});
// display one reward on redeem page, using ":id"
router.get('/redeem/:pointID', function(req, res) {
var rider = db.get('riders').find({ username: 'Alice' }).value()
var reward = db.get('rewards').find({ pointID: req.params.pointID }).value()
res.render('redeem', { reward: reward || {}, rider: rider || {},})
})
//==========================
// auth routes
//==========================
var signup_view_path = path.join('auth', 'signup');
var login_view_path = path.join('auth', 'login');
// var logout_view_path = path.join('auth', 'logout');
// display signup page
// router.get('/signup', function (req, res) {
// res.render(signup_view_path, { errors: [] })
// });
router.get('/signup', function(req, res) {
res.render(signup_view_path)
})
// create user
router.post('/signup', function(req, res) {
// remove extra spaces
var username = req.body.username.trim();
var password = req.body.password.trim();
var password2 = req.body.password2.trim();
var tapCard = req.body.tapCard.trim();
var email = req.body.email.trim();
// console.log(username)
// validate form data
req.checkBody('username', 'Username must have at least 3 characters').isLength({min: 3});
req.checkBody('password', 'Password must have at least 3 characters').isLength({min: 3});
req.checkBody('username', 'Username is required').notEmpty();
req.checkBody('password', 'Password is required').notEmpty();
req.checkBody('password2', 'Confirm password is required').notEmpty();
req.checkBody('password', 'Password do not match').equals(password2);
// check for errors
var errors = req.validationErrors();
// if there are errors, display signup page
if (errors) {
return res.render(signup_view_path, {errors: errors.map(function(error) {return error.msg})})
}
var options = {
username: username,
password: password,
tapCard: tapCard,
email: email,
successRedirectUrl: '/dashboard',
signUpTemplate: signup_view_path,
}
authService.signup(options,res);
})
// display login page
router.get('/login', function (req, res) {
// res.render('login');
res.render(login_view_path, { errors: [] })
});
// peform login
router.post(
'/login',
passport.authenticate(
'local',
{
successRedirect:'/dashboard',
failureRedirect:'/login',
failureFlash: true,
successFlash: 'You are logged in',
}
)
)
// display logout
router.get('/logout', function(req, res) {
req.logout();
req.flash('success', 'You are logged out');
res.redirect('/')
// res.render(logout_view_path)
})
// display password reset page
router.get('/resetpw', function (req, res) {
res.render('resetpw');
});
module.exports = router;