First commit

This commit is contained in:
dyzulk
2025-12-30 12:11:04 +07:00
commit 34dc111344
322 changed files with 31972 additions and 0 deletions

116
src/hooks/useAuth.ts Normal file
View File

@@ -0,0 +1,116 @@
import useSWR from 'swr';
import axios from '@/lib/axios';
import { useEffect } from 'react';
import { useRouter } from 'next/navigation';
export const useAuth = ({ middleware, redirectIfAuthenticated }: { middleware?: string, redirectIfAuthenticated?: string } = {}) => {
const router = useRouter();
const { data: user, error, mutate } = useSWR('/api/user', () =>
axios
.get('/api/user')
.then(res => res.data)
.catch(error => {
if (error.response.status !== 409) throw error;
router.push('/verify-email');
})
);
const csrf = () => axios.get('/sanctum/csrf-cookie');
const register = async ({ setErrors, ...props }: any) => {
await csrf();
setErrors([]);
axios
.post('/register', props)
.then(() => mutate())
.catch(error => {
if (error.response.status !== 422) throw error;
setErrors(error.response.data.errors);
});
};
const login = async ({ setErrors, setStatus, ...props }: any) => {
await csrf();
setErrors([]);
setStatus && setStatus(null);
return axios
.post('/login', props)
.then(response => {
mutate();
return response;
})
.catch(error => {
if (error.response.status !== 422) throw error;
setErrors(error.response.data.errors);
throw error;
});
};
const logout = async () => {
if (!error) {
await axios.post('/logout').then(() => mutate());
}
window.location.pathname = '/signin';
};
const forgotPassword = async ({ setErrors, setStatus, email }: any) => {
await csrf();
setErrors([]);
setStatus(null);
return axios
.post('/forgot-password', { email })
.then(response => setStatus(response.data.message))
.catch(error => {
if (error.response.status !== 422) throw error;
setErrors(error.response.data.errors);
throw error;
});
};
const resetPassword = async ({ setErrors, setStatus, ...props }: any) => {
await csrf();
setErrors([]);
setStatus(null);
return axios
.post('/reset-password', props)
.then(response => {
setStatus(response.data.message);
router.push('/signin?message=' + encodeURIComponent(response.data.message));
})
.catch(error => {
if (error.response.status !== 422) throw error;
setErrors(error.response.data.errors);
throw error;
});
};
useEffect(() => {
if (middleware === 'guest' && redirectIfAuthenticated && user)
router.push(user.default_landing_page || redirectIfAuthenticated);
// Handle unverified users in auth protected routes
if (middleware === 'auth' && user && !user.email_verified_at && window.location.pathname !== '/verify-email') {
router.push('/verify-email');
}
// If on verify-email page but already verified, go to dashboard
if (window.location.pathname === '/verify-email' && user?.email_verified_at) {
router.push('/dashboard');
}
if (middleware === 'auth' && error) logout();
}, [user, error, middleware, redirectIfAuthenticated, router]);
return {
user,
mutate,
register,
login,
logout,
forgotPassword,
resetPassword,
};
};

17
src/hooks/useGoBack.ts Normal file
View File

@@ -0,0 +1,17 @@
import { useRouter } from "next/navigation";
const useGoBack = () => {
const router = useRouter();
const goBack = () => {
if (window.history.length > 1) {
router.back(); // Navigate to the previous route
} else {
router.push("/"); // Redirect to home if no history exists
}
};
return goBack;
};
export default useGoBack;

12
src/hooks/useModal.ts Normal file
View File

@@ -0,0 +1,12 @@
"use client";
import { useState, useCallback } from "react";
export const useModal = (initialState: boolean = false) => {
const [isOpen, setIsOpen] = useState(initialState);
const openModal = useCallback(() => setIsOpen(true), []);
const closeModal = useCallback(() => setIsOpen(false), []);
const toggleModal = useCallback(() => setIsOpen((prev) => !prev), []);
return { isOpen, openModal, closeModal, toggleModal };
};