forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
112 lines (92 loc) · 2.72 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
const path = require('path')
const minimist = require('minimist')
const morgan = require('morgan')
const bodyParser = require('body-parser')
const session = require('express-session')
const express = require('express')
const app = express()
// get port from passed in args from scripts/start.js
const port = minimist(process.argv.slice(2)).port
const matchesUsernameAndPassword = (body = {}) => {
return body.username === 'jane.lane' && body.password === 'password123'
}
const ensureLoggedIn = (req, res, next) => {
if (req.session.user) {
next()
} else {
res.redirect('/unauthorized')
}
}
// parse JSON bodies
const jsonParser = bodyParser.json()
app.use('/node_modules', express.static(path.join(__dirname, '..', '..', 'node_modules')))
app.use(morgan('dev'))
// store a session cookie called
// 'cypress-session-cookie'
app.use(session({
name: 'cypress-session-cookie',
secret: 'sekret',
resave: false,
saveUninitialized: false,
}))
// app.use((req, res, next) => {
// console.log('session info is', req.session)
// next()
// })
app.set('views', __dirname)
app.set('view engine', 'hbs')
app.get('/', (req, res) => res.redirect('/login'))
// this is the standard HTML login page
app.get('/login', (req, res) => {
res.render('./login.hbs')
})
// specifies that the jsonParser should only be
// used on the one route when its coming from a JSON request
app.post('/login', jsonParser, (req, res) => {
// if this matches the secret username and password
if (matchesUsernameAndPassword(req.body)) {
req.session.user = 'jane.lane'
// respond with how we should redirect
res.json({ redirect: '/dashboard' })
} else {
// else send back JSON error
// with unprocessable entity
// status code
res.status(422).json({
error: 'Username and/or password is incorrect',
})
}
})
app.post('/slow-login', jsonParser, (req, res) => {
// if this matches the secret username and password
if (matchesUsernameAndPassword(req.body)) {
// login the user after a delay
setTimeout(function () {
req.session.user = 'jane.lane'
// respond with how we should redirect
res.json({ redirect: '/dashboard' })
}, 2000)
} else {
// else send back JSON error
// with unprocessable entity
// status code
res.status(422).json({
error: 'Username and/or password is incorrect',
})
}
})
app.get('/dashboard', ensureLoggedIn, (req, res) => {
res.render('./dashboard.hbs', {
user: req.session.user,
})
})
app.get('/users', ensureLoggedIn, (req, res) => {
res.render('./users.hbs')
})
app.get('/admin', ensureLoggedIn, (req, res) => {
res.render('./admin.hbs')
})
app.get('/unauthorized', (req, res) => {
res.render('./unauthorized.hbs')
})
app.listen(port)