Compare commits
2 Commits
55451d3eb4
...
11b84ddc5d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11b84ddc5d | ||
|
|
98146a7996 |
@@ -2,12 +2,12 @@ import { create } from "zustand";
|
|||||||
import type { AuthStore } from "../../types/store";
|
import type { AuthStore } from "../../types/store";
|
||||||
import { login, logout, register } from "../../../api";
|
import { login, logout, register } from "../../../api";
|
||||||
import { callApi } from "shared/utils";
|
import { callApi } from "shared/utils";
|
||||||
|
import { UNEXPECTED_ERROR_MESSAGE } from "shared/config";
|
||||||
|
|
||||||
export const useAuthStore = create<AuthStore>()((set) => ({
|
export const useAuthStore = create<AuthStore>()((set) => ({
|
||||||
user: undefined,
|
user: undefined,
|
||||||
status: "idle",
|
status: "idle",
|
||||||
setUser: (user) => set({ user }),
|
error: null,
|
||||||
setStatus: (status) => set({ status }),
|
|
||||||
|
|
||||||
login: async (loginData) => {
|
login: async (loginData) => {
|
||||||
set({ status: "loading" });
|
set({ status: "loading" });
|
||||||
@@ -15,17 +15,21 @@ export const useAuthStore = create<AuthStore>()((set) => ({
|
|||||||
const [responseData, loginError] = await callApi(() => login(loginData));
|
const [responseData, loginError] = await callApi(() => login(loginData));
|
||||||
|
|
||||||
if (loginError) {
|
if (loginError) {
|
||||||
set({ status: "unauthenticated" });
|
set({ status: "unauthenticated", error: loginError });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
set({
|
set({
|
||||||
status: "authenticated",
|
status: "authenticated",
|
||||||
user: responseData?.user,
|
user: responseData?.user,
|
||||||
|
error: null,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn(err);
|
console.error(err);
|
||||||
set({ status: "idle" });
|
set({
|
||||||
|
status: "unauthenticated",
|
||||||
|
error: new Error(UNEXPECTED_ERROR_MESSAGE),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
register: async (registerData) => {
|
register: async (registerData) => {
|
||||||
@@ -35,17 +39,21 @@ export const useAuthStore = create<AuthStore>()((set) => ({
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (registerError) {
|
if (registerError) {
|
||||||
set({ status: "unauthenticated" });
|
set({ status: "unauthenticated", error: registerError });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
set({
|
set({
|
||||||
status: "authenticated",
|
status: "authenticated",
|
||||||
user: responseData?.user,
|
user: responseData?.user,
|
||||||
|
error: null,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn(err);
|
console.error(err);
|
||||||
set({ status: "idle" });
|
set({
|
||||||
|
status: "unauthenticated",
|
||||||
|
error: new Error(UNEXPECTED_ERROR_MESSAGE),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
logout: async () => {
|
logout: async () => {
|
||||||
@@ -54,13 +62,18 @@ export const useAuthStore = create<AuthStore>()((set) => ({
|
|||||||
const [, logoutError] = await callApi(() => logout());
|
const [, logoutError] = await callApi(() => logout());
|
||||||
|
|
||||||
if (logoutError) {
|
if (logoutError) {
|
||||||
set({ status: "authenticated" });
|
set({ error: logoutError });
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
set({ status: "unauthenticated", user: undefined });
|
set({
|
||||||
|
status: "unauthenticated",
|
||||||
|
user: undefined,
|
||||||
|
error: null,
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn(err);
|
console.error(err);
|
||||||
set({ status: "idle" });
|
set({ error: new Error(UNEXPECTED_ERROR_MESSAGE) });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import type { User } from "entities/User";
|
import type { User } from "entities/User";
|
||||||
import type { AuthData, AuthStatus } from "./service";
|
import type { AuthData, AuthStatus } from "./service";
|
||||||
|
import type { ApiError } from "shared/utils";
|
||||||
|
|
||||||
export interface AuthStoreState {
|
export interface AuthStoreState {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User's credentials
|
* User's credentials
|
||||||
*/
|
*/
|
||||||
@@ -11,6 +11,10 @@ export interface AuthStoreState {
|
|||||||
* Authentication status
|
* Authentication status
|
||||||
*/
|
*/
|
||||||
status: AuthStatus;
|
status: AuthStatus;
|
||||||
|
/**
|
||||||
|
* Error data
|
||||||
|
*/
|
||||||
|
error: ApiError | Error | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type LoginAction = (data: AuthData) => void;
|
export type LoginAction = (data: AuthData) => void;
|
||||||
@@ -18,9 +22,6 @@ export type RegisterAction = (data: AuthData) => void;
|
|||||||
export type LogoutAction = () => void;
|
export type LogoutAction = () => void;
|
||||||
|
|
||||||
export interface AuthStoreActions {
|
export interface AuthStoreActions {
|
||||||
setUser: (user: AuthStoreState["user"] | undefined) => void;
|
|
||||||
setStatus: (status: AuthStoreState["status"]) => void;
|
|
||||||
|
|
||||||
// Async actions
|
// Async actions
|
||||||
login: LoginAction;
|
login: LoginAction;
|
||||||
register: RegisterAction;
|
register: RegisterAction;
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
export const BASE_URL =
|
export const BASE_URL =
|
||||||
import.meta.env.VITE_API_BASE_URL || "https://localhost:3001";
|
import.meta.env.VITE_API_BASE_URL || "https://localhost:3001";
|
||||||
|
|
||||||
|
export const UNEXPECTED_ERROR_MESSAGE = "An unexpected error occured";
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import ky from "ky";
|
import ky from "ky";
|
||||||
import { BASE_URL } from "./endpoint";
|
import { BASE_URL } from "./constants";
|
||||||
|
|
||||||
export const api = ky.create({
|
export const api = ky.create({
|
||||||
prefixUrl: BASE_URL,
|
prefixUrl: BASE_URL,
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
export * from "./api/endpoint";
|
export * from "./api/constants";
|
||||||
export * from "./api/httpClient";
|
export * from "./api/httpClient";
|
||||||
|
|||||||
Reference in New Issue
Block a user