-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_session.js
77 lines (70 loc) · 1.69 KB
/
app_session.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
var express = require('express');
var session = require('express-session');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: false}));
app.use(session({
secret: '1sad%@f1s5d@%1fs3d13s%d1',
resave: false,
saveUninitialized: true
}));
app.get('/auth/logout', function(req, res){
delete req.session.displayName;
res.redirect('/welcome');
});
app.get('/welcome' , function(req, res){
if(req.session.displayName){
res.send(`
<h1>Hello, ${req.session.displayName}</h1>
<a href="/auth/logout">logout</a>
`);
} else {
res.send(`
<h1>Welcome</h1>
<a href="/auth/login">Login</a>
`)
}
})
app.post('/auth/login', function(req, res){
var user = {
username:'egoing',
password:'111',
displayName:'Egoing'
};
var uname = req.body.username;
var pwd = req.body.password;
if(uname === user.username && pwd === user.password){
req.session.displayName = user.displayName;
res.redirect('/welcome');
} else {
res.send('Who are you? <a href="/auth/login">login</a>');
}
});
app.get('/auth/login', function(req, res){
var output = `
<h1>Login</h1>
<form action="/auth/login" method="post">
<p>
<input type="text" name="username" placeholder="username">
</p>
<p>
<input type="password" name="password" placeholder="password">
</p>
<p>
<input type="submit">
</p>
</form>
`;
res.send(output);
});
app.get('/count', function(req, res){
if(req.session.count){
req.session.count++;
} else {
req.session.count = 1;
}
res.send('count :'+req.session.count);
})
app.listen(3003, function(){
console.log('Connected 3003 port !!!');
});