-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: extract ITokenService port to remove Fastify from AuthResolver #20
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
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 hidden or 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 hidden or 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
32 changes: 32 additions & 0 deletions
32
apps/api/src/infrastructure/auth/FastifyJwtTokenService.ts
This file contains hidden or 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,32 @@ | ||
| import type { FastifyInstance } from 'fastify'; | ||
| import type { ITokenService, TokenPair } from '@/use-cases/ports/ITokenService.js'; | ||
|
|
||
| interface Deps { | ||
| fastify: FastifyInstance; | ||
| } | ||
|
|
||
| export class FastifyJwtTokenService implements ITokenService { | ||
| constructor(private readonly deps: Deps) {} | ||
|
|
||
| sign(userId: string, email: string): TokenPair { | ||
| const accessToken = this.deps.fastify.jwt.sign( | ||
| { sub: userId, email }, | ||
| { expiresIn: '15m' }, | ||
| ); | ||
| const refreshToken = this.deps.fastify.jwt.sign( | ||
| { sub: userId, email }, | ||
| { key: process.env.JWT_REFRESH_SECRET!, expiresIn: '7d' }, | ||
| ); | ||
| return { accessToken, refreshToken }; | ||
| } | ||
|
|
||
| verifyRefresh(token: string): { sub: string; email: string } { | ||
| try { | ||
| return this.deps.fastify.jwt.verify<{ sub: string; email: string }>(token, { | ||
| key: process.env.JWT_REFRESH_SECRET!, | ||
| }); | ||
| } catch { | ||
| throw Object.assign(new Error('Invalid refresh token'), { code: 'UNAUTHORIZED' }); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,49 +1,28 @@ | ||
| import type { FastifyInstance } from 'fastify'; | ||
| import type { IRegisterUseCase } from '@/use-cases/auth/IRegisterUseCase.js'; | ||
| import type { ILoginUseCase } from '@/use-cases/auth/ILoginUseCase.js'; | ||
| import type { ITokenService, TokenPair } from '@/use-cases/ports/ITokenService.js'; | ||
|
|
||
| interface Deps { | ||
| registerUseCase: IRegisterUseCase; | ||
| loginUseCase: ILoginUseCase; | ||
| fastify: FastifyInstance; | ||
| } | ||
|
|
||
| interface TokenPair { | ||
| accessToken: string; | ||
| refreshToken: string; | ||
| tokenService: ITokenService; | ||
| } | ||
|
|
||
| export class AuthResolver { | ||
| constructor(private readonly deps: Deps) {} | ||
|
|
||
| async register(email: string, password: string): Promise<TokenPair> { | ||
| const { userId } = await this.deps.registerUseCase.execute({ email, password }); | ||
| return this.signTokens(userId, email); | ||
| return this.deps.tokenService.sign(userId, email); | ||
| } | ||
|
|
||
| async login(email: string, password: string): Promise<TokenPair> { | ||
| const user = await this.deps.loginUseCase.execute({ email, password }); | ||
| return this.signTokens(user.id, user.email); | ||
| return this.deps.tokenService.sign(user.id, user.email); | ||
| } | ||
|
|
||
| refreshToken(refreshToken: string): TokenPair { | ||
| let payload: { sub: string; email: string }; | ||
| try { | ||
| payload = this.deps.fastify.jwt.verify<{ sub: string; email: string }>(refreshToken, { | ||
| key: process.env.JWT_REFRESH_SECRET!, | ||
| }); | ||
| } catch { | ||
| throw Object.assign(new Error('Invalid refresh token'), { code: 'UNAUTHORIZED' }); | ||
| } | ||
| return this.signTokens(payload.sub, payload.email); | ||
| } | ||
|
|
||
| private signTokens(userId: string, email: string): TokenPair { | ||
| const accessToken = this.deps.fastify.jwt.sign({ sub: userId, email }, { expiresIn: '15m' }); | ||
| const refreshToken = this.deps.fastify.jwt.sign( | ||
| { sub: userId, email }, | ||
| { key: process.env.JWT_REFRESH_SECRET!, expiresIn: '7d' }, | ||
| ); | ||
| return { accessToken, refreshToken }; | ||
| const payload = this.deps.tokenService.verifyRefresh(refreshToken); | ||
| return this.deps.tokenService.sign(payload.sub, payload.email); | ||
| } | ||
| } |
This file contains hidden or 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,9 @@ | ||
| export interface TokenPair { | ||
| accessToken: string; | ||
| refreshToken: string; | ||
| } | ||
|
|
||
| export interface ITokenService { | ||
| sign(userId: string, email: string): TokenPair; | ||
| verifyRefresh(token: string): { sub: string; email: string }; | ||
| } |
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.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: mankatcheung/job-finder
Length of output: 12864
🏁 Script executed:
Repository: mankatcheung/job-finder
Length of output: 18657
🏁 Script executed:
Repository: mankatcheung/job-finder
Length of output: 6056
Validate
JWT_REFRESH_SECRETin the constructor (apps/api/src/infrastructure/auth/FastifyJwtTokenService.ts:8-32).process.env.JWT_REFRESH_SECRET!is read lazily insign()andverifyRefresh(), and this service is only resolved when auth flows run, so a missing secret lets the app boot and then breaks the first login/refresh request. Cache it in a private field and throw if it is unset.🤖 Prompt for AI Agents