How can I get the request headers inside passport local strategy? I need a separate database for each entity, using mongodb so I need a way to get the subdomain before authentication in order to determine the database that I should connect to
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super({ usernameField: 'email' })
}
async validate(email: string, password: string, headers:Headers): Promise<IUser> {
//** this is what I want to have
//** const subdomain = headers.host.split(".")[0]
const user = await this.authService.validateUser({ email, password ,//** subdomain})
if (!user) {
throw new UnauthorizedException()
}
return user
}
}
You need to add passReqToCallback: true
to the super
call in the constructor. This will make req
the first parameter of the validate
so you can do something like
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super({ usernameField: 'email', passReqToCallback: true })
}
async validate(req: Request, email: string, password: string, headers:Headers): Promise<IUser> {
const subdomain = req.headers.host.split(".")[0];
const user = await this.authService.validateUser({ email, password ,//** subdomain})
if (!user) {
throw new UnauthorizedException()
}
return user
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With