Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
V
VCCI-News
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Văn Hoàng
VCCI-News
Commits
27b0c0de
Commit
27b0c0de
authored
Jan 24, 2026
by
Phan Ngọc Quốc Văn
1
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
5bbdd04f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
0 deletions
+84
-0
AuthGuard.tsx
src/auth/AuthGuard.tsx
+53
-0
GuestGuard.tsx
src/auth/GuestGuard.tsx
+31
-0
No files found.
src/auth/AuthGuard.tsx
0 → 100644
View file @
27b0c0de
/* eslint-disable @typescript-eslint/no-unused-vars */
'use client'
;
// ** React Imports
import
{
ReactNode
,
ReactElement
,
useEffect
}
from
'react'
;
// ** Stores & Hooks
import
useAuthStore
from
'@/stores/auth-store'
;
import
{
useRouter
,
usePathname
}
from
'next/navigation'
;
interface
AuthGuardProps
{
children
:
ReactNode
;
fallback
:
ReactElement
|
null
;
}
const
AuthGuard
=
({
children
,
fallback
}:
AuthGuardProps
)
=>
{
const
auth
=
useAuthStore
();
const
resetStore
=
useAuthStore
((
state
)
=>
state
.
resetStore
);
const
hasHydrated
=
useAuthStore
((
state
)
=>
state
.
_hasHydrated
);
const
router
=
useRouter
();
const
pathname
=
usePathname
();
useEffect
(()
=>
{
console
.
log
(
'AuthGuard useEffect - checking auth'
,
auth
.
username
,
hasHydrated
);
if
(
!
hasHydrated
)
return
;
const
token
=
typeof
window
!==
'undefined'
?
localStorage
.
getItem
(
'auth-client'
)
:
null
;
if
(
!
auth
.
email
||
!
token
)
{
// clear auth
resetStore
();
localStorage
.
removeItem
(
'auth-client'
);
if
(
pathname
!==
'/login'
)
{
router
.
replace
(
`/login?returnUrl=
${
encodeURIComponent
(
pathname
)}
`
);
}
}
},
[
auth
.
email
,
pathname
,
router
,
resetStore
,
hasHydrated
]);
if
(
!
hasHydrated
||
!
auth
.
email
)
{
return
fallback
;
}
return
<>
{
children
}
</>;
};
export
default
AuthGuard
;
src/auth/GuestGuard.tsx
0 → 100644
View file @
27b0c0de
"use client"
;
/* eslint-disable @typescript-eslint/no-unused-vars */
// ** React Imports
import
useAuthStore
from
"@/stores/auth-store"
;
import
{
useRouter
}
from
"next/navigation"
;
import
{
ReactElement
,
ReactNode
,
useEffect
}
from
"react"
;
interface
GuestGuardProps
{
children
:
ReactNode
;
fallback
:
ReactElement
|
null
;
}
const
GuestGuard
=
(
props
:
GuestGuardProps
)
=>
{
const
{
children
,
fallback
}
=
props
;
const
router
=
useRouter
();
const
auth
=
useAuthStore
();
const
hasHydrated
=
useAuthStore
((
state
)
=>
state
.
_hasHydrated
);
useEffect
(()
=>
{
if
(
!
hasHydrated
)
return
;
const
token
=
localStorage
.
getItem
(
"auth-client"
);
if
(
auth
.
email
||
token
)
{
router
.
replace
(
"/user"
);
}
},
[
router
,
auth
.
email
,
hasHydrated
]);
if
(
!
hasHydrated
||
auth
.
email
)
{
return
fallback
;
}
return
<>
{
children
}
</>;
};
export
default
GuestGuard
;
Phan Ngọc Quốc Văn
@quocvanphan
mentioned in commit
e56c3a6b
·
Jan 24, 2026
mentioned in commit
e56c3a6b
mentioned in commit e56c3a6b02a902b3d87281e80b1be5d1229d8b98
Toggle commit list
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment