Skip to content

Commit

Permalink
LinkedIn authentication with passport.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Shrenik0321 committed Nov 7, 2023
1 parent 4e65748 commit e969900
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 3 deletions.
121 changes: 121 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"express-session": "^1.17.3",
"jsonwebtoken": "^9.0.1",
"passport": "^0.6.0",
"passport-jwt": "^4.0.1",
"passport-linkedin-oauth2": "^2.0.0",
"pg": "^8.10.0",
"reflect-metadata": "^0.1.13",
"ts-node": "^10.9.1",
Expand All @@ -34,11 +36,13 @@
"@types/cookie-parser": "^1.4.3",
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/express-session": "^1.17.10",
"@types/jest": "^29.5.3",
"@types/jsonwebtoken": "^9.0.2",
"@types/node": "^20.1.4",
"@types/passport": "^1.0.12",
"@types/passport-jwt": "^3.0.9",
"@types/passport-linkedin-oauth2": "^1.5.5",
"@types/pg": "^8.10.1",
"@types/prettier": "^2.7.2",
"@types/supertest": "^2.0.12",
Expand Down
5 changes: 4 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import categoryRouter from './routes/category/category.route'
import passport from 'passport'
import './configs/passport'
import cookieParser from 'cookie-parser'

import session from 'express-session'
import { SESSION_SECRET } from './configs/envConfig'
const app = express()

app.use(cookieParser())
app.use(bodyParser.json())
app.use(cors())
app.use(session({ secret: SESSION_SECRET }))
app.use(passport.initialize())
app.use(passport.session())

app.get('/', (req, res) => {
res.send('ScholarX Backend')
Expand Down
3 changes: 3 additions & 0 deletions src/configs/envConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ export const DB_PASSWORD = process.env.DB_PASSWORD
export const DB_PORT = process.env.DB_PORT
export const SERVER_PORT = process.env.SERVER_PORT ?? 3000
export const JWT_SECRET = process.env.JWT_SECRET ?? ''
export const LINKEDIN_KEY = process.env.LINKEDIN_KEY ?? ''
export const LINKEDIN_SECRET = process.env.LINKEDIN_SECRET ?? ''
export const SESSION_SECRET = process.env.SESSION_SECRET ?? ''
26 changes: 25 additions & 1 deletion src/configs/passport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import passport from 'passport'
import { Strategy as JwtStrategy } from 'passport-jwt'
import { dataSource } from './dbConfig'
import Profile from '../entities/profile.entity'
import { JWT_SECRET } from './envConfig'
import { JWT_SECRET, LINKEDIN_KEY, LINKEDIN_SECRET } from './envConfig'
import type { Request } from 'express'
import { Strategy as LinkedInStrategy } from 'passport-linkedin-oauth2'
import type { LinkedInUser } from '../types'

const cookieExtractor = (req: Request): string => {
let token = null
Expand Down Expand Up @@ -37,4 +39,26 @@ passport.use(
})
)

passport.use(
new LinkedInStrategy(
{
clientID: LINKEDIN_KEY,
clientSecret: LINKEDIN_SECRET,
callbackURL: 'http://localhost:3000/api/auth/linkedin/callback',
scope: ['r_emailaddress', 'r_liteprofile']
},
(accessToken, refreshToken, profile, done) => {
done(null, profile)
}
)
)

passport.serializeUser((user, done) => {
done(null, user)
})

passport.deserializeUser((user: LinkedInUser, done) => {
done(null, user)
})

export default passport
13 changes: 12 additions & 1 deletion src/routes/auth/auth.route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import express from 'express'
import { register, login, logout } from '../../controllers/auth.controller'
import passport from 'passport'

const authRouter = express.Router()

authRouter.post('/register', register)
authRouter.post('/login', login)
authRouter.get('/logout', logout)

authRouter.get(
'/linkedin',
passport.authenticate('linkedin', { state: 'SOME STATE' })
)
authRouter.get(
'/linkedin/callback',
passport.authenticate('linkedin', {
successRedirect: '/',
failureRedirect: '/'
})
)
export default authRouter
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ export interface ApiResponse<T> {
message?: string
data?: T | null
}

export interface LinkedInUser {
id: string
displayName: string
name: {
familyName: string
givenName: string
}
emails: [{ value: string }]
photos: [{ value: string }]
}

0 comments on commit e969900

Please sign in to comment.