-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
63 lines (48 loc) · 1.5 KB
/
app.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
const express = require('express');
const jwt = require('jsonwebtoken');
const verifytoken = require('./verifytoken');
const secretKey = require('./secret');
const port = process.env.PORT || 6000;
const app = express();
app.use(express.json());
app.post('/', async (req,res) =>{
try {
const email = req.body.email,
password = req.body.password;
// signToken(email, password, res);
jwt.sign({ email, password }, secretKey, {expiresIn: "2 days"}, (err, token) => {
if (err) {
console.log(`Oops! something went wrong trying to sign the payload, ${err}`);
}
// console.log(token);
res.json({ token });
return token;
});
} catch (err) {
console.error(err.message);
}
});
app.post('/login',verifytoken, async(req,res)=>{
try {
jwt.verify(req.token, secretKey,(err, decoded)=>{
if(err){
console.log(`Oops! There's an error verifying the token, ${err}`);
res.sendStatus(403);
}
res.json({
"decoded":decoded,
"permission": "Granted..."});
});
} catch (err) {
console.error(err.message);
}
});
app.get('/secured',(req,res)=>{
res.send('Welcome to the secured page...');
});
app.listen(port, (err)=>{
if(err){
console.log("There was an error starting the server.", error);
}
console.log(`Server running on port: ${port}...`);
});