Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change return req.user object name of passport-local?

I'm developing a backend using nestjs and passport-local strategy. I use local strategy only for owners login in my projects. but at end of validation it returns the owner in req.user.

how can I change it so it returns the owner in req.owner?

import { Injectable } from '@nestjs/common';
import { OwnerService } from '../owner/owner.service';

@Injectable()
export class AuthService {
  constructor(private ownerService: OwnerService) {}

  async validateOwner(username: string, pass: string): Promise<any> {
    const owner = await this.ownerService.findOneByUsername(username);

    // later check with hashed pass
    if (owner && owner.owner && owner.owner.password === pass) {
      const { password, ...result } = owner.owner;
      return result;
    }
    return null;
  }
}

and

import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, HttpException } from '@nestjs/common';
import { AuthService } from './auth.service';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private authService: AuthService) {
    super({
      usernameField: 'username',
      passwordField: 'password',
    });
  }

  async validate(username: string, password: string): Promise<any> {
    const owner = await this.authService.validateOwner(username, password);
    if (!owner) {
      throw new HttpException('No Owner found', 404);
    }
    return owner;
  }
}

how I use it:

 @UseGuards(AuthGuard('local'))
  @Post('login')
  async login(@Request() req) {
    console.log(req.owner, req.user);
    return req.owner;
  }

req.owner is empty but req.user has value

UPDATE:

my Authmodule.ts:

import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { OwnerModule } from '../owner/owner.module';
import { LocalStrategy } from './local.strategy';
import { PassportModule } from '@nestjs/passport';
import { JwtModule } from '@nestjs/jwt';

@Module({
  providers: [AuthService, LocalStrategy],
  imports: [
    OwnerModule,
    PassportModule.register({
      property: 'owner',
    })
  ],
  exports: [AuthService],
})
export class AuthModule {}
like image 274
arianpress Avatar asked Oct 25 '25 18:10

arianpress


2 Answers

I created a LocalAuthGuard class that extends my local strategy:

import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class LocalAuthGuard extends AuthGuard('local') {
  constructor() {
    super({
      property: 'owner',
    });
  }
}

then wherever I use this guard, it adds my target field to req.owner instead of req.user.

  @UseGuards(LocalAuthGuard)
  @Post('login')
  async login(@Request() req): Promise<LoginResponse> {
    return this.authService.login(req.owner);
  }
like image 155
arianpress Avatar answered Oct 28 '25 04:10

arianpress


PassportModule.register({ property: 'owner' })

where PassportModule is imported from @nestjs/passport

like image 21
Micael Levi Avatar answered Oct 28 '25 03:10

Micael Levi