feat(auth): add basic implementation of login/register/logout store actions
This commit is contained in:
@@ -1,9 +1,66 @@
|
|||||||
import { create } from "zustand";
|
import { create } from "zustand";
|
||||||
import type { AuthStore } from "../../types/store";
|
import type { AuthStore } from "../../types/store";
|
||||||
|
import { login, logout, register } from "../../../api";
|
||||||
|
import { callApi } from "shared/utils";
|
||||||
|
|
||||||
export const useAuthStore = create<AuthStore>()((set) => ({
|
export const useAuthStore = create<AuthStore>()((set) => ({
|
||||||
user: undefined,
|
user: undefined,
|
||||||
status: "idle",
|
status: "idle",
|
||||||
setUser: (user) => set({ user }),
|
setUser: (user) => set({ user }),
|
||||||
setStatus: (status) => set({ status }),
|
setStatus: (status) => set({ status }),
|
||||||
|
|
||||||
|
login: async (loginData) => {
|
||||||
|
set({ status: "loading" });
|
||||||
|
try {
|
||||||
|
const [responseData, loginError] = await callApi(() => login(loginData));
|
||||||
|
|
||||||
|
if (loginError) {
|
||||||
|
set({ status: "unauthenticated" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
set({
|
||||||
|
status: "authenticated",
|
||||||
|
user: responseData?.user,
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
console.warn(err);
|
||||||
|
set({ status: "idle" });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
register: async (registerData) => {
|
||||||
|
try {
|
||||||
|
const [responseData, registerError] = await callApi(() =>
|
||||||
|
register(registerData),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (registerError) {
|
||||||
|
set({ status: "unauthenticated" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
set({
|
||||||
|
status: "authenticated",
|
||||||
|
user: responseData?.user,
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
console.warn(err);
|
||||||
|
set({ status: "idle" });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
logout: async () => {
|
||||||
|
set({ status: "loading" });
|
||||||
|
try {
|
||||||
|
const [, logoutError] = await callApi(() => logout());
|
||||||
|
|
||||||
|
if (logoutError) {
|
||||||
|
set({ status: "authenticated" });
|
||||||
|
}
|
||||||
|
|
||||||
|
set({ status: "unauthenticated", user: undefined });
|
||||||
|
} catch (err) {
|
||||||
|
console.warn(err);
|
||||||
|
set({ status: "idle" });
|
||||||
|
}
|
||||||
|
},
|
||||||
}));
|
}));
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import type { User } from "entities/User";
|
import type { User } from "entities/User";
|
||||||
import type { AuthStatus } from "./service";
|
import type { AuthData, AuthStatus } from "./service";
|
||||||
|
|
||||||
export interface AuthStoreState {
|
export interface AuthStoreState {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User's credentials
|
* User's credentials
|
||||||
*/
|
*/
|
||||||
@@ -12,9 +13,18 @@ export interface AuthStoreState {
|
|||||||
status: AuthStatus;
|
status: AuthStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type LoginAction = (data: AuthData) => void;
|
||||||
|
export type RegisterAction = (data: AuthData) => void;
|
||||||
|
export type LogoutAction = () => void;
|
||||||
|
|
||||||
export interface AuthStoreActions {
|
export interface AuthStoreActions {
|
||||||
setUser: (user: AuthStoreState["user"] | undefined) => void;
|
setUser: (user: AuthStoreState["user"] | undefined) => void;
|
||||||
setStatus: (status: AuthStoreState["status"]) => void;
|
setStatus: (status: AuthStoreState["status"]) => void;
|
||||||
|
|
||||||
|
// Async actions
|
||||||
|
login: LoginAction;
|
||||||
|
register: RegisterAction;
|
||||||
|
logout: LogoutAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AuthStore = AuthStoreState & AuthStoreActions;
|
export type AuthStore = AuthStoreState & AuthStoreActions;
|
||||||
|
|||||||
Reference in New Issue
Block a user