-
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
1 parent
033cde2
commit 6aa2cd7
Showing
8 changed files
with
189 additions
and
18 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,37 @@ | ||
import request from 'supertest'; | ||
import { app, server } from '../index'; | ||
import { createConnection, getConnection, getConnectionOptions, getRepository } from 'typeorm'; | ||
import { User } from '../entities/User'; | ||
|
||
beforeAll(async () => { | ||
// Connect to the test database | ||
const connectionOptions = await getConnectionOptions(); | ||
|
||
await createConnection({ ...connectionOptions, name: 'testConnection' }); | ||
}); | ||
|
||
afterAll(async () => { | ||
const connection = getConnection('testConnection'); | ||
const userRepository = connection.getRepository(User); | ||
|
||
// Delete all records from the User | ||
await userRepository.delete({}); | ||
|
||
// Close the connection to the test database | ||
await connection.close(); | ||
|
||
server.close(); | ||
}); | ||
describe('authentication routes test',() => { | ||
it('should redirect to the google authentication page',async() => { | ||
const response = await request(app) | ||
.get('/user/google-auth'); | ||
expect(response.statusCode).toBe(302) | ||
}) | ||
it('should redirect after google authentication', async() => { | ||
const response = await request(app) | ||
.get('/user/auth/google/callback'); | ||
expect(response.statusCode).toBe(302) | ||
}) | ||
}); | ||
|
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { Router } from 'express'; | ||
import { RequestHandler, Router } from 'express'; | ||
import { authMiddleware } from '../middlewares/verifyToken'; | ||
import { hasRole } from '../middlewares'; | ||
import { checkUserStatus } from '../middlewares/isAllowed'; | ||
import { wishlistAddProduct,wishlistRemoveProduct,wishlistGetProducts,wishlistClearAllProducts } from '../controllers/wishListController'; | ||
|
||
const router = Router(); | ||
|
||
router.post('/add/:id', authMiddleware, checkUserStatus, hasRole('BUYER'), wishlistAddProduct); | ||
router.get('/',authMiddleware, checkUserStatus, hasRole('BUYER'),wishlistGetProducts); | ||
router.delete('/delete/:id',authMiddleware, checkUserStatus, hasRole('BUYER'),wishlistRemoveProduct); | ||
router.delete('/clearAll',authMiddleware, checkUserStatus, hasRole('BUYER'),wishlistClearAllProducts); | ||
router.post('/add/:id', authMiddleware as RequestHandler, checkUserStatus, hasRole('BUYER'), wishlistAddProduct); | ||
router.get('/',authMiddleware as RequestHandler, checkUserStatus, hasRole('BUYER'),wishlistGetProducts); | ||
router.delete('/delete/:id',authMiddleware as RequestHandler, checkUserStatus, hasRole('BUYER'),wishlistRemoveProduct); | ||
router.delete('/clearAll',authMiddleware as RequestHandler, checkUserStatus, hasRole('BUYER'),wishlistClearAllProducts); | ||
|
||
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,72 @@ | ||
/* eslint-disable camelcase */ | ||
import passport from 'passport'; | ||
import { Strategy } from "passport-google-oauth20"; | ||
import { User } from '../entities/User'; | ||
import { getRepository } from 'typeorm'; | ||
import bcrypt from 'bcrypt'; | ||
import "../utils/auth"; | ||
passport.use( | ||
new Strategy( | ||
{ | ||
clientID: process.env.GOOGLE_CLIENT_ID as string, | ||
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, | ||
callbackURL: 'http://localhost:6890/user/auth/google/callback/', | ||
scope: ['email', 'profile'], | ||
}, | ||
async (accessToken: any, refreshToken: any, profile: any, cb: any) => { | ||
const userRepository = getRepository(User); | ||
const { family_name, | ||
name, | ||
picture, | ||
email, | ||
email_verified | ||
|
||
} = profile._json; | ||
const { familyName, givenName } = profile.name; | ||
|
||
if (email || givenName || family_name || picture) { | ||
try { | ||
// Check for existing user | ||
const existingUser = await userRepository.findOneBy({ email }); | ||
|
||
if (existingUser) { | ||
return await cb(null, existingUser); | ||
} | ||
const saltRounds = 10; | ||
const hashedPassword = await bcrypt.hash("password", saltRounds); | ||
const newUser = new User(); | ||
newUser.firstName = givenName; | ||
newUser.lastName = family_name ?? familyName ?? "undefined"; | ||
newUser.email = email; | ||
newUser.userType = 'Buyer'; | ||
newUser.photoUrl = picture; | ||
newUser.gender = "Not specified"; | ||
newUser.phoneNumber = "Not specified"; | ||
newUser.password = hashedPassword; | ||
newUser.verified = email_verified; | ||
|
||
await userRepository.save(newUser); | ||
return await cb(null, newUser); | ||
} catch (error) { | ||
console.error(error); | ||
return await cb(error, null); | ||
} | ||
} | ||
return await cb(null, profile, { message: 'Missing required profile information' }); | ||
} | ||
) | ||
); | ||
|
||
passport.serializeUser((user: any, cb) => { | ||
cb(null, user.id); | ||
}); | ||
|
||
passport.deserializeUser(async (id: any, cb) => { | ||
const userRepository = getRepository(User); | ||
try { | ||
const user = await userRepository.findOneBy({id}); | ||
cb(null, user); | ||
} catch (error) { | ||
cb(error); | ||
} | ||
}); |