api: add /check-auth method for account mode
This commit is contained in:
parent
8d2d3a12a9
commit
b8841e52b7
@ -158,4 +158,18 @@ export class AccountController implements BaseAuthController {
|
||||
|
||||
return reply.send(account);
|
||||
}
|
||||
|
||||
@Get('/check-auth')
|
||||
@ApiResponse({
|
||||
status: HttpStatus.OK,
|
||||
})
|
||||
async checkAuth(@AuthToken() token: string, @Res() reply: FastifyReply) {
|
||||
const { authId } = await this.accountService.parseToken(token, { ignoreExpiration: true });
|
||||
|
||||
if (authId) return reply.status(HttpStatus.UNAUTHORIZED).send();
|
||||
|
||||
const user = await this.accountService.getUser(token, { ignoreExpiration: true });
|
||||
|
||||
return reply.status(200).send(user);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ import type { CreateAccountDto } from './dto/create-account.dto';
|
||||
import type { ResetPasswordDto } from './dto/reset-password.dto';
|
||||
import type { UpdateAccountDto } from './dto/update-account.dto';
|
||||
import { BadRequestException, Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import type { JwtVerifyOptions } from '@nestjs/jwt';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
@ -112,9 +113,9 @@ export class AccountService {
|
||||
}
|
||||
}
|
||||
|
||||
public async getUser(token: string) {
|
||||
public async getUser(token: string, options?: JwtVerifyOptions) {
|
||||
try {
|
||||
const { username } = this.jwtService.verify(token) as TokenPayload;
|
||||
const { username } = this.jwtService.verify<DecodedToken>(token, options);
|
||||
|
||||
return this.accountModel.findOne({
|
||||
username,
|
||||
@ -123,4 +124,12 @@ export class AccountService {
|
||||
throw new UnauthorizedException('Invalid token');
|
||||
}
|
||||
}
|
||||
|
||||
public async parseToken(token: string, options?: JwtVerifyOptions) {
|
||||
try {
|
||||
return this.jwtService.verify<TokenPayload>(token, options);
|
||||
} catch (error) {
|
||||
throw new UnauthorizedException(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user