Skip to content
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

Linked in authentication implementation #80

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
fd3ab92
Get all users endpoint
Shrenik0321 Aug 12, 2023
be88998
Merge branch 'main' of https://github.com/Shrenik0321/scholarx-backen…
Shrenik0321 Aug 12, 2023
38ddb1f
Merge branch 'main' into get-all-users-endpoint
YoshithaRathnayake Aug 12, 2023
0c23030
Merge branch 'main' into get-all-users-endpoint
Shrenik0321 Aug 18, 2023
7640d98
Folder structure fix
Shrenik0321 Aug 18, 2023
50d0837
Merge branch 'main' of https://github.com/Shrenik0321/scholarx-backen…
Shrenik0321 Aug 18, 2023
932e287
Issue fixes
Shrenik0321 Aug 18, 2023
2301af2
Merge branch 'get-all-users-endpoint' of https://github.com/Shrenik03…
Shrenik0321 Aug 18, 2023
990c846
Unit Test updates
Shrenik0321 Aug 19, 2023
17bf386
Testing for Admin role
Shrenik0321 Aug 20, 2023
833f565
Issue fixes for Admin role
Shrenik0321 Aug 20, 2023
23d63c3
Merge conflict
Shrenik0321 Sep 16, 2023
b363354
Implement the mentor availability endpoint
Shrenik0321 Sep 18, 2023
cadcd48
Implement the mentor availability endpoint
Shrenik0321 Sep 18, 2023
0e3c4af
Changed routes and added test
Shrenik0321 Sep 22, 2023
73097e3
lint warning fix
Shrenik0321 Sep 22, 2023
ddffc6c
Admin route update
Shrenik0321 Sep 23, 2023
135f8b0
Controller function refactor updates
Shrenik0321 Oct 8, 2023
8f91cce
Controller function refactor updates
Shrenik0321 Oct 8, 2023
e007225
Lint fix
Shrenik0321 Oct 8, 2023
214a89f
Test fix
Shrenik0321 Oct 8, 2023
7331746
Added Typescript generics
Shrenik0321 Oct 9, 2023
669e6d8
Merge branch 'main' of https://github.com/Shrenik0321/scholarx-backen…
Shrenik0321 Oct 9, 2023
b7c6c8a
Lint fix
Shrenik0321 Oct 9, 2023
7a92895
Code fixes
Shrenik0321 Oct 10, 2023
0dd64fc
Code fixes
Shrenik0321 Oct 10, 2023
4ccf832
Updates
Shrenik0321 Oct 10, 2023
8d7eed1
Removal of nested if statements
Shrenik0321 Oct 10, 2023
b9981a7
merge resolve
Shrenik0321 Oct 22, 2023
2470b9e
merge conflict resolves
Shrenik0321 Nov 6, 2023
4e65748
Merge branch 'main' of https://github.com/Shrenik0321/scholarx-backen…
Shrenik0321 Nov 6, 2023
e969900
LinkedIn authentication with passport.js
Shrenik0321 Nov 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }]
}
Loading