Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Added logout route

Yevhenii Kulisidi36b53c55e173
diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts
index 1c312c2ad..585e8b633 100644
--- a/src/auth/auth.controller.ts
+++ b/src/auth/auth.controller.ts
@@ -371,4 +371,35 @@ export class AuthController {
return { accessToken }
}
+
+ @ApiOperation({ summary: 'Logout' })
+ @ApiOkResponse({
+ description: 'Logged out',
+ })
+ @Public()
+ @UseGuards(JwtRefreshTokenGuard)
+ @Post('logout')
+ @HttpCode(200)
+ public async logout(
+ @CurrentUserId() userId: string,
+ @CurrentUser('refreshToken') refreshToken: string,
+ @I18n() i18n: I18nContext,
+ ) {
+ const user = await this.userService.findUserById(userId)
+
+ if (!user) {
+ throw new UnauthorizedException()
+ }
+
+ const isRefreshTokenValid = await this.authService.checkRefreshToken(
+ user.id,
+ refreshToken,
+ )
+
+ if (!isRefreshTokenValid) {
+ throw new ConflictException(i18n.t('auth.invalidRefreshToken'))
+ }
+
+ await this.authService.logout(user.id, refreshToken)
+ }
}
diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts
index b2343ae4d..8a95b6112 100644
--- a/src/auth/auth.service.ts
+++ b/src/auth/auth.service.ts
@@ -382,4 +382,8 @@ export class AuthService {
return { accessToken, refreshToken }
}
+
+ async logout(userId: string, refreshToken: string) {
+ await this.userService.deleteRefreshToken(userId, refreshToken)
+ }
}
diff --git a/src/user/user.service.ts b/src/user/user.service.ts
index ebe61b69e..675f02ded 100644
--- a/src/user/user.service.ts
+++ b/src/user/user.service.ts
@@ -188,4 +188,11 @@ export class UserService {
},
})
}
+
+ public async deleteRefreshToken(userId: string, refreshToken: string) {
+ await this.refreshTokenRepository.delete({
+ userId,
+ refreshToken,
+ })
+ }
}