Skip to content

Commit

Permalink
Add api logout
Browse files Browse the repository at this point in the history
  • Loading branch information
PhamAnhHoang committed Dec 18, 2023
1 parent 7092cd2 commit 6e39d89
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions apps/api/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { LocalAuthGuard } from './guards/local-auth.guard'
import { SessionAuthGuard } from './guards/session-auth.guard'
import { TokenInterceptor } from './interceptors/token.interceptor'
import {
LogoutResponseInterface,
PasswordResetPerformInterface,
PasswordResetRequestInterface,
Pure,
Expand Down Expand Up @@ -108,4 +109,16 @@ export class AuthController {
status: StatusType.OK
}
}

@Post('/logout')
@UseGuards(SessionAuthGuard, JWTAuthGuard)
@HttpCode(HttpStatus.OK)
async logout(
@AuthUser() user: Pure<UserEntity>
): Promise<LogoutResponseInterface> {
await this.authService.logout(user)
return {
status: StatusType.OK
}
}
}
4 changes: 4 additions & 0 deletions apps/api/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,8 @@ export class AuthService {
const hashedToken = await bcrypt.hash(token, salt)
await this.userService.storeRefreshToken(user, hashedToken)
}

async logout(user: UserEntity) {
return this.userService.storeRefreshToken(user, null);
}
}
5 changes: 5 additions & 0 deletions libs/interfaces/src/auth/logoutResponse.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { StatusType } from '../generic/Status.type'

export interface LogoutResponseInterface {
status: StatusType
}
1 change: 1 addition & 0 deletions libs/interfaces/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export * from './auth/signUpResponse.interface'
export * from './auth/confirmationCode.interface'
export * from './auth/refreshTokenResponse.interface'
export * from './auth/loginResponse.interface'
export * from './auth/logoutResponse.interface'
export * from './organization/Organization.interface'
export * from './user/enum/user-organization-enum'

0 comments on commit 6e39d89

Please sign in to comment.