From 3ef3fff739dece491f53f700d0f81658ff0f4b10 Mon Sep 17 00:00:00 2001 From: Platon Yasev Date: Sun, 19 Feb 2023 19:04:48 +0300 Subject: [PATCH] Change isAuthorized type --- src/SignOut.jsx | 4 ++-- src/api.js | 4 +++- src/stores/auth.js | 9 ++++----- src/stores/signin.js | 4 ++-- vite.config.js | 6 ++++-- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/SignOut.jsx b/src/SignOut.jsx index 7561690..b6ba119 100644 --- a/src/SignOut.jsx +++ b/src/SignOut.jsx @@ -1,6 +1,6 @@ import { Button } from "antd"; import { LogoutOutlined } from "@ant-design/icons"; -import { setToken } from "./stores/auth"; +import { setAuth } from "./stores/auth"; import { api } from "./api"; export function SignOut() { @@ -14,7 +14,7 @@ export function SignOut() { onClick={async () => { await api.post("accounts/logout/"); - setToken(""); + setAuth(false); }} /> diff --git a/src/api.js b/src/api.js index b93ec3d..49abd65 100644 --- a/src/api.js +++ b/src/api.js @@ -1,10 +1,12 @@ import axios from "axios"; +export const BASE_URL = "https://postnet-dev.selftech.ru"; + export const api = axios.create({ baseURL: import.meta.env.MODE === "development" ? "http://localhost:5173/" - : "https://postnet-dev.selftech.ru/", + : `${BASE_URL}/`, withCredentials: true, xsrfHeaderName: "X-CSRFToken", xsrfCookieName: "csrftoken", diff --git a/src/stores/auth.js b/src/stores/auth.js index 06e0934..2b85ce8 100644 --- a/src/stores/auth.js +++ b/src/stores/auth.js @@ -1,12 +1,11 @@ -import { action, atom, computed } from "nanostores"; +import { action, atom } from "nanostores"; import { api } from "../api"; -export const token$ = atom(""); export const userInfoLoading$ = atom(true); -export const isAuthorized$ = computed([token$], (token) => token !== ""); +export const isAuthorized$ = atom(false); -export const setToken = action(token$, "setToken", (store, newValue) => { +export const setAuth = action(isAuthorized$, "setAuth", (store, newValue) => { store.set(newValue); }); @@ -14,7 +13,7 @@ async function checkAuth() { try { await api.get("/accounts/profile/"); - setToken("123456"); + setAuth(true); } catch (e) { console.log("Not authorized"); } finally { diff --git a/src/stores/signin.js b/src/stores/signin.js index 4ef37cb..b290f97 100644 --- a/src/stores/signin.js +++ b/src/stores/signin.js @@ -1,6 +1,6 @@ import { atom } from "nanostores"; import { api } from "../api"; -import { setToken } from "./auth"; +import { setAuth } from "./auth"; export class DjangoValidationError extends Error { errors; @@ -34,7 +34,7 @@ export async function signin(values) { try { const { data } = await api.post("accounts/login/", values); - setToken("123456"); + setAuth(true); return data; } catch (e) { var error = DEFAULT_ERROR; diff --git a/vite.config.js b/vite.config.js index 98d1e2b..2cd45ce 100644 --- a/vite.config.js +++ b/vite.config.js @@ -2,14 +2,16 @@ import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; import svgr from "vite-plugin-svgr"; +const BASE_URL = "https://postnet-dev.selftech.ru/"; + // https://vitejs.dev/config/ export default defineConfig({ base: "/", plugins: [svgr(), react()], server: { proxy: { - "/account": "https://postnet-dev.selftech.ru/", - "/api": "https://postnet-dev.selftech.ru/", + "/account": BASE_URL, + "/api": BASE_URL, }, }, css: {