feat(frontend): manage API tokens in Frontend

This commit is contained in:
Julian Tölle 2023-02-20 23:50:57 +01:00
parent d0ca2b967e
commit ac0f9ff5d3
13 changed files with 484 additions and 33 deletions

View file

@ -1,6 +1,7 @@
import { ForbiddenException, Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { JwtService } from "@nestjs/jwt";
import { randomBytes } from "crypto";
import { User } from "../users/user.entity";
import { UsersService } from "../users/users.service";
import { ApiToken } from "./api-token.entity";
@ -8,7 +9,6 @@ import { ApiTokenRepository } from "./api-token.repository";
import { AuthSession } from "./auth-session.entity";
import { AuthSessionRepository } from "./auth-session.repository";
import { LoginDto } from "./dto/login.dto";
import { randomBytes } from "crypto";
@Injectable()
export class AuthService {
@ -129,11 +129,13 @@ export class AuthService {
return this.apiTokenRepository.scoped.byUser(user).getMany();
}
async revokeApiToken(token: string): Promise<void> {
const apiToken = await this.findApiToken(token);
async revokeApiToken(user: User, id: string): Promise<void> {
const apiToken = await this.apiTokenRepository.findOneBy({ user, id });
apiToken.revokedAt = new Date();
await this.apiTokenRepository.save(apiToken);
if (apiToken && apiToken.revokedAt == null) {
apiToken.revokedAt = new Date();
await this.apiTokenRepository.save(apiToken);
}
return;
}