-
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.
- Loading branch information
Showing
9 changed files
with
145 additions
and
20 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,31 @@ | ||
import request from 'supertest'; | ||
import { app, server } from '../index'; // update this with the path to your app file | ||
import { createConnection, getConnection, getConnectionOptions } from 'typeorm'; | ||
|
||
beforeAll(async () => { | ||
// Connect to the test database | ||
const connectionOptions = await getConnectionOptions(); | ||
await createConnection({ ...connectionOptions, name: 'testConnection' }); | ||
}); | ||
|
||
afterAll(async () => { | ||
await getConnection('testConnection').close(); | ||
server.close(); | ||
}); | ||
|
||
describe('POST /user/signin', () => { | ||
it('should sign in a user', async () => { | ||
// Arrange | ||
const userSignin = { | ||
email: '[email protected]', | ||
password: 'ndevu', | ||
}; | ||
|
||
// Act | ||
const res = await request(app).post('/user/signin').send(userSignin); | ||
|
||
// Assert | ||
expect(res.status).toBe(200); | ||
expect(res.body).toEqual({ Message: 'signed in successfully', userObject: { accessToken: expect.any(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { UserController } from './authController'; | ||
import signinAndOutController from './signinAndOutController'; | ||
|
||
export{UserController}; | ||
export { UserController, signinAndOutController }; |
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,57 @@ | ||
import { Request, Response } from 'express'; | ||
import { User } from '../entities/User'; | ||
import { sign } from '../helpers/token'; | ||
import { check } from '../helpers/verifyPassward'; | ||
import { getRepository } from 'typeorm'; | ||
|
||
// import { blackListedTokens } from '../middlewares/authentication'; | ||
|
||
class signinAndOutController { | ||
// Method to login admin | ||
static async userSignin(req: Request, res: Response): Promise<void> { | ||
Check warning on line 11 in src/controllers/signinAndOutController.ts GitHub Actions / build-lint-test-coverage
|
||
try { | ||
const { email, password } = req.body; | ||
|
||
if (!email || !password) { | ||
res.status(400).json({ Message: 'Email and password are required' }); | ||
return; | ||
} | ||
const userRepository = getRepository(User); | ||
|
||
const user = await userRepository.findOneBy({ email: email }); | ||
|
||
if (!user) { | ||
res.status(401).json({ Message: 'Invalid email' }); | ||
return; | ||
} | ||
|
||
const isPasswordCorrect = check(user.password, password); | ||
if (!isPasswordCorrect) { | ||
res.status(401).json({ Message: 'Invalid password' }); | ||
return; | ||
} | ||
|
||
const accessToken = sign({ | ||
id: user.id, | ||
email: user.email, | ||
role: user.userType, | ||
}); | ||
|
||
const userObject: any = { | ||
accessToken: accessToken, | ||
}; | ||
if (process.env.NODE_ENV === 'production') { | ||
res.cookie('accessToken', accessToken, { httpOnly: true, sameSite: 'none', secure: true }); | ||
} else { | ||
res.cookie('accessToken', accessToken, { httpOnly: true, sameSite: 'lax' }); | ||
} | ||
|
||
res.status(200).json({ Message: 'signed in successfully', userObject }); | ||
} catch (error) { | ||
console.error('Error signing in a user:', error); | ||
res.status(500).json({ error: 'Sorry, Something went wrong' }); | ||
} | ||
} | ||
} | ||
|
||
export default signinAndOutController; |
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,15 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
const jwtSecretKey = process.env.JWT_SECRETKEY; | ||
|
||
if (!jwtSecretKey) { | ||
throw new Error('JWT_SECRETKEY is not defined in the environment variables.'); | ||
} | ||
|
||
export const sign = (payload: string | object | Buffer): string => | ||
jwt.sign(payload, jwtSecretKey, { expiresIn: '48h' }); | ||
|
||
export const verify = (token: string): string | object => jwt.verify(token, jwtSecretKey); |
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,14 @@ | ||
import bcrypt from 'bcrypt'; | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
const jwtSecretKey = process.env.JWT_SECRETKEY; | ||
|
||
if (!jwtSecretKey) { | ||
throw new Error('JWT_SECRETKEY is not defined in the environment variables.'); | ||
} | ||
|
||
export const check = (hashedPassword: string, password: string): boolean => { | ||
return bcrypt.compareSync(password, hashedPassword); | ||
}; |
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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { Router } from 'express'; | ||
import { Router } from 'express'; | ||
import { UserController } from '../controllers/index'; | ||
|
||
import { signinAndOutController } from '../controllers/index'; | ||
|
||
const { registerUser } = UserController; | ||
const { userSignin } = signinAndOutController; | ||
|
||
const router = Router(); | ||
|
||
router.post('/register', registerUser); | ||
router.post('/signin', userSignin); | ||
|
||
export default router; | ||
export default router; |