-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
066e144
commit 34ce9ac
Showing
19 changed files
with
770 additions
and
80 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { validationParser } from '../../utils/validation'; | ||
import { loginReqSchema, registerReqSchema } from './validations'; | ||
import authService from './service'; | ||
|
||
const authController = { | ||
login: async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const { body } = await validationParser(loginReqSchema, req); | ||
|
||
const user = await authService.login(body.email, body.password); | ||
|
||
req.session.regenerate(function (err) { | ||
if (err) next(err); | ||
|
||
req.session.user = user; | ||
|
||
req.session.save(function (err) { | ||
if (err) return next(err); | ||
console.log(req.session); | ||
res.json({ message: 'Login Successful' }); | ||
}); | ||
}); | ||
} catch (e) { | ||
next(e); | ||
} | ||
}, | ||
|
||
register: async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const { body } = await validationParser(registerReqSchema, req); | ||
await authService.register( | ||
body.email, | ||
body.password, | ||
body.firstName, | ||
body.lastName, | ||
body.phone, | ||
); | ||
return res.status(201).json({ message: 'User registration successful' }); | ||
} catch (e) { | ||
next(e); | ||
} | ||
}, | ||
|
||
logout: async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
req.session.user = null; | ||
req.session.save((err) => { | ||
if (err) return next(err); | ||
|
||
req.session.regenerate((err) => { | ||
if (err) return next(err); | ||
|
||
res.json({ message: 'User was successfully logged out' }); | ||
}); | ||
}); | ||
} catch (e) { | ||
next(e); | ||
} | ||
}, | ||
}; | ||
|
||
export default authController |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Router, Request, Response, NextFunction } from 'express'; | ||
import authController from './controller'; | ||
|
||
const authRouter = Router(); | ||
|
||
authRouter.post('/login', authController.login); | ||
authRouter.post('/register', authController.register); | ||
authRouter.get('/logout', authController.logout); | ||
|
||
export default authRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ConflictError, UnauthorizedError } from '../..//utils/errors'; | ||
import { prisma } from '../prisma'; | ||
import * as bcrypt from 'bcrypt'; | ||
import { AuthInfo } from 'express-session'; | ||
|
||
const authService = { | ||
/** | ||
* Takes in an email and password and checks if the credentials match any user | ||
* in the database. If successful the user auth info is returned else an Unauthorized error | ||
* is thrown. | ||
* | ||
* @param email - user's email | ||
* @param password - user's password | ||
* @returns | ||
*/ | ||
login: async (email: string, password: string): Promise<AuthInfo> => { | ||
const user = await prisma.user.findUnique({ where: { email: email } }); | ||
|
||
if (!user || !(await bcrypt.compare(password, user.passwordHash))) { | ||
throw new UnauthorizedError('Invalid email or password'); | ||
} | ||
|
||
return { id: user.id }; | ||
}, | ||
|
||
register: async(email:string, password:string, firstname:string, lastname:string, phone?: string): Promise<AuthInfo>=>{ | ||
const existingUser = await prisma.user.findUnique({where:{email:email}}) | ||
|
||
if (existingUser){ | ||
throw new ConflictError("User already exists") | ||
} | ||
const passwordHash = await bcrypt.hash(password, 10); | ||
const newUser = await prisma.user.create({data:{email:email, firstName: firstname, lastName: lastname, phone: phone, passwordHash: passwordHash }}) | ||
return {id: newUser.id} | ||
} | ||
}; | ||
|
||
export default authService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { z } from 'zod'; | ||
|
||
export const loginReqSchema = z.object({ | ||
body: z.object({ | ||
email: z.string(), | ||
password: z.string(), | ||
}), | ||
}); | ||
|
||
export const registerReqSchema = z.object({ | ||
body: z.object({ | ||
firstName: z.string().optional(), | ||
lastName: z.string().optional(), | ||
email: z.string().email(), | ||
phone: z.string().optional(), | ||
password: z.string(), | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.