-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
58 lines (52 loc) · 1.96 KB
/
Copy pathapp.js
File metadata and controls
58 lines (52 loc) · 1.96 KB
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
const express = require('express')
const bodyParser = require('body-parser')
const {check, validationResult, body} = require('express-validator')
const fs = require('fs')
const app = express()
const port = process.env.PORT || 5000
const cookieParser = require('cookie-parser');
const crypto = require('crypto')
const urlEncodedParser = bodyParser.urlencoded({extended:false})
app.use(cookieParser('MY SECRET'));
module.exports = crypto
app.set('view engine', 'ejs')
let options = {
maxAge: 1000 * 60 * 15,
httpOnly: true,
signed: true
}
app.get('', (req, res) => {
randomID = crypto.randomUUID()
cookieName = crypto.randomUUID()
res.cookie(cookieName, randomID, options,{ signed : true });
res.render('index')
})
app.get('/register', (req, res)=> {
randomID = crypto.randomUUID()
cookieName = crypto.randomUUID()
res.cookie(cookieName, randomID, options,{ signed : true });
res.render('register')
})
app.post('/register', urlEncodedParser,
[
check('username', 'This username must be 3+ characters long').exists().isLength(({min : 3})),
check('email', 'Email is not valid').isEmail().normalizeEmail(),
check('password', 'Your password require an uppercase Letter, a special character and a number.').isStrongPassword(),
],
(req, res)=> {
const errors = validationResult(req)
if (!errors.isEmpty()) {
const alert = errors.array()
res.render('register', {
alert
})
}
else {
let name = req.body.username
let email = req.body.email
let password = req.body.password
let data = `Username : ${name.concat(" Email: ", email, " Password: ", password)}`;
fs.writeFile('accounts.txt', data+"\n", { flag: 'a+' },(err) => { if (err) throw err; });
}
})
app.listen(port, () => console.info(`App listening on port: ${port}`))