All files / src/modules/auth auth.resolver.ts

53.12% Statements 34/64
66.03% Branches 35/53
57.89% Functions 11/19
52.45% Lines 32/61

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 11813x 13x 13x 13x 13x     13x 13x     22x 13x                   22x 13x                   22x 13x                   22x 13x                 22x 13x 1x 1x 1x 1x 1x 1x               1x     22x 13x                     22x 13x                               22x 13x           22x 13x                
import { Resolver, Mutation, Args, Context } from '@nestjs/graphql';
import { AuthService } from '@app/modules/auth/auth.service';
import { UseGuards } from '@nestjs/common';
import { GqlAuthGuard } from '@app/common/guards/gql-auth.guard';
import { BadRequestException } from '@nestjs/common';
 
@Resolver()
export class AuthResolver {
  constructor(private readonly auth: AuthService) {}
 
  @UseGuards(GqlAuthGuard)
  @Mutation(() => Boolean)
  async requestEmailChange(
    @Context() ctx: any,
    @Args('newEmail') newEmail: string
  ): Promise<boolean> {
    const userId = ctx.req.user.id;
    const meta = { ip: ctx.req.ip, userAgent: ctx.req.headers['user-agent'] };
    await this.auth.createEmailChangeToken(userId, newEmail, meta);
    return true;
  }
 
  @Mutation(() => Boolean)
  async confirmEmailChange(
    @Args('token') token: string,
    @Context() ctx: any
  ): Promise<boolean> {
    const meta = { ip: ctx.req.ip, userAgent: ctx.req.headers['user-agent'] };
    await this.auth.useEmailChangeToken(token, meta);
    return true;
  }
 
  @UseGuards(GqlAuthGuard)
  @Mutation(() => Boolean)
  async requestPhoneChange(
    @Context() ctx: any,
    @Args('newPhone') newPhone: string
  ): Promise<boolean> {
    const userId = ctx.req.user.id;
    const meta = { ip: ctx.req.ip, userAgent: ctx.req.headers['user-agent'] };
    await this.auth.createPhoneChangeToken(userId, newPhone, meta);
    return true;
  }
 
  @Mutation(() => Boolean)
  async confirmPhoneChange(
    @Args('token') token: string,
    @Context() ctx: any
  ): Promise<boolean> {
    const meta = { ip: ctx.req.ip, userAgent: ctx.req.headers['user-agent'] };
    await this.auth.usePhoneChangeToken(token, meta);
    return true;
  }
 
  @Mutation(() => String)
  async refreshToken(@Args('refreshToken', { nullable: true }) refreshToken: string, @Context() ctx: any): Promise<string> {
    const token = refreshToken || ctx.req.cookies?.refresh_token;
    Iif (!token) throw new BadRequestException('No refresh token provided');
    const meta = { ip: ctx.req.ip, userAgent: ctx.req.headers['user-agent'] };
    const result = await this.auth.refreshTokens(token, meta);
    if (ctx.res && result.refreshToken) {
      ctx.res.cookie('refresh_token', result.refreshToken, {
        httpOnly: true,
        secure: process.env.NODE_ENV === 'production',
        sameSite: 'lax',
        maxAge: 1000 * 60 * 60 * 24 * 14,
        path: '/auth/refresh',
      });
    }
    return result.accessToken;
  }
 
  @Mutation(() => Boolean)
  async logout(@Args('refreshToken', { nullable: true }) refreshToken: string, @Context() ctx: any): Promise<boolean> {
    const token = refreshToken || ctx.req.cookies?.refresh_token;
    Iif (token) {
      await this.auth.revokeRefreshToken(token);
    }
    Iif (ctx.res) {
      ctx.res.clearCookie('refresh_token', { path: '/auth/refresh' });
    }
    return true;
  }
 
  @Mutation(() => String)
  async googleLogin(@Args('idToken') idToken: string, @Context() ctx: any): Promise<string> {
    const meta = { ip: ctx.req.ip, userAgent: ctx.req.headers['user-agent'] };
    const result = await this.auth.loginWithGoogle(idToken, meta);
    Iif (ctx.res && result.refreshToken) {
      ctx.res.cookie('refresh_token', result.refreshToken, {
        httpOnly: true,
        secure: process.env.NODE_ENV === 'production',
        sameSite: 'lax',
        maxAge: 1000 * 60 * 60 * 24 * 14,
        path: '/auth/refresh',
      });
    }
    return result.accessToken;
  }
 
  @UseGuards(GqlAuthGuard)
  @Mutation(() => Boolean)
  async setPassword(@Args('newPassword') newPassword: string, @Context() ctx: any): Promise<boolean> {
    await this.auth.setPasswordForUser(ctx.req.user.id, newPassword);
    return true;
  }
 
  @UseGuards(GqlAuthGuard)
  @Mutation(() => Boolean)
  async changePassword(
    @Args('currentPassword') currentPassword: string,
    @Args('newPassword') newPassword: string,
    @Context() ctx: any,
  ): Promise<boolean> {
    await this.auth.changePasswordForUser(ctx.req.user.id, currentPassword, newPassword);
    return true;
  }
}