Search code examples
vue.jsnuxt.jsnuxt-auth

Using Nuxt-Auth with Cookie


I am attempting to get Nuxt.js to work with cookie authentication. I am using nuxt-auth with cookie setting. Laravel Backend with Passport.

The reason I need to use Cookies is because I plan on having the nuxt project be on my main domain name (with login) and then having app.mydomainname.com for the actual application. The main website has public facing pages that use authentication as well.

Here is my config for nuxt.js for nuxt-auth:

  auth: {
    local: false,
    redirect: {
      login: "/login",
      logout: "/login",
      callback: "/login",
      home: false,
    },
    strategies: {
      cookie: {
        token: {
          property: "data.access_token",
        },
        user: {
          property: "data",
        },
        endpoints: {
          login: {
            url: "v1/auth/login",
            method: "post",
            propertyName: "access_token",
          },
          logout: { url: "/v1/auth/logout", method: "delete" },
          user: { url: "/v1/settings", method: "get" },
        },
      },
    },
  },

Login works fine, but then the cookie does not set when I look in my editthiscookie chrome plugin, thus the call to /settings does not work:

FineMyCookie Chrome Plugin

As you see the cookie is just being set to true and not the access token.

Any help with the configuration would be helpful.

Thanks


Solution

  • Figured it out. I had to set set required: true and type: "Bearer" in the token config.

    So it looks like this now:

    auth: {
        redirect: {
          login: "/login",
          logout: "/login",
          callback: "/login",
          home: false,
        },
        strategies: {
          local: false,
          cookie: {
            token: {
              property: "data.access_token",
              required: true,
              type: "Bearer",
            },
            user: {
              property: "data",
            },
            endpoints: {
              login: {
                url: "v1/auth/login",
                method: "post",
              },
              logout: { url: "/v1/auth/logout", method: "delete" },
              user: { url: "/v1/settings", method: "get" },
            },
          },
        },
      },