Nuxtjs auth module - token endpoint in auth strategies configuration is never called

623 Views Asked by At

We have two endpoints /auth and /token. The endpoint /auth returns the authorization code that can be used when calling /token to get an access token.

Using NuxtJS the auth module became the way to go. The login process this.$auth.loginWith("company") is working fine as far as I know. I get redirected to the Login Page. I can enter my credentials and when those are valid I get redirected to the configured URL.

Up to that point everything works as expected. The redirect is passing the authorization code as request parameters.

This is what the URL looks like:

http://localhost:3000/?state=Y6CWcCZanJ&session_state=2c966cd9-5834-4045-9bfb-6aa9f616f841&code=fbabf615-cd5e-4479-818a-6a7ba72de01b.2c966cd9-5834-4045-9bfb-6aa9f616f841.553d562b-c454-4681-83ae-98cd93dbfa90

However with this code I expect that the auth module is automatically calling the /token endpoint. But it does not. Why is that?

Do I need to call it explicitly after using this.$auth.loginWith("company")? Something like:

this.$auth.loginWith("company");
this.$auth.fetchToken();

Or is it done implictly?

This is the configuration in nuxt.config.js

...
  auth: {
    strategies: {
      company: {
        scheme: "oauth2",
        endpoints: {
          authorization:
            "https://login.mycompany.com/auth/realms/apps/protocol/openid-connect/auth",
          token:
            "https://login.mycompany.com/auth/realms/apps/protocol/openid-connect/token",
          userInfo:
            "https://login.mycompany.com/auth/realms/apps/protocol/openid-connect/userinfo",
          logout: "http://localhost:3000/logout"
        },
        token: {
          name: "Authorization",
          property: "access_token",
          type: "Bearer",
          maxAge: 1800
        },
        refreshToken: {
          property: "refresh_token",
          maxAge: 60 * 60 * 24 * 30
        },
        responseType: "code",
        grantType: "authorization_code",
        accessType: undefined,
        redirectUri: "http://localhost:3000",
        logoutRedirectUri: undefined,
        clientId:
          process.env.CLIENT_ID ||
          "3004761-241-dab74c5e-ad70-11eb-bea4-4193bd361dc612123",
        scope: ["all"],
        codeChallengeMethod: "S256"
      }
    }
  },
...
1

There are 1 best solutions below

0
On

Is there any chance you forgot to setup @nuxtjs/axios or explicitly configure auth middleware?

// nuxt.config.js
modules: ['@nuxtjs/auth-next', '@nuxtjs/axios'],

router: {
  middleware: ['auth'],
},