nuxt-community / auth-module

Zero-boilerplate authentication support for Nuxt 2
https://auth.nuxtjs.org
MIT License
1.93k stars 925 forks source link

Setting cookie: false is not respected #1436

Closed LukasHirt closed 2 years ago

LukasHirt commented 2 years ago

Version

module: 5.0.0-1624817847.21691f1 nuxt: 2.15.7

Nuxt configuration

mode:

Nuxt configuration

  auth: {
    cookie: false,
    redirect: {
      login: '/sign-in',
    },
    strategies: {
      local: {
        scheme: '@/schemes/auth-refresh-token',
        token: {
          property: 'accessToken',
          maxAge: 900,
          global: true,
          type: 'Bearer',
        },
        refreshToken: {
          property: 'refreshToken',
          data: 'refreshToken',
          maxAge: 60 * 60 * 24 * 30,
        },
        user: {
          property: false,
        },
        endpoints: {
          login: { url: '/auth/login', method: 'post' },
          refresh: { url: '/auth/refresh-token', method: 'post' },
          user: { url: '/users/profile', method: 'get' },
          logout: false,
        },
      },
    },
  },

Custom scheme

import { RefreshScheme, ExpiredAuthSessionError } from '@nuxtjs/auth-next/dist/runtime'

function cleanObj(obj) {
  for (const key in obj) {
    if (obj[key] === 0) {
      delete obj[key];
    }
  }
  return obj;
}

export default class CustomRefreshScheme extends RefreshScheme {
  async refreshTokens() {
    if (!this.options.endpoints.refresh) {
      return Promise.resolve();
    }

    if (!this.check().valid) {
      return Promise.resolve();
    }

    const refreshTokenStatus = this.refreshToken.status();

    if (refreshTokenStatus.expired()) {
      this.$auth.reset();

      throw new ExpiredAuthSessionError();
    }

    if (!this.options.refreshToken.tokenRequired) {
      this.requestHandler.clearHeader();
    }

    const endpoint = {
      data: {
        client_id: 0,
        grant_type: 0
      }
    };

    if (this.options.refreshToken.required && this.options.refreshToken.data) {
      endpoint.data[this.options.refreshToken.data] = this.refreshToken.get();
    }

    if (this.options.clientId) {
      endpoint.data.client_id = this.options.clientId;
    }

    if (this.options.grantType) {
      endpoint.data.grant_type = "refresh_token";
    }

    const userEmail = this.$auth.user.email

    if (userEmail) {
      endpoint.data.email = userEmail
    }

    cleanObj(endpoint.data);

    try {
      const response = await this.$auth.request(endpoint, this.options.endpoints.refresh);
      this.updateTokens(response, { isRefreshing: true });

      return response;
    } catch (error) {
      this.$auth.callOnError(error, { method: "refreshToken" });

      return await Promise.reject(error);
    }
  }
}

What is expected?

Cookie is not set

What is actually happening?

Auth cookies storing strategy, token, etc. are still being set

Steps to reproduce

Set cookie: false in the auth config.

Checklist

Intevel commented 2 years ago

Fixed in #1436

LukasHirt commented 2 years ago

Fixed in #1436

Thank you! Just correcting the PR number - #1442

Intevel commented 2 years ago

Oh sorry 😄

Intevel commented 2 years ago

1442 was merged, you can close this issue.