-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
forgot password and role end-point created
- Loading branch information
Showing
13 changed files
with
203 additions
and
8 deletions.
There are no files selected for viewing
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,16 @@ | ||
import express from 'express'; | ||
import { | ||
loginEmployee, | ||
forgotPassword, | ||
verifyResetPassword, | ||
setNewPassword, | ||
} from '../services/auth-service.js'; | ||
|
||
const router = express.Router(); | ||
|
||
router.post('/login-employee', loginEmployee); | ||
router.post('/forgot-password', forgotPassword); | ||
router.post('/verify-reset-password', verifyResetPassword); | ||
router.post('/set-new-password', setNewPassword); | ||
|
||
export default router; |
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,8 @@ | ||
import express from 'express'; | ||
import { addRole } from '../services/role-service.js'; | ||
|
||
const router = express.Router(); | ||
|
||
router.post('/add-role', addRole); | ||
|
||
export default router; |
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,6 @@ | ||
import loginEmployee from './auth/login-employee.js'; | ||
import forgotPassword from './auth/forgot-password.js'; | ||
import verifyResetPassword from './auth/verify-reset-password.js'; | ||
import setNewPassword from './auth/set-new-password.js'; | ||
|
||
export { loginEmployee, forgotPassword, verifyResetPassword, setNewPassword }; |
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,51 @@ | ||
import { prisma } from '../../database/prisma-config.js'; | ||
import errorResponseHandler from '../../handlers/error-response-handlers.js'; | ||
import responseHandler from '../../handlers/response-handler.js'; | ||
import { sendEmail } from '../../utils/email.js'; | ||
|
||
const forgotPassword = async (req, res) => { | ||
const { employeeEmail } = req.body; | ||
|
||
if (!employeeEmail) { | ||
return errorResponseHandler(res, 400, 'fail', 'Please provide your email'); | ||
} | ||
|
||
const employee = await prisma.employee.findUnique({ | ||
where: { employeeEmail }, | ||
}); | ||
|
||
if (!employee) { | ||
return errorResponseHandler(res, 404, 'fail', 'Employee not found'); | ||
} | ||
|
||
// generate 6 digit random number | ||
const resetCode = Math.floor(100000 + Math.random() * 900000); | ||
|
||
// update employee reset code | ||
await prisma.employee.update({ | ||
where: { employeeEmail }, | ||
data: { | ||
verificationCode: resetCode.toString(), | ||
}, | ||
}); | ||
|
||
// send email | ||
const subject = 'Reset Password'; | ||
const message = `Your reset code is ${resetCode}`; | ||
|
||
try { | ||
await sendEmail({ email: employeeEmail, subject, message }); | ||
} catch (error) { | ||
return errorResponseHandler(res, 500, 'fail', 'Error sending email'); | ||
} | ||
|
||
return responseHandler( | ||
res, | ||
200, | ||
'success', | ||
'Reset code sent to your email', | ||
null | ||
); | ||
}; | ||
|
||
export default forgotPassword; |
2 changes: 1 addition & 1 deletion
2
server/services/employee/login-employee.js → server/services/auth/login-employee.js
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,46 @@ | ||
import { prisma } from '../../database/prisma-config.js'; | ||
import errorResponseHandler from '../../handlers/error-response-handlers.js'; | ||
import responseHandler from '../../handlers/response-handler.js'; | ||
import { hashPassword } from '../../helper/password.js'; | ||
|
||
const setNewPassword = async (req, res) => { | ||
const { employeePassword, employeeEmail } = req.body; | ||
|
||
if (!employeePassword || !employeeEmail) { | ||
return errorResponseHandler( | ||
res, | ||
400, | ||
'fail', | ||
'Please provide all required fields' | ||
); | ||
} | ||
|
||
const employee = await prisma.employee.findUnique({ | ||
where: { employeeEmail }, | ||
}); | ||
|
||
if (!employee) { | ||
return errorResponseHandler(res, 404, 'fail', 'Employee not found'); | ||
} | ||
|
||
// hash password | ||
const hashedPassword = await hashPassword(employeePassword); | ||
|
||
// update employee password | ||
await prisma.employee.update({ | ||
where: { employeeEmail }, | ||
data: { | ||
employeePassword: hashedPassword, | ||
}, | ||
}); | ||
|
||
return responseHandler( | ||
res, | ||
200, | ||
'success', | ||
'Password updated successfully', | ||
null | ||
); | ||
}; | ||
|
||
export default setNewPassword; |
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,40 @@ | ||
import { prisma } from '../../database/prisma-config.js'; | ||
import errorResponseHandler from '../../handlers/error-response-handlers.js'; | ||
import responseHandler from '../../handlers/response-handler.js'; | ||
|
||
const verifyResetPassword = async (req, res) => { | ||
const { verificationCode, employeeEmail } = req.body; | ||
|
||
if (!verificationCode || !employeeEmail) { | ||
return errorResponseHandler( | ||
res, | ||
400, | ||
'fail', | ||
'Please provide all required fields' | ||
); | ||
} | ||
|
||
const employee = await prisma.employee.findUnique({ | ||
where: { employeeEmail }, | ||
}); | ||
|
||
if (!employee) { | ||
return errorResponseHandler(res, 404, 'fail', 'Employee not found'); | ||
} | ||
|
||
if (employee.verificationCode !== verificationCode) { | ||
return errorResponseHandler(res, 400, 'fail', 'Invalid verification code'); | ||
} | ||
|
||
// update employee reset code | ||
await prisma.employee.update({ | ||
where: { employeeEmail }, | ||
data: { | ||
verificationCode: null, | ||
}, | ||
}); | ||
|
||
return responseHandler(res, 200, 'success', 'Verification Successful', null); | ||
}; | ||
|
||
export default verifyResetPassword; |
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,3 @@ | ||
import addRole from "./role/add-role.js"; | ||
|
||
export { addRole }; |
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,21 @@ | ||
import { prisma } from '../../database/prisma-config.js'; | ||
import errorResponseHandler from '../../handlers/error-response-handlers.js'; | ||
import responseHandler from '../../handlers/response-handler.js'; | ||
|
||
const addRole = async (req, res) => { | ||
const { roleName } = req.body; | ||
|
||
if (!roleName) { | ||
return errorResponseHandler(res, 400, 'fail', 'Please provide a role name'); | ||
} | ||
|
||
const role = await prisma.role.create({ | ||
data: { | ||
roleName, | ||
}, | ||
}); | ||
|
||
return responseHandler(res, 201, 'success', 'Role added successfully', role); | ||
}; | ||
|
||
export default addRole; |