Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f9ce12c

Browse files
prep
1 parent ecd4cba commit f9ce12c

File tree

2 files changed

+96
-6
lines changed

2 files changed

+96
-6
lines changed

week3/prep-exercise/server/app.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import express from 'express';
22
// TODO Use below import statement for importing middlewares from users.js for your routes
3-
// TODO import { ....... } from "./users.js";
3+
import { login, logout, getProfile, register } from './users.js';
44

55
let app = express();
66

77
app.use(express.json());
8-
// TODO: Create routes here, e.g. app.post("/register", .......)
8+
app.use(express.urlencoded({ extended: false }));
9+
10+
app.post('/auth/register', register);
11+
12+
app.post('/auth/login', login);
13+
14+
app.get('/auth/profile', getProfile);
15+
16+
app.post('/auth/logout', logout);
917

1018
// Serve the front-end application from the `client` folder
1119
app.use(express.static('client'));

week3/prep-exercise/server/users.js

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,94 @@
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;
27

38
// Change this boolean to true if you wish to keep your
49
// 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';
713

814
// 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+
};
1092

1193
// You can also create helper functions in this file to help you implement logic
1294
// inside middlewares

0 commit comments

Comments
 (0)