-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Dileepa Mabulage <[email protected]>
- Loading branch information
1 parent
519c6d3
commit b6ff45c
Showing
12 changed files
with
236 additions
and
26 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,101 @@ | ||
import type { Request } from 'express' | ||
import passport from 'passport' | ||
import { Strategy as JwtStrategy } from 'passport-jwt' | ||
import Profile from '../entities/profile.entity' | ||
import { dataSource } from './dbConfig' | ||
import { | ||
JWT_SECRET, | ||
LINKEDIN_CLIENT_ID, | ||
LINKEDIN_CLIENT_SECRET, | ||
LINKEDIN_REDIRECT_URL | ||
} from './envConfig' | ||
|
||
import { Strategy as LinkedInStrategy } from 'passport-linkedin-oauth2' | ||
import { findOrCreateUser } from '../services/auth.service' | ||
import { type CreateProfile, type LinkedInProfile, type User } from '../types' | ||
|
||
passport.use( | ||
new LinkedInStrategy( | ||
{ | ||
clientID: LINKEDIN_CLIENT_ID, | ||
clientSecret: LINKEDIN_CLIENT_SECRET, | ||
callbackURL: LINKEDIN_REDIRECT_URL, | ||
scope: ['openid', 'email', 'profile'], | ||
passReqToCallback: true | ||
}, | ||
async function ( | ||
req: Request, | ||
accessToken: string, | ||
refreshToken: string, | ||
profile: passport.Profile, | ||
done: (err: Error | null, user?: Profile) => void | ||
) { | ||
try { | ||
const data = profile as unknown as LinkedInProfile | ||
const createProfile: CreateProfile = { | ||
id: data.id, | ||
primary_email: data?.email ?? '', | ||
first_name: data?.givenName ?? '', | ||
last_name: data?.familyName ?? '', | ||
image_url: data?.picture ?? '' | ||
} | ||
const user = await findOrCreateUser(createProfile) | ||
done(null, user) | ||
} catch (err) { | ||
done(err as Error) | ||
} | ||
} | ||
) | ||
) | ||
|
||
passport.serializeUser((user: Express.User, done) => { | ||
done(null, (user as User).primary_email) | ||
}) | ||
|
||
passport.deserializeUser(async (primary_email: string, done) => { | ||
try { | ||
const profileRepository = dataSource.getRepository(Profile) | ||
const user = await profileRepository.findOne({ | ||
where: { primary_email }, | ||
relations: ['mentor', 'mentee'] | ||
}) | ||
done(null, user) | ||
} catch (err) { | ||
done(err) | ||
} | ||
}) | ||
|
||
const cookieExtractor = (req: Request): string => { | ||
let token = null | ||
if (req?.cookies) { | ||
token = req.cookies.jwt | ||
} | ||
return token | ||
} | ||
|
||
const options = { | ||
jwtFromRequest: cookieExtractor, | ||
secretOrKey: JWT_SECRET | ||
} | ||
|
||
passport.use( | ||
new JwtStrategy(options, async (jwtPayload, done) => { | ||
try { | ||
const profileRepository = dataSource.getRepository(Profile) | ||
const profile = await profileRepository.findOne({ | ||
where: { uuid: jwtPayload.userId }, | ||
relations: ['mentor', 'mentee'] | ||
}) | ||
|
||
if (!profile) { | ||
done(null, false) | ||
} else { | ||
done(null, profile) | ||
} | ||
} catch (error) { | ||
done(error, false) | ||
} | ||
}) | ||
) | ||
|
||
export default passport |
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