refactor: simplify backend and guard boundaries #42
@@ -12,7 +12,7 @@ import { HttpErrorCode } from '../../../common/error-code';
|
||||
import { isPrismaErrorCode, PrismaErrorCode } from '../../../prisma/prisma-error-code';
|
||||
import { PrismaService } from '../../../prisma/prisma.service';
|
||||
import { AuthErrorCode } from './auth-error-code';
|
||||
import { AuthUser, RefreshPayload } from './auth.types';
|
||||
import { AuthenticationResult, AuthUser, RefreshPayload } from './auth.types';
|
||||
|
||||
const EXPECTED_ROTATED_SESSIONS = 1;
|
||||
|
||||
@@ -35,7 +35,7 @@ export class AuthService {
|
||||
async register(
|
||||
email: string,
|
||||
password: string,
|
||||
): Promise<{ accessToken: string; refreshToken: string; user: AuthUser }> {
|
||||
): Promise<AuthenticationResult> {
|
||||
const existing = await this.prisma.user.findUnique({
|
||||
where: { email },
|
||||
});
|
||||
@@ -64,7 +64,7 @@ export class AuthService {
|
||||
async login(
|
||||
email: string,
|
||||
password: string,
|
||||
): Promise<{ accessToken: string; refreshToken: string; user: AuthUser }> {
|
||||
): Promise<AuthenticationResult> {
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { email },
|
||||
});
|
||||
@@ -80,7 +80,7 @@ export class AuthService {
|
||||
|
||||
async refresh(
|
||||
token: string,
|
||||
): Promise<{ accessToken: string; refreshToken: string; user: AuthUser }> {
|
||||
): Promise<AuthenticationResult> {
|
||||
let payload: RefreshPayload;
|
||||
try {
|
||||
payload = await this.jwt.verifyAsync<RefreshPayload>(token, {
|
||||
@@ -151,7 +151,7 @@ export class AuthService {
|
||||
|
||||
private async createSession(
|
||||
user: User,
|
||||
): Promise<{ accessToken: string; refreshToken: string; user: AuthUser }> {
|
||||
): Promise<AuthenticationResult> {
|
||||
const session = await this.prisma.session.create({
|
||||
data: {
|
||||
id: randomUUID(),
|
||||
@@ -166,7 +166,7 @@ export class AuthService {
|
||||
private async rotateSession(
|
||||
session: Session & { user: User },
|
||||
expectedRefreshTokenHash?: string,
|
||||
): Promise<{ accessToken: string; refreshToken: string; user: AuthUser }> {
|
||||
): Promise<AuthenticationResult> {
|
||||
const refreshToken = await this.jwt.signAsync(
|
||||
{
|
||||
sub: session.userId,
|
||||
|
||||
@@ -3,6 +3,12 @@ export interface AuthUser {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export type AuthenticationResult = {
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
user: AuthUser;
|
||||
};
|
||||
|
||||
export interface AccessPayload {
|
||||
sid: string;
|
||||
sub: string;
|
||||
|
||||
@@ -23,7 +23,7 @@ import {
|
||||
import { ApiProblemResponses } from '../../../common/decorators/api-safe-error-response.decorator';
|
||||
import { HttpErrorCode } from '../../../common/error-code';
|
||||
import { AuthService } from '../application/auth.service';
|
||||
import { AccessPayload } from '../application/auth.types';
|
||||
import { AccessPayload, AuthenticationResult } from '../application/auth.types';
|
||||
import { CredentialsDto } from './auth.dto';
|
||||
import { JwtAuthGuard } from './jwt-auth.guard';
|
||||
|
||||
@@ -117,11 +117,7 @@ export class AuthController {
|
||||
|
||||
private respond(
|
||||
response: Response,
|
||||
result: {
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
user: { email: string; id: string };
|
||||
},
|
||||
result: AuthenticationResult,
|
||||
) {
|
||||
response.cookie(REFRESH_COOKIE, result.refreshToken, this.cookieOptions());
|
||||
return { user: result.user, accessToken: result.accessToken };
|
||||
|
||||
Reference in New Issue
Block a user