Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

custom AuthProvider Error #38

Open
mickaeldupray opened this issue Feb 3, 2022 · 0 comments
Open

custom AuthProvider Error #38

mickaeldupray opened this issue Feb 3, 2022 · 0 comments

Comments

@mickaeldupray
Copy link

mickaeldupray commented Feb 3, 2022

Hello,
I got this everytime even I'm able to login and get token.

Uncaught (in promise) Error: Redirected when going from "/login" to "/dashboard" via a navigation guard.
createRouterError @ vue-router.esm.js?8c4f:2065
createNavigationRedirectedError @ vue-router.esm.js?8c4f:2024
eval @ vue-router.esm.js?8c4f:2376
eval @ admin.js?4914:362
.....

below my code:

import {
  LOGIN,
  LOGOUT,
  CHECK_AUTH,
  CHECK_ERROR,
  GET_NAME,
  GET_EMAIL,
  GET_PERMISSIONS,
} from "vuetify-admin/src/providers/auth/actions";

export default (httpClient, params = {}) => {
  params = {
    routes: {
      login: "/login/access-token",
      user: "/users/me",
    },
    storageKey: "token",
    getAuthHeaders(storageKey) {
      return {
        headers: {
          Authorization: `Bearer ${localStorage.getItem(storageKey)}`,
        },
      };
    },
    getCredentials: ({ username, password }) => {
      const form_data = new FormData();
      form_data.append("username", username);
      form_data.append("password", password);
      return form_data;
    },
    getName: (u) => u.username,
    getEmail: (u) => u.email,
    getToken: (u) => u.access_token,
    getPermissions: (u) => {
      if (u.is_superuser == true) {
        return ["admin", "reseller", "user"];
      }
      if (u.is_reseller == true) {
        return ["reseller", "user"];
      }
      return ["user"];
    },
    ...params,
  };

  let {
    routes,
    getCredentials,
    getName,
    getEmail,
    getPermissions,
    storageKey,
    getToken,
    getAuthHeaders,
  } = params;

  return {
    [LOGIN]: async ({ username, password }) => {
      let response = await httpClient.post(
        routes.login,
        getCredentials({ username, password })
      );

      if (response.status < 200 || response.status >= 300) {
        throw new Error(response.statusText);
      }
      localStorage.setItem(storageKey, getToken(response.data));
      //return Promise.resolve();
    },
    [LOGOUT]: async () => {
      localStorage.removeItem(storageKey);
      return Promise.resolve();
    },
    [CHECK_AUTH]: async () => {
      /**
       * Refresh token
       */
      if (routes.refresh) {
        let response = await httpClient.get(
          routes.refresh,
          getAuthHeaders(storageKey)
        );
        localStorage.setItem(storageKey, getToken(response.data));
      }

      /**
       * Get user infos
       */
      
      if (routes.user) {
        let response = await httpClient.get(
          routes.user,
          getAuthHeaders(storageKey)
        );

        if (response.status < 200 || response.status >= 300) {
          throw new Error(response.statusText);
        }

        return response.data;
      }

      return localStorage.getItem(storageKey)
        ? Promise.resolve({
            data: true,
          })
        : Promise.reject();
    },
    [CHECK_ERROR]: ({ status }) => {
      if (status === 401 || status === 403) {
        return Promise.reject();
      }
      return Promise.resolve();
    },
    [GET_NAME]: (user) => getName(user),
    [GET_EMAIL]: (user) => getEmail(user),
    [GET_PERMISSIONS]: (user) => getPermissions(user),
  };
};

Admin.js

const baseURL = process.env.VUE_APP_API_URL || "http://localhost:8001/api/v1";

const configHeaders = {
  "content-type": "application/x-www-form-urlencoded",
  // eslint-disable-next-line prettier/prettier
  "Accept": "application/json"
};

const http = axios.create({
  baseURL,
  headers: configHeaders,
});

/**
 * Init admin
 */
export default new VuetifyAdmin({
  router,
  store,
  i18n,
  title: "Vuetify Admin",
  routes,
  locales: {
    en,
    fr,
  },
  translations: ["en", "fr"],
  authProvider: authProvider(http),
  resources,
  options: {
    dateFormat: "long",
  },
});
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant