-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
google authentication #84
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @GSinseswa721 remove unnecessary commented codes. |
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; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @GSinseswa721 remove the console.log(".................") |
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); | ||
} | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GSinseswa721 Remove as any on middlewares , using any we're not utilising power of typescript.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GSinseswa721
Remove as any on middlewares !