Skip to content

Commit 0cc5ef8

Browse files
fixed useEffect dependencies warning in auth.js
1 parent cb750a4 commit 0cc5ef8

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

.eslintrc.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "next/core-web-vitals",
3+
"rules": {
4+
"react-hooks/exhaustive-deps": "warn"
5+
}
6+
}

src/hooks/auth.js

+23-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from '@/lib/axios';
22
import { useParams, useRouter } from 'next/navigation';
3-
import { useEffect } from 'react';
3+
import { useCallback, useEffect } from 'react';
44
import useSWR from 'swr';
55

66
export const useAuth = ({ middleware, redirectIfAuthenticated } = {}) => {
@@ -21,12 +21,20 @@ export const useAuth = ({ middleware, redirectIfAuthenticated } = {}) => {
2121
}),
2222
);
2323

24-
// Add role checking utilities
2524
const isAdmin = user?.role === 'admin';
2625
const isUser = user?.role === 'user';
2726

2827
const csrf = () => axios.get('/sanctum/csrf-cookie');
2928

29+
// Wrap logout in useCallback
30+
const logout = useCallback(async () => {
31+
if (!error) {
32+
await axios.post('/logout').then(() => mutate());
33+
}
34+
35+
window.location.pathname = '/login';
36+
}, [error, mutate]);
37+
3038
const register = async ({ setErrors, ...props }) => {
3139
await csrf();
3240

@@ -94,22 +102,23 @@ export const useAuth = ({ middleware, redirectIfAuthenticated } = {}) => {
94102
axios.post('/email/verification-notification').then((response) => setStatus(response.data.status));
95103
};
96104

97-
const logout = async () => {
98-
if (!error) {
99-
await axios.post('/logout').then(() => mutate());
105+
useEffect(() => {
106+
if (middleware === 'guest' && redirectIfAuthenticated && user) {
107+
router.push(redirectIfAuthenticated);
100108
}
101109

102-
window.location.pathname = '/login';
103-
};
104-
105-
useEffect(() => {
106-
if (middleware === 'guest' && redirectIfAuthenticated && user) router.push(redirectIfAuthenticated);
110+
if (middleware === 'auth' && !user?.email_verified_at) {
111+
router.push('/verify-email');
112+
}
107113

108-
if (middleware === 'auth' && !user?.email_verified_at) router.push('/verify-email');
114+
if (window.location.pathname === '/verify-email' && user?.email_verified_at) {
115+
router.push(redirectIfAuthenticated);
116+
}
109117

110-
if (window.location.pathname === '/verify-email' && user?.email_verified_at) router.push(redirectIfAuthenticated);
111-
if (middleware === 'auth' && error) logout();
112-
}, [user, error]);
118+
if (middleware === 'auth' && error) {
119+
logout();
120+
}
121+
}, [user, error, router, middleware, redirectIfAuthenticated, logout]);
113122

114123
return {
115124
user,

src/lib/axios.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Axios from 'axios';
22

33
const axios = Axios.create({
4-
baseURL: process.env.NEXT_PUBLIC_BACKEND_URL,
4+
baseURL: process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:8000',
55
headers: {
66
'X-Requested-With': 'XMLHttpRequest',
77
},

0 commit comments

Comments
 (0)