Skip to content
This repository was archived by the owner on Mar 22, 2024. It is now read-only.

Commit 33d6717

Browse files
author
KB885
committed
Auth-ESLint-Routes-More
1 parent 010eccd commit 33d6717

File tree

14 files changed

+229
-203
lines changed

14 files changed

+229
-203
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
"sourceType": "module"
1212
},
1313
"rules": {
14+
"indent": ["error", 4]
1415
}
1516
}

src/app/Http/Admin/AdminController.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class AdminController {
2+
/**
3+
* Show the admin dashboard
4+
* @param {*} req
5+
* @param {*} res
6+
*/
7+
async showBoard (req, res) {
8+
res.render('admin/board')
9+
};
10+
}
11+
12+
module.exports = new AdminController()
Lines changed: 134 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,137 @@
11
const User = require('../Models/User')
22
const bcrypt = require('bcrypt')
3-
// On any request req, req.session.user is now available where the userSession can be accesed
4-
// by User = require("../Models/User"); and then User.findOne to fetch the user from the database
5-
6-
/**
7-
* Show the login page and check if the user is already logged in
8-
* @param {*} req
9-
* @param {*} res
10-
*/
11-
exports.login = (req, res) => {
12-
if(req.session.user) {
13-
res.redirect("/dashboard");
14-
} else {
15-
res.render("auth/login");
16-
}
17-
};
18-
19-
20-
21-
/**
22-
* Show the register page and check if the user is already logged in
23-
* @param {*} req
24-
* @param {*} res
25-
*/
26-
exports.register = (req, res) => {
27-
if(req.session.user) {
28-
res.redirect("/dashboard");
29-
} else {
30-
res.render("auth/register");
31-
}
32-
};
33-
34-
35-
/**
36-
* Show the forgot password page
37-
* @param {*} req
38-
* @param {*} res
39-
*/
40-
exports.forgot = (req, res) => {
41-
if(req.session.user) {
42-
res.redirect("/dashboard");
43-
} else {
44-
res.render("auth/forgot");
45-
}
46-
};
47-
48-
49-
/**
50-
* Authenticate the user and redirect to the dashboard
51-
* @param {*} req
52-
* @param {*} res
53-
* @returns
54-
*/
55-
exports.authenticate = async (req, res) => {
56-
const { username, password } = req.body;
57-
58-
if (!username || !password) return res.status(400).send("Please fill all the fields");
59-
60-
User
61-
.findOne({ username })
62-
.then((user) => {
63-
if (!user) return res.status(400).send("User does not exist");
64-
65-
bcrypt
66-
.compare(password, user.password)
67-
.then((isMatch) => {
68-
if (isMatch) {
69-
req.session.user = user;
70-
res.redirect("/project");
71-
} else {
72-
return res.status(400).send("Incorrect password");
73-
}
3+
4+
class AuthenticationController {
5+
/**
6+
* Show the login page and check if the user is already logged in
7+
* @param {*} req
8+
* @param {*} res
9+
*/
10+
async showLogin (req, res) {
11+
if (req.session.user) {
12+
res.redirect('/dashboard')
13+
} else {
14+
res.render('auth/login')
15+
}
16+
};
17+
18+
/**
19+
* Show the register page and check if the user is already logged in
20+
* @param {*} req
21+
* @param {*} res
22+
*/
23+
async showRegister (req, res) {
24+
if (req.session.user) {
25+
res.redirect('/dashboard')
26+
} else {
27+
res.render('auth/register')
28+
}
29+
};
30+
31+
/**
32+
* Show the forgot password page and check if the user is already logged in
33+
* @param {*} req
34+
* @param {*} res
35+
*/
36+
async showForgot (req, res) {
37+
if (req.session.user) {
38+
res.redirect('/dashboard')
39+
} else {
40+
res.render('auth/forgot')
41+
}
42+
};
43+
44+
/**
45+
* Authenticate the user and redirect to the dashboard
46+
* @param {*} req
47+
* @param {*} res
48+
* @returns
49+
*/
50+
async authenticate (req, res) {
51+
const { username, password } = req.body
52+
53+
if (!username || !password) return res.status(400).send('Please fill all the fields')
54+
55+
User
56+
.findOne({
57+
username
58+
})
59+
.then((user) => {
60+
if (!user) return res.status(400).send('User does not exist')
61+
62+
bcrypt
63+
.compare(password, user.password)
64+
.then((isMatch) => {
65+
if (isMatch) {
66+
req.session.user = user
67+
res.redirect('/project')
68+
} else {
69+
return res.status(400).send('Incorrect password')
70+
}
71+
})
72+
.catch((err) => console.log(err))
73+
})
74+
};
75+
76+
/**
77+
* Create a new user and redirect to the dashboard
78+
* @param {*} req
79+
* @param {*} res
80+
* @returns
81+
*/
82+
async signup (req, res) {
83+
const {
84+
name,
85+
username,
86+
email,
87+
password,
88+
passwordConfirmation
89+
} = req.body
90+
91+
if (!name || !username || !email || !password || !passwordConfirmation) { return res.status(400).send('Please fill all the fields') }
92+
93+
if (password !== passwordConfirmation) { return res.status(400).send('Passwords do not match') }
94+
95+
if (password.length < 8) { return res.status(400).send('Password must be at least 6 characters') }
96+
97+
User.findOne({
98+
username
99+
}).then((user) => {
100+
if (user) return res.status(400).send('User already exists')
101+
102+
const newUser = new User({ name, username, email, password })
103+
104+
bcrypt.genSalt(10, (salt) => {
105+
bcrypt.hash(newUser.password, salt, (err, hash) => {
106+
if (err) console.log(err)
107+
newUser.password = hash
108+
newUser
109+
.save()
110+
.then((user) => {
111+
// req.session.user = user;
112+
// res.json(user);
113+
res.redirect('login')
114+
})
115+
.catch((err) => console.log(err))
116+
})
117+
})
74118
})
75-
.catch((err) => console.log(err));
76-
});
77-
};
78-
79-
/**
80-
* Create a new user and redirect to the dashboard
81-
* @param {*} req
82-
* @param {*} res
83-
* @returns
84-
*/
85-
exports.signup = (req, res) => {
86-
const { name, username, email, password, passwordConfirmation } = req.body;
87-
88-
if (!name || !username || !email || !password || !passwordConfirmation)
89-
return res.status(400).send("Please fill all the fields");
90-
91-
if (password !== passwordConfirmation)
92-
return res.status(400).send("Passwords do not match");
93-
94-
if (password.length < 8)
95-
return res.status(400).send("Password must be at least 6 characters");
96-
97-
User.findOne({username}).then((user) => {
98-
if (user) return res.status(400).send("User already exists");
99-
100-
const newUser = new User({
101-
name,
102-
username,
103-
email,
104-
password,
105-
});
106-
107-
bcrypt.genSalt(10, (err, salt) => {
108-
bcrypt.hash(newUser.password, salt, (err, hash) => {
109-
if (err) console.log(err);
110-
newUser.password = hash;
111-
newUser
112-
.save()
113-
.then((user) => {
114-
// req.session.user = user;
115-
// res.json(user);
116-
res.redirect("login");
117-
})
118-
.catch((err) => console.log(err));
119-
});
120-
});
121-
});
122-
};
123-
124-
/**
125-
* Logout the user and redirect to the login page
126-
* @param {*} req
127-
* @param {*} res
128-
*/
129-
exports.logout = (req, res) => {
130-
if(req.session) {
131-
req.session.destroy(() => {
132-
res.redirect("/login");
133-
});
134-
} else {
135-
res.redirect("/login");
136-
}
137-
};
119+
};
120+
121+
/**
122+
* Logout the user and redirect to the login page
123+
* @param {*} req
124+
* @param {*} res
125+
*/
126+
async logout (req, res) {
127+
if (req.session) {
128+
req.session.destroy(() => {
129+
res.redirect('/login')
130+
})
131+
} else {
132+
res.redirect('/login')
133+
}
134+
};
135+
}
136+
137+
module.exports = new AuthenticationController()

src/app/Middleware/Authorization.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Authorization {
2+
/**
3+
* Login required middleware
4+
*/
5+
async authLogin (req, res, next) {
6+
if (!req.session || !req.session.user) {
7+
return res.redirect('/login')
8+
}
9+
await next()
10+
}
11+
12+
/**
13+
* Role required middleware
14+
*/
15+
async authRole (req, res, next) {
16+
if (req.user.role !== role) {
17+
return res.send('Not allowed')
18+
}
19+
await next()
20+
}
21+
}
22+
23+
module.exports = new Authorization()

src/app/Middleware/Role.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const ROLE = {
2+
ADMIN: 'admin',
3+
USER: 'user'
4+
}
5+
6+
module.exports = ROLE

src/resources/views/admin/board.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
admin board
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
header
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mid
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
right
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sidebar

0 commit comments

Comments
 (0)