@nestjs/passport#AuthGuard TypeScript Examples

The following examples show how to use @nestjs/passport#AuthGuard. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ft-auth.guard.ts    From 42_checkIn with GNU General Public License v3.0 7 votes vote down vote up
@Injectable()
export class FtAuthGuard extends AuthGuard('42') {
  handleRequest(err, user, info, context: ExecutionContext) {
    if (err || !user) {
      const res = context.switchToHttp().getResponse();
      return res.redirect('/');
    }
    return user;
  }
}
Example #2
Source File: google-auth.guard.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
@Injectable()
export default class GoogleAuthGuard extends AuthGuard('google') {
  async canActivate(context: ExecutionContext): Promise<boolean | never> {
    const can = await super.canActivate(context);

    if (can) {
      const request = context.switchToHttp().getRequest();

      await super.logIn(request);
    }

    return true;
  }

  static getRequest(context: ExecutionContext) {
    return context.switchToHttp().getRequest();
  }
}
Example #3
Source File: global-search.controller.ts    From barista with Apache License 2.0 6 votes vote down vote up
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@Controller('global-search')
@ApiTags('GlobalSearch')
export class GlobalSearchController {
  constructor(private globalSearchService: GlobalSearchService) {}

  @ApiResponse({
    status: 200,
    type: ModuleSearchParentRecordDto,
    isArray: true,
  })
  @Get('/search-module-1/:modulePartialName/:page/:pageSize')
  async searchForModuleStep1(
    @Param('modulePartialName') modulePartialName: string,
    @Param('page') page: number = 0,
    @Param('pageSize') pageSize: number = 50,
  ): Promise<GetManyDefaultResponse<ModuleSearchParentRecordDto>> {
    return await this.globalSearchService.searchForModule(modulePartialName, page, pageSize);
  }

  @ApiResponse({
    status: 200,
    type: ModuleSearchChildRecordDto,
    isArray: true,
  })
  @Get('/search-module-2/:projectId/:modulePartialName/:page/:pageSize')
  async searchForModuleStep2(
    @Param('projectId') projectId: number,
    @Param('modulePartialName') modulePartialName: string,
    @Param('page') page: number = 0,
    @Param('pageSize') pageSize: number = 50,
  ): Promise<GetManyDefaultResponse<ModuleSearchChildRecordDto>> {
    return await this.globalSearchService.searchForModuleFromProject(projectId, modulePartialName, page, pageSize);
  }
}
Example #4
Source File: gql.auth-guard.ts    From svvs with MIT License 6 votes vote down vote up
/**
 * GqlAuthGuard translate GqlExecutionContext request => UseGuard
 *
 */
@Injectable()
export class GqlAuthGuard extends AuthGuard('jwt') {
  /**
   * getRequest return ExecutionContext as GqlExecutionContext request
   *
   * @param context
   */
  getRequest(context: ExecutionContext) {
    const ctx = GqlExecutionContext.create(context)

    return ctx.getContext().req
  }
}
Example #5
Source File: auth.controller.ts    From amplication with Apache License 2.0 6 votes vote down vote up
@UseInterceptors(MorganInterceptor('combined'))
  @UseFilters(GithubAuthExceptionFilter)
  @Get('/github/callback')
  @UseGuards(AuthGuard('github'))
  async githubCallback(@Req() request: Request, @Res() response: Response) {
    const user: AuthUser = request.user as AuthUser;
    this.logger.log({
      level: 'info',
      message: `receive login callback from github account_id=${user.account.id}`
    });
    const token = await this.authService.prepareToken(user);
    response.redirect(301, `${this.host}?token=${token}`);
  }
Example #6
Source File: firebase-auth.guard.ts    From aqualink-app with MIT License 6 votes vote down vote up
@Injectable()
export class FirebaseAuthGuard extends AuthGuard('custom') {
  constructor(private reflector: Reflector) {
    super();
  }

  canActivate(context: ExecutionContext) {
    const isPublic = this.reflector.get<boolean>(
      'isPublic',
      context.getHandler(),
    );

    if (isPublic) {
      return true;
    }

    return super.canActivate(context);
  }
}
Example #7
Source File: jwt-auth.guard.ts    From codeclannigeria-backend with MIT License 6 votes vote down vote up
@Injectable()
export class JwtAuthGuard extends AuthGuard(AUTH_GUARD_TYPE) {
  canActivate(
    context: ExecutionContext
  ): boolean | Promise<boolean> | Observable<boolean> {
    // console.log(context.switchToHttp().getRequest().sessionID);
    return configuration().isAuthEnabled ? super.canActivate(context) : true;
  }
}
Example #8
Source File: jwt-auth.guard.ts    From nestjs-rest-sample with GNU General Public License v3.0 6 votes vote down vote up
@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    // Add your custom authentication logic here
    // for example, call super.logIn(request) to establish a session.
    return super.canActivate(context);
  }

  handleRequest(err, user, info) {
    // You can throw an exception based on either "info" or "err" arguments
    if (err || !user) {
      throw err || new UnauthorizedException();
    }
    return user;
  }
}
Example #9
Source File: jwt-auth.guard.ts    From nestjs-starter-rest-api with MIT License 6 votes vote down vote up
@Injectable()
export class JwtAuthGuard extends AuthGuard(STRATEGY_JWT_AUTH) {
  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    // Add your custom authentication logic here
    // for example, call super.logIn(request) to establish a session.
    return super.canActivate(context);
  }

  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  handleRequest(err, user, info) {
    // You can throw an exception based on either "info" or "err" arguments
    if (err || !user) {
      throw err || new UnauthorizedException(`${info}`);
    }
    return user;
  }
}
Example #10
Source File: auth.controller.ts    From Phantom with MIT License 6 votes vote down vote up
@Get('google/redirect')
  @UseGuards(AuthGuard('google'))
  async googleAuthRedirect(@Req() req, @Res() res) {
    const data = await this.authService.googleLogin(req);
    if (data) {
      res.redirect(
        process.env.FRONT_BASE_URL +
          '/aouth/google?token=' +
          data.token +
          '&type=' +
          data.type,
      );
    } else {
      throw new NotFoundException();
    }
  }
Example #11
Source File: gql.auth.guard.ts    From api with GNU Affero General Public License v3.0 6 votes vote down vote up
@Injectable()
export class GqlAuthGuard extends AuthGuard('jwt') {
  getRequest(context: ExecutionContext) {
    if (context.getType<any>() === 'graphql') {
      const ctx = GqlExecutionContext.create(context);

      if (!ctx.getContext().cache) {
        ctx.getContext().cache = new ContextCache()
      }
      return ctx.getContext().req;
    }

    return context.switchToHttp().getRequest()
  }

  handleRequest<T = UserEntity>(err, user: T): T {
    if (err) {
      throw new Error('invalid token')
    }

    return user
  }
}
Example #12
Source File: auth.controller.ts    From MyAPI with MIT License 6 votes vote down vote up
@ApiOperation({ summary: 'Validate authentication token' })
  @ApiOkResponse({ description: 'Authentication token is valid' })
  @ApiUnauthorizedResponse({ description: 'The authentication token is invalid' })
  @UseGuards(AuthGuard())
  @ApiBearerAuth()
  @Get()
  @HttpCode(HttpStatus.OK)
  // eslint-disable-next-line @typescript-eslint/no-empty-function
  isAuthenticated(): void { }
Example #13
Source File: jwt-auth.guard.ts    From 42_checkIn with GNU General Public License v3.0 5 votes vote down vote up
@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {}
Example #14
Source File: jwt-auth.guard.ts    From NestJs-youtube with MIT License 5 votes vote down vote up
@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {}
Example #15
Source File: jwt-access.guard.ts    From nest-js-boilerplate with MIT License 5 votes vote down vote up
@Injectable()
export default class JwtAccessGuard extends AuthGuard('accessToken') {}