-
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
b775d65
commit 347e17f
Showing
11 changed files
with
287 additions
and
22 deletions.
There are no files selected for viewing
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { getRepository } from 'typeorm'; | ||
import { User } from '../entities/user.auth'; | ||
import express from 'express'; | ||
import passport from 'passport'; | ||
// import session from 'express-session'; | ||
// import { PassportStatic } from 'passport'; | ||
const routerfb = express.Router(); | ||
require('dotenv').config(); | ||
|
||
async function findUserAccountIdProvider(accountId: string, provider: string): Promise<User | undefined> { | ||
try { | ||
const userRepository = getRepository(User); | ||
|
||
const user = await userRepository.findOne({ | ||
where: { | ||
accountId: accountId, | ||
provider: provider, | ||
}, | ||
}); | ||
|
||
if (user === null) { | ||
return undefined; | ||
} | ||
|
||
return user as User; | ||
} catch (error) { | ||
console.error('Error finding user:', error); | ||
return undefined; | ||
} | ||
} | ||
const accountId = 'your_account_id'; | ||
const provider = 'facebook'; | ||
|
||
findUserAccountIdProvider(accountId, provider) | ||
.then((user) => { | ||
if (user) { | ||
console.log('User found:', user); | ||
} else { | ||
console.log('User not found'); | ||
} | ||
}) | ||
.catch((error) => { | ||
console.error('Error:', error); | ||
}); | ||
|
||
routerfb.get('/', passport.authenticate('facebook', { scope: 'email' })); | ||
|
||
routerfb.get( | ||
'/callback', | ||
passport.authenticate('facebook', { | ||
failureRedirect: '/auth/facebook/error', | ||
}), | ||
function (req, res) { | ||
// Successful authentication, redirect to success screen. | ||
res.redirect('/auth/facebook/success'); | ||
} | ||
); | ||
|
||
routerfb.get('/success', async (req, res) => { | ||
const userInfo = { | ||
id: req.session.passport.user.id, | ||
displayName: req.session.passport.user.displayName, | ||
provider: req.session.passport.user.provider, | ||
}; | ||
res.render('fb-github-success', { user: userInfo }); | ||
}); | ||
|
||
routerfb.get('/error', (req, res) => res.send('Error logging in via Facebook..')); | ||
|
||
routerfb.get('/signout', (req, res) => { | ||
try { | ||
req.session.destroy(function (err) { | ||
console.log('session destroyed.'); | ||
}); | ||
res.render('auth'); | ||
} catch (err) { | ||
res.status(400).send({ message: 'Failed to sign out fb user' }); | ||
} | ||
}); | ||
|
||
module.exports = routerfb; | ||
|
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,49 @@ | ||
import passport from 'passport'; | ||
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy; | ||
import express from 'express'; | ||
import googleAuth from '../middlewares/auth'; | ||
const routers = express.Router(); | ||
require('dotenv').config(); | ||
|
||
let userProfile: any; | ||
passport.use( | ||
new GoogleStrategy( | ||
{ | ||
clientID: process.env.GOOGLE_CLIENT_ID, | ||
clientSecret: process.env.GOOGLE_CLIENT_SECRET, | ||
callbackURL: process.env.CALLBACK_URL, | ||
}, | ||
function (_accessToken: any, _refreshToken: any, profile: any, done: (arg0: null, arg1: any) => any) { | ||
userProfile = profile; | ||
return done(null, userProfile); | ||
} | ||
) | ||
); | ||
|
||
// request at /auth/google, when user click sign-up with google button transferring | ||
// the request to google server, to show emails screen | ||
routers.get( | ||
'/', | ||
passport.authenticate('google', { scope: ['profile', 'email'] }) | ||
); | ||
|
||
// URL Must be same as 'Authorized redirect URIs' field of OAuth client, i.e: /auth/google/callback | ||
routers.get( | ||
'/callback', | ||
passport.authenticate('google', { failureRedirect: '/auth/google/error' }), | ||
(req: any, res: { redirect: (arg0: string) => void; }) => { | ||
res.redirect('/auth/google/success'); // Successful authentication, redirect success. | ||
} | ||
); | ||
|
||
routers.get('/success', async (req: any, res: { render: (arg0: string, arg1: { user: any; }) => void; }) => { | ||
const { failure, success } = await googleAuth.registerWithGoogle(userProfile); | ||
if (failure) console.log('Google user already exist in DB..'); | ||
else console.log('Registering new Google user..'); | ||
res.render('success', { user: userProfile }); | ||
}); | ||
|
||
routers.get('/error', (req: any, res: { send: (arg0: string) => any; }) => res.send('Error logging in via Google..')); | ||
|
||
|
||
export default routers; |
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,22 @@ | ||
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from 'typeorm'; | ||
|
||
@Entity() | ||
export class User extends BaseEntity { | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string | undefined; | ||
|
||
@Column({ type: 'text', nullable: true, unique: true }) | ||
email: string | undefined; | ||
|
||
@Column({ type: 'text', nullable: true }) | ||
accountId: string | undefined; | ||
|
||
@Column({ type: 'text', nullable: true }) | ||
name!: string; | ||
|
||
@Column({ type: 'text', nullable: true }) | ||
photoURL: string | undefined; | ||
|
||
@Column({ type: 'text', nullable: true }) | ||
provider: string | undefined; | ||
} |
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,72 @@ | ||
import { User } from '../entities/user.auth'; | ||
import { getRepository, FindOneOptions } from 'typeorm'; | ||
|
||
interface OAuthUser { | ||
id: any; | ||
provider: any; | ||
displayName: any; | ||
emails: { value: any }[]; | ||
photos: { value: any }[]; | ||
} | ||
|
||
const googleAuthDal = { | ||
registerWithGoogle: async (oauthUser: OAuthUser) => { | ||
const userRepository = getRepository(User); | ||
|
||
try { | ||
// Check if user already exists | ||
const existingUser = await userRepository.findOne({ | ||
where: { | ||
accountId: oauthUser.id, | ||
provider: oauthUser.provider, | ||
}, | ||
}); | ||
|
||
if (existingUser) { | ||
return { failure: { message: 'User already registered.' } }; | ||
} | ||
|
||
// Create a new user entity | ||
const newUser = userRepository.create({ | ||
accountId: oauthUser.id, | ||
name: oauthUser.displayName, | ||
provider: oauthUser.provider, | ||
// Adjust property names based on your User entity | ||
email: oauthUser.emails[0]?.value, // Assuming emails[0] contains the email value | ||
photoURL: oauthUser.photos[0]?.value, // Assuming photos[0] contains the photo URL value | ||
}); | ||
|
||
// Save the new user to the database | ||
await userRepository.save(newUser); | ||
|
||
return { success: { message: 'User registered successfully.' } }; | ||
} catch (error) { | ||
console.error('Error registering user with Google:', error); | ||
return { failure: { message: 'Failed to register user.' } }; | ||
} | ||
}, | ||
|
||
loginUser: async (oauthUser: OAuthUser) => { | ||
const userRepository = getRepository(User); | ||
|
||
try { | ||
// Check if a user with the provided email exists | ||
const userExists = await userRepository.findOne({ | ||
where: { | ||
email: oauthUser.emails[0]?.value, // Assuming emails[0] contains the email value | ||
}, | ||
}); | ||
|
||
if (userExists) { | ||
return { success: { message: 'User successfully logged in.' } }; | ||
} else { | ||
return { failure: { message: 'Email not registered. You need to sign up first.' } }; | ||
} | ||
} catch (error) { | ||
console.error('Error logging in user:', error); | ||
return { failure: { message: 'Failed to log in user.' } }; | ||
} | ||
}, | ||
}; | ||
|
||
export default googleAuthDal; |
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-session'; | ||
import { PassportStatic } from 'passport'; | ||
|
||
declare module 'express-session' { | ||
interface SessionData { | ||
passport?: any; // Define the passport property as any | ||
} | ||
} |
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