Closed viniciusgati closed 5 years ago
@viniciusgati can you provide a basic repo to reproduce this?
Here it goes @johnbiundo thanks for your help.
I took a look at your repo, and there a few things wrong.
First, you need an auth service. See https://docs.nestjs.com/techniques/authentication#implementing-passport-strategies. Often times an auth.service
would inject a UsersModule dependency, as to resolve authentication against users. You don't have a user service in your project, so we'll skip that detail.
Second, as far as the AuthController is concerned, you'll need to fix up your auth.module
. Remove your controller line, and include your newly created AuthService
to your providers list: providers: [AuthService]
. Remove the controller injection in the module.
You'll want to use the @AuthGuard
on any controller route in your project that you want guarded, and then your AuthService will handle the logic (often times the AuthService
would check against a UserService:authCheck
kind of method). You'll need to create a users module if you want to go that route, but you can always authenticate against anything else, not just users, so we'll keep this advice generic for now.
the auto-import used
import { Strategy } from 'passport-strategy';
instead of
import { Strategy } from 'passport-local';
@viniciusgati You are a god amongst men. Thank you sir!!
I am facing the same issue. I tried to replicate the example in a simple form though. It seems the AuthGuard isn't doing anything at all. Also using a custom guard isn't working too.
Auth.module.ts
import { Module } from "@nestjs/common";
import { PassportModule } from "@nestjs/passport";
import { SessionSerializer } from "./serializer/serializer";
import { LocalStrategy } from './strategies/local.strategy'
@Module({
imports: [PassportModule],
providers: [LocalStrategy, SessionSerializer],
exports: [PassportModule]
})
export class AuthModule {
}
LocalStrategy.ts
import { Strategy, IStrategyOptions } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport'
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy, 'local') {
constructor() {
const options: IStrategyOptions = {
session: true
}
super(options)
}
async validate(username: string, password: string) {
const user = "vansham"
console.log("VERIFY CALLBACK >>>")
if (username !== user)
throw new HttpException("wrong username ", HttpStatus.UNAUTHORIZED);
return user
}
}
Login Guard
import { ExecutionContext, Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport'
@Injectable()
export class LoginGuard extends AuthGuard('local') {
async canActivate(context: ExecutionContext) {
console.log("LOGIN GUARD ACTIVTAED >> ")
const result = (await super.canActivate(context)) as boolean;
console.log("LOGIN GUARD RESULT >>", result)
const request = context.switchToHttp().getRequest();
await super.logIn(request)
console.log("LOGIN GUARD RESULT >>", result)
return result;
}
}
app.controller.ts
import { Controller, Get, Post, Req, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { Request } from 'express';
import { LoginGuard } from 'src/Guards/loginGuard';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get('root.get')
getHello(): string {
return this.appService.getHello();
}
@UseGuards(LoginGuard)
@Post('login')
login(@Req() req: Request) {
console.log("LOGIN ROUTE", req.body)
return "HI"
}
}
It just returns me unauthorized. It's a struggle everyday with nest. SMH
@vansham-gortnm if you're looking for support, I suggest you try out Discord channel. The authentication setup in the docs has successfully worked for others, so I do not believe this to be a bug.
In my case auto import used import { Strategy } from 'passport';
instead of
import { ExtractJwt, Strategy } from 'passport-jwt';
Bug Report
Current behavior
Following the examples from documentation for authentication i got 2 problems.
I solved this with:
instead of
and doin a request for this controlle with login and password throws:
This error i am not being able to solve.
Expected behavior
Possible Solution
Environment