question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Basic authentication from documentation does not work

See original GitHub issue

Bug Report

Current behavior

Following the examples from documentation for authentication i got 2 problems.

  1. Passport returned:
[Nest] 27924   - 2019-11-14 15:16:06   [ExceptionHandler] Authentication strategies must have a name +3ms
Error: Authentication strategies must have a name
    at Authenticator.use (C:\Users\Vinicius\Code\dms-2\node_modules\passport\lib\authenticator.js:56:22)
    at new MixinStrategy (C:\Users\Vinicius\Code\dms-2\node_modules\@nestjs\passport\dist\passport\passport.strategy.js:35:26)
    at new LocalStrategy (C:\Users\Vinicius\Code\dms-2\dist\shared\auth\local.strategy.js:18:9)
    at Injector.instantiateClass (C:\Users\Vinicius\Code\dms-2\node_modules\@nestjs\core\injector\injector.js:277:19)
    at callback (C:\Users\Vinicius\Code\dms-2\node_modules\@nestjs\core\injector\injector.js:74:41)
    at process._tickCallback (internal/process/next_tick.js:68:7)

I solved this with:

export class LocalStrategy extends PassportStrategy(Strategy, "local") {

instead of

export class LocalStrategy extends PassportStrategy(Strategy) {
  1. having the controller like:
import { Controller, Request, UseGuards, Post } from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport";

@Controller("auth")
export class AuthController {
  @UseGuards(AuthGuard("local"))
  @Post("login")
  async login(@Request() req) {
    return req.user;
  }
}

and doin a request for this controlle with login and password throws:

[Nest] 5348   - 2019-11-14 15:18:36   [ExceptionsHandler] Strategy#authenticate must be overridden by subclass +38758ms
Error: Strategy#authenticate must be overridden by subclass
    at LocalStrategy.Strategy.authenticate (C:\Users\Vinicius\Code\dms-2\node_modules\passport-strategy\lib\strategy.js:21:9)
    at attempt (C:\Users\Vinicius\Code\dms-2\node_modules\passport\lib\middleware\authenticate.js:361:16)
    at authenticate (C:\Users\Vinicius\Code\dms-2\node_modules\passport\lib\middleware\authenticate.js:362:7)
    at Promise (C:\Users\Vinicius\Code\dms-2\node_modules\@nestjs\passport\dist\auth.guard.js:83:3)
    at new Promise (<anonymous>)
    at C:\Users\Vinicius\Code\dms-2\node_modules\@nestjs\passport\dist\auth.guard.js:75:83
    at MixinAuthGuard.<anonymous> (C:\Users\Vinicius\Code\dms-2\node_modules\@nestjs\passport\dist\auth.guard.js:47:36)
    at Generator.next (<anonymous>)
    at C:\Users\Vinicius\Code\dms-2\node_modules\@nestjs\passport\dist\auth.guard.js:19:71
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\Vinicius\Code\dms-2\node_modules\@nestjs\passport\dist\auth.guard.js:15:12)
    at MixinAuthGuard.canActivate (C:\Users\Vinicius\Code\dms-2\node_modules\@nestjs\passport\dist\auth.guard.js:40:20)
    at GuardsConsumer.tryActivate (C:\Users\Vinicius\Code\dms-2\node_modules\@nestjs\core\guards\guards-consumer.js:14:34)
    at canActivateFn (C:\Users\Vinicius\Code\dms-2\node_modules\@nestjs\core\router\router-execution-context.js:132:59)
    at C:\Users\Vinicius\Code\dms-2\node_modules\@nestjs\core\router\router-execution-context.js:41:37
    at C:\Users\Vinicius\Code\dms-2\node_modules\@nestjs\core\router\router-proxy.js:8:23

This error i am not being able to solve.

Expected behavior

Possible Solution

Environment


Nest version: 6.7.2
 
For Tooling issues:
- Node version: 10.13.0
- Platform:  Windows

Others:
using yarn

dependencies:
```
    "@nestjs/common": "^6.7.2",
    "@nestjs/core": "^6.7.2",
    "@nestjs/mongoose": "^6.1.2",
    "@nestjs/passport": "^6.1.0",
    "@nestjs/platform-express": "^6.7.2",
    "class-transformer": "^0.2.3",
    "class-validator": "^0.11.0",
    "dotenv": "^8.2.0",
    "express-rate-limit": "^5.0.0",
    "helmet": "^3.21.2",
    "mongoose": "^5.7.10",
    "passport": "^0.4.0",
    "passport-http-bearer": "^1.0.1",
    "passport-local": "^1.0.0",
    "redis": "^2.8.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.0",
    "rxjs": "^6.5.3",
    "simple-node-logger": "^18.12.23"
```


Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

45reactions
viniciusgaticommented, Nov 16, 2019

the auto-import used

import { Strategy } from ‘passport-strategy’;

instead of

import { Strategy } from ‘passport-local’;

1reaction
primary-bytecommented, Mar 3, 2021

@viniciusgati You are a god amongst men. Thank you sir!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deprecation of Basic authentication in Exchange Online
The username/password isn't sent to the service using Basic, but the Basic Auth header is required to send the session's OAuth token, because ......
Read more >
Basic Authentication - Swagger
Basic authentication is a simple authentication scheme built into the HTTP protocol. The client sends HTTP requests with the Authorization header that ...
Read more >
BasicAuthentication policy runtime error troubleshooting
Possible causes. The input source variable to BasicAuthentication Policy is not a valid Base64 encoded string. The header containing the Base64 encoded string ......
Read more >
Basic Authentication Troubleshooting Guide | Atlassian Support
Ensure the username used in the "Incoming Authentication" and "Outgoing Authentication" do not have spaces. Problems caused by using Basic ...
Read more >
Solved: Basic Authentication is not working
The preferred authentication scheme is OAuth1 or OAuth2, using tokens rather than user names and passwords. The documentation for this can be found...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found