Skip to content

Commit ed0bae2

Browse files
committed
Add controller
1 parent c4dfd74 commit ed0bae2

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

controllers/main.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const jwt = require('jsonwebtoken')
2+
const { BadRequestError } = require('../errors')
3+
4+
const login = async (req, res) => {
5+
const { username, password } = req.body
6+
7+
if (!username || !password) {
8+
throw new BadRequestError('Please provide email and password')
9+
}
10+
11+
const id = new Date().getDate()
12+
13+
const token = jwt.sign({ id, username }, process.env.JWT_SECRET, {
14+
expiresIn: '30d',
15+
})
16+
17+
res.status(200).json({ msg: 'user created', token })
18+
}
19+
20+
const dashboard = async (req, res) => {
21+
const luckyNumber = Math.floor(Math.random() * 100)
22+
23+
res.status(200).json({
24+
msg: `Hello, ${req.user.username}`,
25+
secret: `Here is your authorized data, your lucky number is ${luckyNumber}`,
26+
})
27+
}
28+
29+
module.exports = {
30+
login,
31+
dashboard,
32+
}

0 commit comments

Comments
 (0)