Login method is not implemented by jwt scheme - Oauth2


#1

Following the tutoiral here to implement auth via facebook credentials.

In the callback endpoint , the following line fails with an error message

await auth.login(user)

code:“E_INVALID_METHOD”

message:“E_INVALID_METHOD: login method is not implemented by jwt scheme\n> More details: https://err.sh/adonisjs/errors/E_INVALID_METHOD

name:“RuntimeException”

stack:“RuntimeException: E_INVALID_METHOD: login method is not implemented by jwt scheme\n> More details: https://err.sh/adonisjs/errors/E_INVALID_METHOD\n at Function.invoke (node_modules/@adonisjs/auth/node_modules/@adonisjs/generic-exceptions/src/RuntimeException.js:102:12)\n at JwtScheme.BaseScheme.(anonymous function) [as login] (node_modules/@adonisjs/auth/src/Schemes/Base.js:281:31)\n at LoginController.callback (app/Controllers/Http/LoginController.js:38:18)”

status:500

Can someone please let me know what’s going on wrong?


#2

+1, same one’s here.


#3

Hey @jay-branchr! :wave:

Could you please share some code?


#4

@romain.lanz

I followed the instructions given in the adonijs API documentation page. I noticed the callback is invoked. Below is the code i have in the callback method.

const fbUser = await ally.driver('facebook').fields(['email']).getUser();
/* a valid user is returned back. for testing purpose i hardcoded the email value */
const user = await User.findOrCreate(whereClause, {email:'xxx@yyy.com'}); 
await auth.login(user); // failed to authenticate

Tried using instead of login method above but no luck

await auth.loginViaId(user.user_id);


#5

There is nothing called login when you are using JWT. You generate a token and save it on client to authenticate requests.

What you need is auth.generate(user)


#6

Thanks, @virk. I will try it out.


#7

@virk

I get the following error message when i tried to redirect to a protected URL after using auth.generate(user)

E_INVALID_JWT_TOKEN: jwt must be provided.

request.request.headers[‘authorization’] = bearer ${fbUser.token};

response.redirect(’/index’);

While debugging I observed that

line #378 const token = this.getAuthHeader() in jwt.js file always returns a null. For some reason the authorization token set in the header is not available. I tried setting request.headers['authorization'] too, but no luck.


#8

Why are you setting the header on the server? The client who is making the request sends the token to the server


#9

@virk

I’m using JWT auth mechanism in my project and one of the login source is to login via facebook credentials.

Below are the steps I’ve implemented

  1. Authentication via from facebook API . The Ally-Facebook API works fine.

  2. As you recommended I used auth.generate(user) to generate the token for the authenticated user.

  3. When i try to redirect to a protected url - response.redirect(’/index’).

    • A simple redirect without the header setting threw an E_INVALID_JWT_TOKEN: jwt must be provided. error.
    • To solve this error I tried adding the authorization in the header
  4. Routes.js has the following configuration

     .get('index', 'UserController.index')
     .middleware('auth') ```
    
  5. kernel.js has the following middleware configured

const globalMiddleware = [
'Adonis/Middleware/BodyParser',
'Adonis/Middleware/AuthInit',
'Adonis/Middleware/Cors'
]```

```const namedMiddleware = {
auth: 'Adonis/Middleware/Auth'
}

Server
.registerGlobal(globalMiddleware)
.registerNamed(namedMiddleware)
.use(['Adonis/Middleware/Static'])

Is there anything wrong in my flow, please let me know.