bearer для токена

This commit is contained in:
krokodilka 2023-05-05 22:05:09 +03:00
parent c251a598cc
commit 9cb2548603
4 changed files with 14 additions and 3 deletions

@ -5,6 +5,7 @@ import { authStore } from "@root/stores/auth";
const PublicRoute = ({ children }: any) => {
const location = useLocation();
const { token } = authStore();
console.log(token)
if (token) {
return <Navigate to="/users" state={{ from: location }} />;

@ -16,8 +16,16 @@ import ClearIcon from "@mui/icons-material/Clear";
import { getRoles_mock, TMockData } from "../../../api/roles";
import theme from "../../../theme";
import axios from "axios";
import {authStore} from "@stores/auth";
const Users: React.FC = () => {
const { makeRequest } = authStore();
makeRequest({
url: "https://admin.pena.digital/strator/account",
method: "get",
bearer: true,
contentType: true,
})
const radioboxes = ["a", "b", "c"];
const [selectedValue, setSelectedValue] = React.useState("a");

@ -53,7 +53,7 @@ const Header: React.FC = () => {
<IconButton
onClick={() => {
makeRequest({
url: "https://admin.pena.digital/auth/logout",
url: "https://admin.pena.digital/auth/auth/logout",
contentType: true,
}).then(() => localStorage.setItem("AT", ""));
}}

@ -17,11 +17,12 @@ interface FirstRequest<T> {
body?: T;
useToken?: boolean;
contentType?: boolean;
bearer?: boolean;
signal?: AbortSignal;
}
export const authStore = create<AuthStore>()(
devtools(
devtools(
(set, get) => ({
token: "",
setToken: (newToken) => set({ token: newToken }),
@ -50,13 +51,14 @@ async function makeRequest<TRequest, TResponse>({
useToken = true,
signal,
contentType = false,
bearer = false,
HC,
token,
}: MakeRequest<TRequest>) {
//В случае 401 рефреш должен попробовать вызваться 1 раз
let counterRefresh = true;
let headers: any = {};
if (useToken) headers["Authorization"] = token;
if (useToken) headers["Authorization"] = bearer ? "Bearer " + token : token;
if (contentType) headers["Content-Type"] = "application/json";
try {