|
1 |
| -import newDatabase from './database.js' |
| 1 | +import newDatabase from './database.js'; |
| 2 | +import jwt from 'jsonwebtoken'; |
| 3 | +import bcrypt from 'bcrypt'; |
| 4 | +import { v4 as uuidv4 } from 'uuid'; |
| 5 | + |
| 6 | +const saltRounds = 10; |
2 | 7 |
|
3 | 8 | // Change this boolean to true if you wish to keep your
|
4 | 9 | // users between restart of your application
|
5 |
| -const isPersistent = false |
6 |
| -const database = newDatabase({isPersistent}) |
| 10 | +const isPersistent = false; |
| 11 | +const database = newDatabase({ isPersistent }); |
| 12 | +const SECRET_KEY = 'ke%81t4klvbnlrkenbmcrk44regrtg455'; |
7 | 13 |
|
8 | 14 | // Create middlewares required for routes defined in app.js
|
9 |
| -// export const register = async (req, res) => {}; |
| 15 | +export const register = async (req, res) => { |
| 16 | + const userName = req.body.name; |
| 17 | + const password = req.body.password; |
| 18 | + |
| 19 | + if (!userName || !password) { |
| 20 | + return res |
| 21 | + .status(401) |
| 22 | + .json({ message: 'userName and password is required' }) |
| 23 | + .end(); |
| 24 | + } |
| 25 | + |
| 26 | + try { |
| 27 | + const hashedPassword = await bcrypt.hash(password, saltRounds); |
| 28 | + const newUser = { |
| 29 | + name: userName, |
| 30 | + password: hashedPassword |
| 31 | + }; |
| 32 | + const storedObj = database.create(newUser); |
| 33 | + |
| 34 | + res.status(201).json({ id: uuidv4(), name: newUser.name }); |
| 35 | + } catch (error) { |
| 36 | + res.status(500).json({ message: 'Something went wrong with server!' }); |
| 37 | + } |
| 38 | + |
| 39 | + return; |
| 40 | +}; |
| 41 | + |
| 42 | +export const login = async (req, res) => { |
| 43 | + const userName = req.body.name; |
| 44 | + const password = req.body.password; |
| 45 | + if (!userName || !password) { |
| 46 | + return res |
| 47 | + .status(401) |
| 48 | + .json({ message: 'userName and password is required' }) |
| 49 | + .end(); |
| 50 | + } |
| 51 | + |
| 52 | + const allUser = database.getAll(); |
| 53 | + const user = allUser.find((user) => user.userName === userName); |
| 54 | + if (!user) { |
| 55 | + res.status(404).send('user not found'); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + try { |
| 60 | + const correctPassword = await bcrypt.compare(password, user.password); |
| 61 | + if (correctPassword) { |
| 62 | + const token = jwt.sign({ id: user.id }, SECRET_KEY, { expiresIn: '30m' }); |
| 63 | + res.status(200).send({ message: 'you login ', token }); |
| 64 | + } |
| 65 | + } catch (error) { |
| 66 | + res.status(500).json({ message: 'Something went wrong with server!' }); |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +export const getProfile = async (req, res) => { |
| 71 | + const tokenJWT = req.headers.authorization.split(' ')[1]; |
| 72 | + if (!tokenJWT) { |
| 73 | + res.status(404).json({ message: 'token lost' }); |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + const decodeUser = jwt.verify(tokenJWT, SECRET_KEY); |
| 78 | + |
| 79 | + if (!user) { |
| 80 | + res.status(403).json({ message: 'something wrong with token!' }); |
| 81 | + } |
| 82 | + const user = database.getById(decodeUser.id); |
| 83 | + res.status(201).json({ message: `this is user with name ${user.name}` }); |
| 84 | + } catch (error) { |
| 85 | + res.status(500).json({ message: 'Something went wrong with server!' }); |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +export const logout = async (req, res) => { |
| 90 | + res.status(200).send('you are logout!'); |
| 91 | +}; |
10 | 92 |
|
11 | 93 | // You can also create helper functions in this file to help you implement logic
|
12 | 94 | // inside middlewares
|
0 commit comments