mirror of
https://github.com/swissmakers/swiss-datashare.git
synced 2026-04-17 04:33:15 +02:00
feat: use cookies for authentication
This commit is contained in:
@@ -37,7 +37,7 @@ const ActionAvatar = () => {
|
||||
|
||||
<Menu.Item
|
||||
onClick={async () => {
|
||||
authService.signOut();
|
||||
await authService.signOut();
|
||||
}}
|
||||
icon={<TbDoorExit size={14} />}
|
||||
>
|
||||
|
||||
@@ -1,20 +1,7 @@
|
||||
import axios, { AxiosError } from "axios";
|
||||
import { getCookie } from "cookies-next";
|
||||
import axios from "axios";
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: "/api",
|
||||
});
|
||||
|
||||
api.interceptors.request.use(
|
||||
(config) => {
|
||||
const accessToken = getCookie("access_token");
|
||||
if (accessToken) {
|
||||
config!.headers!.Authorization = `Bearer ${accessToken}`;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error: AxiosError) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
export default api;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getCookie, setCookie } from "cookies-next";
|
||||
import { getCookie } from "cookies-next";
|
||||
import * as jose from "jose";
|
||||
import api from "./api.service";
|
||||
|
||||
@@ -12,11 +12,6 @@ const signIn = async (emailOrUsername: string, password: string) => {
|
||||
password,
|
||||
});
|
||||
|
||||
setCookie("access_token", response.data.accessToken);
|
||||
setCookie("refresh_token", response.data.refreshToken, {
|
||||
maxAge: 60 * 60 * 24 * 30 * 3,
|
||||
});
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
@@ -37,45 +32,30 @@ const signInTotp = async (
|
||||
loginToken,
|
||||
});
|
||||
|
||||
setCookie("access_token", response.data.accessToken);
|
||||
setCookie("refresh_token", response.data.refreshToken, {
|
||||
maxAge: 60 * 60 * 24 * 30 * 3,
|
||||
});
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
const signUp = async (email: string, username: string, password: string) => {
|
||||
const response = await api.post("auth/signUp", { email, username, password });
|
||||
|
||||
setCookie("access_token", response.data.accessToken);
|
||||
setCookie("refresh_token", response.data.refreshToken, {
|
||||
maxAge: 60 * 60 * 24 * 30 * 3,
|
||||
});
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
const signOut = () => {
|
||||
setCookie("access_token", null);
|
||||
setCookie("refresh_token", null);
|
||||
const signOut = async () => {
|
||||
await api.post("/auth/signOut");
|
||||
window.location.reload();
|
||||
};
|
||||
|
||||
const refreshAccessToken = async () => {
|
||||
try {
|
||||
const accessToken = getCookie("access_token") as string;
|
||||
const refreshToken = getCookie("refresh_token");
|
||||
if (
|
||||
(accessToken &&
|
||||
(jose.decodeJwt(accessToken).exp ?? 0) * 1000 <
|
||||
Date.now() + 2 * 60 * 1000) ||
|
||||
(refreshToken && !accessToken)
|
||||
!accessToken ||
|
||||
(jose.decodeJwt(accessToken).exp ?? 0) * 1000 < Date.now() + 2 * 60 * 1000
|
||||
) {
|
||||
const response = await api.post("auth/token", { refreshToken });
|
||||
setCookie("access_token", response.data.accessToken);
|
||||
await api.post("/auth/token");
|
||||
}
|
||||
} catch {
|
||||
} catch (e) {
|
||||
console.info("Refresh token invalid or expired");
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user