Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
thecybernanny-webapp
Project
Overview
Details
Activity
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
asranov0003
thecybernanny-webapp
Commits
0409f918
Commit
0409f918
authored
Jul 04, 2025
by
asranov0003
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: auth context
parent
7ae3818b
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
46 additions
and
41 deletions
+46
-41
AuthContext.tsx
src/contexts/AuthContext/AuthContext.tsx
+5
-27
AuthContext.types.ts
src/contexts/AuthContext/AuthContext.types.ts
+4
-0
AuthProvider.tsx
src/contexts/AuthContext/AuthProvider.tsx
+16
-0
useAuth.ts
src/contexts/AuthContext/useAuth.ts
+12
-0
main.tsx
src/main.tsx
+1
-1
Login.tsx
src/pages/Auth/Login/Login.tsx
+1
-2
Recover.tsx
src/pages/Auth/Recover/Recover.tsx
+1
-1
Register.tsx
src/pages/Auth/Register/Register.tsx
+2
-2
ProtectedRoute.tsx
src/routes/ProtectedRoute.tsx
+3
-7
Router.tsx
src/routes/Router.tsx
+1
-1
No files found.
src/contexts/AuthContext/AuthContext.tsx
View file @
0409f918
import
React
,
{
createContext
,
useContext
,
useState
}
from
"react"
;
import
{
TG_USER_ID
}
from
"../../constants/constant
s"
;
import
{
createContext
}
from
"react"
;
import
type
{
AuthContextType
}
from
"./AuthContext.type
s"
;
interface
AuthContextType
{
isAuthenticated
:
boolean
;
setAuthenticated
:
(
auth
:
boolean
)
=>
void
;
}
const
AuthContext
=
createContext
<
AuthContextType
>
({
isAuthenticated
:
false
,
setAuthenticated
:
()
=>
{},
});
export
const
AuthProvider
:
React
.
FC
<
{
children
:
React
.
ReactNode
}
>
=
({
children
,
})
=>
{
const
[
isAuthenticated
,
setAuthenticated
]
=
useState
<
boolean
>
(()
=>
{
return
!!
localStorage
.
getItem
(
`token-
${
TG_USER_ID
}
`
);
});
return
(
<
AuthContext
.
Provider
value=
{
{
isAuthenticated
,
setAuthenticated
}
}
>
{
children
}
</
AuthContext
.
Provider
>
);
};
export
const
useAuth
=
()
=>
useContext
(
AuthContext
);
export
const
AuthContext
=
createContext
<
AuthContextType
|
undefined
>
(
undefined
);
src/contexts/AuthContext/AuthContext.types.ts
0 → 100644
View file @
0409f918
export
interface
AuthContextType
{
isAuthenticated
:
boolean
;
setAuthenticated
:
(
auth
:
boolean
)
=>
void
;
}
src/contexts/AuthContext/AuthProvider.tsx
0 → 100644
View file @
0409f918
import
{
useState
}
from
"react"
;
import
{
AuthContext
}
from
"./AuthContext"
;
export
const
AuthProvider
:
React
.
FC
<
{
children
:
React
.
ReactNode
}
>
=
({
children
,
})
=>
{
const
[
isAuthenticated
,
setAuthenticated
]
=
useState
<
boolean
>
(()
=>
{
return
!!
localStorage
.
getItem
(
`<token-1>TG_USER_ID</token-1>`
);
});
return
(
<
AuthContext
.
Provider
value=
{
{
isAuthenticated
,
setAuthenticated
}
}
>
{
children
}
</
AuthContext
.
Provider
>
);
};
src/contexts/AuthContext/useAuth.ts
0 → 100644
View file @
0409f918
import
{
useContext
}
from
"react"
;
import
{
AuthContext
}
from
"./AuthContext"
;
export
const
useAuth
=
()
=>
{
const
context
=
useContext
(
AuthContext
);
if
(
!
context
)
{
throw
new
Error
(
"useAuth must be used within an AuthProvider"
);
}
return
context
;
};
src/main.tsx
View file @
0409f918
...
...
@@ -6,7 +6,7 @@ import { store } from "./stores/store.ts";
import
"./locales/i18n.ts"
;
import
{
ThemeProvider
}
from
"./contexts/ThemeContext/ThemeProvider.tsx"
;
import
{
LanguageProvider
}
from
"./contexts/LanguageContext/LanguageProvider.tsx"
;
import
{
AuthProvider
}
from
"./contexts/AuthContext/Auth
Context
.tsx"
;
import
{
AuthProvider
}
from
"./contexts/AuthContext/Auth
Provider
.tsx"
;
createRoot
(
document
.
getElementById
(
"root"
)
!
).
render
(
<
Provider
store=
{
store
}
>
...
...
src/pages/Auth/Login/Login.tsx
View file @
0409f918
...
...
@@ -12,8 +12,8 @@ import {
useAppSelector
,
type
RootState
,
}
from
"../../../stores/store"
;
import
{
useAuth
}
from
"../../../contexts/AuthContext/AuthContext"
;
import
{
TG_USER_ID
}
from
"../../../constants/constants"
;
import
{
useAuth
}
from
"../../../contexts/AuthContext/useAuth"
;
interface
ILoginFormData
{
login
:
string
;
...
...
@@ -36,7 +36,6 @@ const Login: React.FC = () => {
const
onSubmit
=
async
(
data
:
ILoginFormData
)
=>
{
try
{
const
result
=
await
dispatch
(
authLogin
(
data
));
if
(
authLogin
.
fulfilled
.
match
(
result
))
{
const
token
=
result
.
payload
.
token
;
localStorage
.
setItem
(
`token-
${
TG_USER_ID
}
`
,
token
);
...
...
src/pages/Auth/Recover/Recover.tsx
View file @
0409f918
...
...
@@ -42,7 +42,7 @@ const Recover: React.FC = () => {
return
(
<
div
className=
"wrapper recover"
>
<
AuthHeader
back
onBack=
{
()
=>
navigate
(
"/
auth/login
"
)
}
/>
<
AuthHeader
back
onBack=
{
()
=>
navigate
(
"/"
)
}
/>
<
h2
>
{
t
(
"auth.passwordRecovery"
)
}
</
h2
>
...
...
src/pages/Auth/Register/Register.tsx
View file @
0409f918
...
...
@@ -10,7 +10,7 @@ import { useSelector } from "react-redux";
import
{
useAppDispatch
,
type
RootState
}
from
"../../../stores/store"
;
import
{
authRegister
}
from
"../../../stores/slices/authSlice"
;
import
{
TG_USER_ID
}
from
"../../../constants/constants"
;
import
{
useAuth
}
from
"../../../contexts/AuthContext/
AuthContext
"
;
import
{
useAuth
}
from
"../../../contexts/AuthContext/
useAuth
"
;
interface
IRegisterFormData
{
login
:
string
;
...
...
@@ -50,7 +50,7 @@ const Register: React.FC = () => {
return
(
<
div
className=
"wrapper"
>
<
AuthHeader
back
onBack=
{
()
=>
navigate
(
"/
auth/login
"
)
}
/>
<
AuthHeader
back
onBack=
{
()
=>
navigate
(
"/"
)
}
/>
<
h2
>
{
t
(
"auth.register"
)
}
</
h2
>
...
...
src/routes/ProtectedRoute.tsx
View file @
0409f918
...
...
@@ -12,17 +12,13 @@ const ProtectedRoute: React.FC<ProtectedRouteProps> = ({
redirectTo
,
})
=>
{
const
isAuthenticated
=
!!
localStorage
.
getItem
(
`token-
${
TG_USER_ID
}
`
);
const
hasP
incode
=
!!
sessionStorage
.
getItem
(
`pincode-
${
TG_USER_ID
}
`
);
const
p
incode
=
!!
sessionStorage
.
getItem
(
`pincode-
${
TG_USER_ID
}
`
);
if
(
!
isAuthenticated
)
{
return
<
Navigate
to=
{
redirectTo
}
/>;
}
if
(
!
hasPincode
)
{
if
(
isAuthenticated
&&
!
pincode
)
{
return
<
Navigate
to=
"/pincode"
/>;
}
return
element
;
return
isAuthenticated
?
element
:
<
Navigate
to=
{
redirectTo
}
/>
;
};
export
default
ProtectedRoute
;
src/routes/Router.tsx
View file @
0409f918
...
...
@@ -26,7 +26,7 @@ const Router: React.FC = () => {
element
:
isAuthenticated
&&
<
Navigate
to=
{
"/home"
}
/>,
children
:
[
{
path
:
"/auth/login"
,
index
:
true
,
element
:
<
Login
/>,
},
{
...
...
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