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!

(imporvments) logout form all devices

Maksim Mrugd54a72cec5ac
diff --git a/apps/production/src/auth/auth.controller.ts b/apps/production/src/auth/auth.controller.ts
index 63ea66f77..d850239c9 100644
--- a/apps/production/src/auth/auth.controller.ts
+++ b/apps/production/src/auth/auth.controller.ts
@@ -421,6 +421,37 @@ export class AuthController {
await this.authService.logout(user.id, refreshToken)
}
+ @ApiOperation({ summary: 'Logout from all devices' })
+ @ApiOkResponse({
+ description: 'Logged out from all devices',
+ })
+ @Public()
+ @UseGuards(JwtRefreshTokenGuard)
+ @Post('logout-all')
+ @HttpCode(200)
+ public async logoutAll(
+ @CurrentUserId() userId: string,
+ @CurrentUser('refreshToken') refreshToken: string,
+ @I18n() i18n: I18nContext,
+ ): Promise<void> {
+ 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.logoutAll(user.id)
+ }
+
// SSO section
@ApiOperation({ summary: 'Generate SSO authentication URL' })
@Post('sso/generate')
diff --git a/apps/production/src/auth/auth.service.ts b/apps/production/src/auth/auth.service.ts
index acb1a4fc8..a1d0a06dc 100644
--- a/apps/production/src/auth/auth.service.ts
+++ b/apps/production/src/auth/auth.service.ts
@@ -463,6 +463,10 @@ export class AuthService {
await this.userService.deleteRefreshToken(userId, refreshToken)
}
+ async logoutAll(userId: string) {
+ await this.userService.deleteAllRefreshTokens(userId)
+ }
+
async validateApiKey(apiKey: string): Promise<User | null> {
return this.userService.findUserByApiKey(apiKey)
}
diff --git a/apps/production/src/user/user.service.ts b/apps/production/src/user/user.service.ts
index 392124400..189ca9db4 100644
--- a/apps/production/src/user/user.service.ts
+++ b/apps/production/src/user/user.service.ts
@@ -249,6 +249,12 @@ export class UserService {
})
}
+ public async deleteAllRefreshTokens(userId: string) {
+ await this.refreshTokenRepository.delete({
+ userId,
+ })
+ }
+
public async findUserByApiKey(apiKey: string) {
return this.usersRepository.findOne({ where: { apiKey } })
}