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!

fixed /logout & /refresh-token for selfhosted, linting fixes

Blue Mouseb8c36e137e5d
diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts
index 6405180ad..582601f42 100644
--- a/src/auth/auth.controller.ts
+++ b/src/auth/auth.controller.ts
@@ -27,9 +27,9 @@ import { I18nValidationExceptionFilter, I18n, I18nContext } from 'nestjs-i18n'
import * as _pick from 'lodash/pick'
import { checkRateLimit } from 'src/common/utils'
-import { UserType, User } from 'src/user/entities/user.entity'
+import { UserType, User, generateSelfhostedUser } from 'src/user/entities/user.entity'
import { UserService } from 'src/user/user.service'
-import { isDevelopment, isSelfhosted } from 'src/common/constants'
+import { isSelfhosted } from 'src/common/constants'
import { AuthService } from './auth.service'
import { Public, CurrentUserId, CurrentUser, Roles } from './decorators'
import { SelfhostedGuard } from '../common/guards/selfhosted.guard'
@@ -384,7 +384,9 @@ export class AuthController {
@CurrentUser('refreshToken') refreshToken: string,
@I18n() i18n: I18nContext,
): Promise<{ accessToken: string }> {
- const user = await this.userService.findUserById(userId)
+ const user = isSelfhosted
+ ? generateSelfhostedUser()
+ : await this.userService.findUserById(userId)
if (!user) {
throw new UnauthorizedException()
@@ -420,7 +422,9 @@ export class AuthController {
@CurrentUser('refreshToken') refreshToken: string,
@I18n() i18n: I18nContext,
): Promise<void> {
- const user = await this.userService.findUserById(userId)
+ const user = isSelfhosted
+ ? generateSelfhostedUser()
+ : await this.userService.findUserById(userId)
if (!user) {
throw new UnauthorizedException()
diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts
index ed62fef45..c8ec13e11 100644
--- a/src/auth/auth.service.ts
+++ b/src/auth/auth.service.ts
@@ -41,6 +41,7 @@ import { ProjectService } from 'src/project/project.service'
import {
saveRefreshTokenClickhouse,
findRefreshTokenClickhouse,
+ deleteRefreshTokenClickhouse,
} from 'src/common/utils'
import {
REDIS_SSO_UUID,
@@ -50,7 +51,6 @@ import {
isSelfhosted,
SELFHOSTED_EMAIL,
SELFHOSTED_PASSWORD,
- SELFHOSTED_UUID,
} from 'src/common/constants'
import { RefreshToken } from 'src/user/entities/refresh-token.entity'
import { SSOProviders } from './dtos/sso-generate.dto'
@@ -455,15 +455,12 @@ export class AuthService {
userId: string,
refreshToken: string,
): Promise<boolean> {
- let token: string | RefreshToken
-
if (isSelfhosted) {
- const token2 = await findRefreshTokenClickhouse(userId, refreshToken)
-
- console.log(token2)
+ const tokens = await findRefreshTokenClickhouse(userId, refreshToken)
+ return !_isEmpty(tokens)
}
- token = await this.userService.findRefreshToken(userId, refreshToken)
+ const token = await this.userService.findRefreshToken(userId, refreshToken)
return Boolean(token)
}
@@ -489,6 +486,11 @@ export class AuthService {
}
async logout(userId: string, refreshToken: string) {
+ if (isSelfhosted) {
+ await deleteRefreshTokenClickhouse(userId, refreshToken)
+ return
+ }
+
await this.userService.deleteRefreshToken(userId, refreshToken)
}
diff --git a/src/common/utils.ts b/src/common/utils.ts
index 348f4bd6a..8092959e9 100644
--- a/src/common/utils.ts
+++ b/src/common/utils.ts
@@ -234,6 +234,20 @@ const findRefreshTokenClickhouse = async (
return clickhouse.query(query, paramsData).toPromise()
}
+const deleteRefreshTokenClickhouse = async (
+ userId: string,
+ refreshToken: string,
+) => {
+ const paramsData = {
+ params: {
+ userId,
+ refreshToken,
+ },
+ }
+ const query = `ALTER table refresh_token DELETE WHERE userId = {userId:String} AND refreshToken = {refreshToken:String}`
+ return clickhouse.query(query, paramsData).toPromise()
+}
+
const generateRecoveryCode = () =>
randomstring.generate({
length: 30,
@@ -270,4 +284,5 @@ export {
getSelfhostedUUID,
saveRefreshTokenClickhouse,
findRefreshTokenClickhouse,
+ deleteRefreshTokenClickhouse,
}
diff --git a/src/project/project.controller.ts b/src/project/project.controller.ts
index 8ef6693dc..d94ecd5e5 100644
--- a/src/project/project.controller.ts
+++ b/src/project/project.controller.ts
@@ -13,7 +13,6 @@ import {
NotFoundException,
ForbiddenException,
Headers,
- NotImplementedException,
Patch,
HttpStatus,
ConflictException,