Added Local Storage Based Login Method while in Development Environment
This commit is contained in:
parent
882e3ac33d
commit
f32e46efc3
|
|
@ -2,6 +2,9 @@ import { useAuth } from "@/stores/Auth/auth.store";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { storeToRefs } from "pinia";
|
import { storeToRefs } from "pinia";
|
||||||
import { auth } from "./endpoints";
|
import { auth } from "./endpoints";
|
||||||
|
import { isDevelopment } from "../utils/common.util";
|
||||||
|
import jwtServices from "./jwt";
|
||||||
|
import router from "@/router";
|
||||||
|
|
||||||
const apiURL: string = DOLPHIN.config.API_BASE_URL;
|
const apiURL: string = DOLPHIN.config.API_BASE_URL;
|
||||||
|
|
||||||
|
|
@ -13,9 +16,14 @@ const api = axios.create({
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// No need to save the Token as they are already attached to the cookie from backend
|
|
||||||
api.interceptors.request.use(
|
api.interceptors.request.use(
|
||||||
function (config) {
|
function (config) {
|
||||||
|
if (isDevelopment()) {
|
||||||
|
const token = jwtServices.getToken();
|
||||||
|
if (token) {
|
||||||
|
config.headers["Authorization"] = `Bearer ${token}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
return config;
|
return config;
|
||||||
},
|
},
|
||||||
function (error) {
|
function (error) {
|
||||||
|
|
@ -33,13 +41,28 @@ api.interceptors.response.use(
|
||||||
response: { status },
|
response: { status },
|
||||||
} = error;
|
} = error;
|
||||||
const originalRequest = config;
|
const originalRequest = config;
|
||||||
if (status === 401 && !originalRequest._retry) {
|
|
||||||
originalRequest._retry = true;
|
|
||||||
const authStore = useAuth();
|
const authStore = useAuth();
|
||||||
const { isAuthenticated } = storeToRefs(authStore);
|
const { isAuthenticated } = storeToRefs(authStore);
|
||||||
|
if (status === 401 && !originalRequest._retry) {
|
||||||
|
originalRequest._retry = true;
|
||||||
try {
|
try {
|
||||||
await api.post(auth.tokenRefresh, null, {
|
let data = {};
|
||||||
|
if (isDevelopment()) {
|
||||||
|
const refreshToken = jwtServices.getRefreshToken();
|
||||||
|
data = {
|
||||||
|
refresh: refreshToken,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
await api
|
||||||
|
.post(auth.tokenRefresh, data, {
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
|
})
|
||||||
|
.then((response) => {
|
||||||
|
if (isDevelopment()) {
|
||||||
|
const token = response.data.access;
|
||||||
|
jwtServices.setToken(token);
|
||||||
|
originalRequest.headers["Authorization"] = `Bearer ${token}`;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
isAuthenticated.value = true;
|
isAuthenticated.value = true;
|
||||||
return await axios(originalRequest);
|
return await axios(originalRequest);
|
||||||
|
|
@ -48,6 +71,12 @@ api.interceptors.response.use(
|
||||||
isAuthenticated.value = false;
|
isAuthenticated.value = false;
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
} else if (status === 401 && originalRequest._retry) {
|
||||||
|
isAuthenticated.value = false;
|
||||||
|
router.push({ name: "login" });
|
||||||
|
if (isDevelopment()) {
|
||||||
|
jwtServices.destroyToken();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
35
src/core/app/jwt.ts
Normal file
35
src/core/app/jwt.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
const tokenKey: string = "token";
|
||||||
|
const refreshTokenKey: string = "refresh";
|
||||||
|
|
||||||
|
const setToken = function (token: string): void {
|
||||||
|
if (token) window.localStorage.setItem(tokenKey, token);
|
||||||
|
};
|
||||||
|
|
||||||
|
const setRefreshToken = function (refresh: string): void {
|
||||||
|
if (refresh) window.localStorage.setItem(refreshTokenKey, refresh);
|
||||||
|
};
|
||||||
|
|
||||||
|
const getToken = function (): string | null {
|
||||||
|
const token = window.localStorage.getItem(tokenKey);
|
||||||
|
return token;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getRefreshToken = function (): string | null {
|
||||||
|
const refreshToken = window.localStorage.getItem(refreshTokenKey);
|
||||||
|
return refreshToken;
|
||||||
|
};
|
||||||
|
|
||||||
|
const destroyToken = function (): void {
|
||||||
|
window.localStorage.removeItem(tokenKey);
|
||||||
|
window.localStorage.removeItem(refreshTokenKey);
|
||||||
|
};
|
||||||
|
|
||||||
|
const jwtServices = {
|
||||||
|
setToken,
|
||||||
|
setRefreshToken,
|
||||||
|
getToken,
|
||||||
|
getRefreshToken,
|
||||||
|
destroyToken,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default jwtServices;
|
||||||
|
|
@ -8,4 +8,16 @@ const getNameInitials = (name: string): string => {
|
||||||
return first + last;
|
return first + last;
|
||||||
};
|
};
|
||||||
|
|
||||||
export { getNameInitials };
|
const getEnvironment = () => {
|
||||||
|
return DOLPHIN.config.ENVIRONMENT;
|
||||||
|
};
|
||||||
|
|
||||||
|
const isDevelopment = () => {
|
||||||
|
return getEnvironment() == "DEVELOPMENT";
|
||||||
|
};
|
||||||
|
|
||||||
|
const isProduction = () => {
|
||||||
|
return getEnvironment() == "PRODUCTION";
|
||||||
|
};
|
||||||
|
|
||||||
|
export { getNameInitials, getEnvironment, isDevelopment, isProduction };
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ import type { LoginDetails, UserDetails } from "@/core/types/auth/auth.type";
|
||||||
import { defineStore } from "pinia";
|
import { defineStore } from "pinia";
|
||||||
import { useLoader } from "../App/loader.store";
|
import { useLoader } from "../App/loader.store";
|
||||||
import router from "@/router";
|
import router from "@/router";
|
||||||
|
import { isDevelopment } from "@/core/utils/common.util";
|
||||||
|
import jwtServices from "@/core/app/jwt";
|
||||||
|
|
||||||
export const useAuth = defineStore("auth", {
|
export const useAuth = defineStore("auth", {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
|
|
@ -28,8 +30,12 @@ export const useAuth = defineStore("auth", {
|
||||||
loaderStore.start("login");
|
loaderStore.start("login");
|
||||||
await api
|
await api
|
||||||
.post(auth.login, this.loginDetails)
|
.post(auth.login, this.loginDetails)
|
||||||
.then(() => {
|
.then((response) => {
|
||||||
loaderStore.stop("login");
|
loaderStore.stop("login");
|
||||||
|
if (isDevelopment()) {
|
||||||
|
jwtServices.setToken(response.data.data[0].access);
|
||||||
|
jwtServices.setRefreshToken(response.data.data[0].refresh);
|
||||||
|
}
|
||||||
this.isAuthenticated = true;
|
this.isAuthenticated = true;
|
||||||
router.push({ name: "dashboard" });
|
router.push({ name: "dashboard" });
|
||||||
})
|
})
|
||||||
|
|
@ -65,6 +71,9 @@ export const useAuth = defineStore("auth", {
|
||||||
this.isAuthenticated = false;
|
this.isAuthenticated = false;
|
||||||
router.push({ name: "login" });
|
router.push({ name: "login" });
|
||||||
loaderStore.stop("logout");
|
loaderStore.stop("logout");
|
||||||
|
if (isDevelopment()) {
|
||||||
|
jwtServices.destroyToken();
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user