feat(auth): add registration page
This commit is contained in:
@ -1,6 +1,11 @@
|
||||
import { create } from "zustand";
|
||||
import { persist } from "zustand/middleware";
|
||||
import { api, type User, type LoginRequest } from "../utils/api";
|
||||
import {
|
||||
api,
|
||||
type User,
|
||||
type LoginRequest,
|
||||
type RegistrationRequest,
|
||||
} from "../utils/api";
|
||||
|
||||
interface AuthState {
|
||||
user: User | null;
|
||||
@ -8,7 +13,9 @@ interface AuthState {
|
||||
isAuthenticated: boolean;
|
||||
isLoading: boolean;
|
||||
error: string | null;
|
||||
registrationMessage: string | null;
|
||||
login: (credentials: LoginRequest) => Promise<boolean>;
|
||||
register: (credentials: RegistrationRequest) => Promise<boolean>;
|
||||
logout: () => void;
|
||||
clearError: () => void;
|
||||
}
|
||||
@ -21,6 +28,7 @@ export const useAuthStore = create<AuthState>()(
|
||||
isAuthenticated: false,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
registrationMessage: null,
|
||||
|
||||
login: async (credentials: LoginRequest) => {
|
||||
set({ isLoading: true, error: null });
|
||||
@ -51,6 +59,31 @@ export const useAuthStore = create<AuthState>()(
|
||||
return false;
|
||||
}
|
||||
},
|
||||
register: async (credentials: RegistrationRequest) => {
|
||||
set({ isLoading: true, error: null });
|
||||
|
||||
try {
|
||||
const response = await api.register(credentials);
|
||||
|
||||
set({
|
||||
registrationMessage: response.message,
|
||||
});
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
const errorMessage =
|
||||
error instanceof Error ? error.message : "Registration failed";
|
||||
set({
|
||||
user: null,
|
||||
token: null,
|
||||
isAuthenticated: false,
|
||||
isLoading: false,
|
||||
error: errorMessage,
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
logout: () => {
|
||||
set({
|
||||
@ -70,6 +103,6 @@ export const useAuthStore = create<AuthState>()(
|
||||
token: state.token,
|
||||
isAuthenticated: state.isAuthenticated,
|
||||
}),
|
||||
}
|
||||
)
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user