Skip to content

Commit

Permalink
omae wa mo shindeiru
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsAnkan committed Jul 19, 2023
1 parent b3a1892 commit 3829d58
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions database/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ async function getUserFromSessionID (sessionId) {
}

async function createSession (userId) {
// 3524: The Goose is Dead
const sessionId = [3, 5, 2, 4].map(i => (Math.random() + 1).toString(36).substring(2, 2 + i)).join('-');
// 3374: You Are Already Dead
const sessionId = [3, 3, 7, 4].map(i => (Math.random() + 1).toString(36).substring(2, 2 + i)).join('-');
const session = new Session({
userID: userId,
sessionID: sessionId
Expand Down
19 changes: 16 additions & 3 deletions routers/auth-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ router.post('/login', [
.notEmpty().withMessage('No Username Provided')
.trim(),
body('password')
.notEmpty().withMessage('No Username Provided')
.notEmpty().withMessage('No Password Provided')
.trim()
], async (req, res) => {
if (req.loggedIn) return res.error('You shall not pass');
const errors = validationResult(req);
if (!errors.isEmpty()) {
const errorMessages = errors.array().map(error => error.msg);
throw new Error(errorMessages[0]);
}
const userData = req.body;
const sessionID = await dbh.createSession(await dbh.validateUser(userData));
res.cookie('sessionID', sessionID);
Expand All @@ -40,7 +46,8 @@ router.post('/signup', [
.notEmpty().withMessage('No Name Provided'),
body('roll')
.trim()
.notEmpty().withMessage('No Roll Provided'),
.notEmpty().withMessage('No Roll Provided')
.matches(/^[12][890123][A-Z]{2}[0-9][A-Z0-9]{2}\d\d$/i).withMessage('Please provide a valid roll number'),
body('phone')
.trim()
.notEmpty().withMessage('No Phone Number Provided')
Expand All @@ -59,6 +66,12 @@ router.post('/signup', [
.isLength({ min: 6, max: 32 }).withMessage('Password must be between 6 and 32 characters long.')
.matches(/^\S+$/).withMessage('Password cannot contain whitespaces')
], async (req, res) => {
if (req.loggedIn) return res.error('How are you signing up when you are already logged in, what is this power !');
const errors = validationResult(req);
if (!errors.isEmpty()) {
const errorMessages = errors.array().map(error => error.msg);
throw new Error(errorMessages[0]);
}
await dbh.createUser(req.body);
res.redirect('/');
});
Expand All @@ -67,7 +80,7 @@ app.post('/logout', async (req, res, next) => {
// If we may require them, then...
// const { sessionId } = req.cookies;
// await db.removeSession(sessionId);
if (req.loggedIn) return res.error('Stop trying to break the website ;-;');
if (!req.loggedIn) return res.error('Stop trying to break the website ;-;');
await res.clearCookie('sessionID');
return res.send('Signed out successfully. Mata ne.');
});
Expand Down

1 comment on commit 3829d58

@Goose-Of-War
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

N- nani? 💀

Please sign in to comment.