Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
I
initial_project
Project
Project
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
Нұрасыл Қайратұлы
initial_project
Commits
718404e2
Commit
718404e2
authored
Aug 13, 2024
by
Нұрасыл Қайратұлы
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
a2e84f82
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
1412 additions
and
210 deletions
+1412
-210
package-lock.json
client/package-lock.json
+1074
-180
package.json
client/package.json
+4
-3
App.tsx
client/src/App.tsx
+25
-6
ProtectedRoute.tsx
client/src/components/ProtectedRoute.tsx
+13
-0
Register.tsx
client/src/components/Register.tsx
+58
-0
SignIn.tsx
client/src/components/SignIn.tsx
+52
-0
AppToolbar.tsx
client/src/components/UI/AppToolbar.tsx
+6
-2
Auth.tsx
client/src/containers/Auth.tsx
+41
-0
Products2.tsx
client/src/containers/Products2.tsx
+0
-19
usersSlice.ts
client/src/features/usersSlice.ts
+119
-0
index.ts
client/src/store/index.ts
+2
-0
user.controller.ts
server/src/controllers/user.controller.ts
+6
-0
user.repository.ts
server/src/repositories/user.repository.ts
+7
-0
user.route.ts
server/src/routes/user.route.ts
+1
-0
user.service.ts
server/src/services/user.service.ts
+4
-0
No files found.
client/package-lock.json
View file @
718404e2
This diff is collapsed.
Click to expand it.
client/package.json
View file @
718404e2
...
@@ -10,12 +10,13 @@
...
@@ -10,12 +10,13 @@
"preview"
:
"vite preview"
"preview"
:
"vite preview"
},
},
"dependencies"
:
{
"dependencies"
:
{
"@emotion/react"
:
"^11.1
1.4
"
,
"@emotion/react"
:
"^11.1
3.0
"
,
"@emotion/styled"
:
"^11.1
1.5
"
,
"@emotion/styled"
:
"^11.1
3.0
"
,
"@fontsource/roboto"
:
"^5.0.12"
,
"@fontsource/roboto"
:
"^5.0.12"
,
"@mui/icons-material"
:
"^5.15.14"
,
"@mui/icons-material"
:
"^5.15.14"
,
"@mui/material"
:
"^5.1
5.14
"
,
"@mui/material"
:
"^5.1
6.7
"
,
"@reduxjs/toolkit"
:
"^2.1.0"
,
"@reduxjs/toolkit"
:
"^2.1.0"
,
"antd"
:
"^5.20.0"
,
"axios"
:
"^1.6.7"
,
"axios"
:
"^1.6.7"
,
"path"
:
"^0.12.7"
,
"path"
:
"^0.12.7"
,
"react"
:
"^18.2.0"
,
"react"
:
"^18.2.0"
,
...
...
client/src/App.tsx
View file @
718404e2
import
{
Container
,
CssBaseline
}
from
'@mui/material'
;
import
{
Container
}
from
'@mui/material'
;
import
{
Route
,
Routes
}
from
'react-router-dom'
;
import
{
Route
,
Routes
}
from
'react-router-dom'
;
import
{
AppToolbar
}
from
'./components/UI/AppToolbar'
;
import
{
AppToolbar
}
from
'./components/UI/AppToolbar'
;
import
{
Products
}
from
'./containers/Products'
;
import
{
Products
}
from
'./containers/Products'
;
import
{
NewProductForm
}
from
'./containers/NewProductForm'
;
import
{
NewProductForm
}
from
'./containers/NewProductForm'
;
import
{
Auth
}
from
'./containers/Auth'
;
import
{
ProtectedRoute
}
from
'./components/ProtectedRoute'
;
import
{
useAppDispatch
,
useAppSelector
}
from
'./store/hook'
;
import
{
useEffect
}
from
'react'
;
import
{
validateToken
}
from
'./features/usersSlice'
;
function
App
()
{
function
App
()
{
const
dispatch
=
useAppDispatch
()
const
{
user
}
=
useAppSelector
(
state
=>
state
.
users
)
useEffect
(()
=>
{
dispatch
(
validateToken
())
},
[
dispatch
])
return
(
return
(
<>
<>
<
a
href=
""
></
a
>
<
CssBaseline
/>
<
header
>
<
header
>
<
AppToolbar
/>
<
AppToolbar
user=
{
user
}
/>
</
header
>
</
header
>
<
main
>
<
main
>
<
Container
maxWidth=
"xl"
sx=
{
{
mt
:
10
}
}
>
<
Container
maxWidth=
"xl"
sx=
{
{
mt
:
10
}
}
>
<
Routes
>
<
Routes
>
<
Route
path=
"/"
element=
{
<
Products
/>
}
/>
<
Route
path=
"/"
element=
{
<
Route
path=
"/products/new"
element=
{
<
NewProductForm
/>
}
/>
<
ProtectedRoute
user=
{
user
}
>
<
Products
/>
</
ProtectedRoute
>
}
/>
<
Route
path=
"/products/new"
element=
{
<
ProtectedRoute
user=
{
user
}
>
<
NewProductForm
/>
</
ProtectedRoute
>
}
/>
<
Route
path=
'/auth'
element=
{
<
Auth
/>
}
/>
</
Routes
>
</
Routes
>
</
Container
>
</
Container
>
</
main
>
</
main
>
...
...
client/src/components/ProtectedRoute.tsx
0 → 100644
View file @
718404e2
import
{
IUserState
}
from
"@/features/usersSlice"
import
{
ReactNode
}
from
"react"
import
{
Navigate
}
from
"react-router-dom"
type
TProps
=
{
user
:
IUserState
|
null
children
:
ReactNode
}
export
const
ProtectedRoute
=
({
user
,
children
}:
TProps
)
=>
{
if
(
!
user
)
return
<
Navigate
to=
{
'/auth'
}
/>
return
children
}
\ No newline at end of file
client/src/components/Register.tsx
0 → 100644
View file @
718404e2
import
{
registerUser
}
from
'@/features/usersSlice'
;
import
{
useAppDispatch
}
from
'@/store/hook'
;
import
{
Button
,
Form
,
FormProps
,
Input
}
from
'antd'
export
type
FieldTypeRegister
=
{
username
:
string
;
displayName
:
string
;
password
:
string
;
}
export
const
Register
=
()
=>
{
const
dispatch
=
useAppDispatch
()
const
onFinish
:
FormProps
<
FieldTypeRegister
>
[
'onFinish'
]
=
(
values
)
=>
{
dispatch
(
registerUser
(
values
))
}
return
(
<
Form
name=
"basic"
labelCol=
{
{
span
:
8
}
}
wrapperCol=
{
{
span
:
16
}
}
style=
{
{
maxWidth
:
600
}
}
initialValues=
{
{
remember
:
true
}
}
onFinish=
{
onFinish
}
autoComplete=
"off"
>
<
Form
.
Item
<
FieldTypeRegister
>
label="Username"
name="username"
rules=
{
[{
required
:
true
,
message
:
'Please input your username!'
}]
}
>
<
Input
/>
</
Form
.
Item
>
<
Form
.
Item
<
FieldTypeRegister
>
label="Nickname"
name="displayName"
>
<
Input
/>
</
Form
.
Item
>
<
Form
.
Item
<
FieldTypeRegister
>
label="Password"
name="password"
rules=
{
[{
required
:
true
,
message
:
'Please input your password!'
}]
}
>
<
Input
.
Password
/>
</
Form
.
Item
>
<
Form
.
Item
wrapperCol=
{
{
offset
:
8
,
span
:
16
}
}
>
<
Button
type=
"primary"
htmlType=
"submit"
>
Submit
</
Button
>
</
Form
.
Item
>
</
Form
>
)
}
\ No newline at end of file
client/src/components/SignIn.tsx
0 → 100644
View file @
718404e2
import
{
signInUser
}
from
'@/features/usersSlice'
;
import
{
useAppDispatch
}
from
'@/store/hook'
;
import
{
Button
,
Form
,
FormProps
,
Input
}
from
'antd'
export
type
FieldTypeSignIn
=
{
username
:
string
;
password
:
string
;
}
export
const
SignIn
=
()
=>
{
const
dispatch
=
useAppDispatch
()
const
onFinish
:
FormProps
<
FieldTypeSignIn
>
[
'onFinish'
]
=
(
values
)
=>
{
dispatch
(
signInUser
(
values
))
}
return
(
<
Form
name=
"basic"
labelCol=
{
{
span
:
8
}
}
wrapperCol=
{
{
span
:
16
}
}
style=
{
{
maxWidth
:
600
}
}
initialValues=
{
{
remember
:
true
}
}
onFinish=
{
onFinish
}
autoComplete=
"off"
>
<
Form
.
Item
<
FieldTypeSignIn
>
label="Username"
name="username"
rules=
{
[{
required
:
true
,
message
:
'Please input your username!'
}]
}
>
<
Input
/>
</
Form
.
Item
>
<
Form
.
Item
<
FieldTypeSignIn
>
label="Password"
name="password"
rules=
{
[{
required
:
true
,
message
:
'Please input your password!'
}]
}
>
<
Input
.
Password
/>
</
Form
.
Item
>
<
Form
.
Item
wrapperCol=
{
{
offset
:
8
,
span
:
16
}
}
>
<
Button
type=
"primary"
htmlType=
"submit"
>
Submit
</
Button
>
</
Form
.
Item
>
</
Form
>
)
}
\ No newline at end of file
client/src/components/UI/AppToolbar.tsx
View file @
718404e2
import
{
IUserState
}
from
'@/features/usersSlice'
;
import
{
AppBar
,
Toolbar
,
Typography
,
styled
}
from
'@mui/material'
;
import
{
AppBar
,
Toolbar
,
Typography
,
styled
}
from
'@mui/material'
;
import
{
Link
}
from
'react-router-dom'
;
import
{
Link
}
from
'react-router-dom'
;
...
@@ -7,14 +8,17 @@ const StyledLink = styled(Link)(() => ({
...
@@ -7,14 +8,17 @@ const StyledLink = styled(Link)(() => ({
[
'&:hover'
]:
{
color
:
'inherit'
},
[
'&:hover'
]:
{
color
:
'inherit'
},
}));
}));
export
function
AppToolbar
()
{
export
function
AppToolbar
(
{
user
}:
{
user
:
IUserState
|
null
}
)
{
return
(
return
(
<>
<>
<
AppBar
position=
"fixed"
>
<
AppBar
position=
"fixed"
>
<
Toolbar
>
<
Toolbar
style=
{
{
display
:
'flex'
,
justifyContent
:
'space-between'
}
}
>
<
Typography
variant=
"h6"
component=
{
StyledLink
}
to=
{
'/'
}
>
<
Typography
variant=
"h6"
component=
{
StyledLink
}
to=
{
'/'
}
>
Computer parts shop
Computer parts shop
</
Typography
>
</
Typography
>
<
Typography
variant=
"h6"
component=
{
StyledLink
}
to=
{
'/'
}
>
{
user
?.
username
}
</
Typography
>
</
Toolbar
>
</
Toolbar
>
</
AppBar
>
</
AppBar
>
</>
</>
...
...
client/src/containers/Auth.tsx
0 → 100644
View file @
718404e2
import
{
Register
}
from
"@/components/Register"
;
import
{
SignIn
}
from
"@/components/SignIn"
;
import
{
useAppSelector
}
from
"@/store/hook"
;
import
{
Row
,
Tabs
,
TabsProps
}
from
"antd"
;
import
{
useEffect
}
from
"react"
;
import
{
useNavigate
}
from
"react-router-dom"
;
const
items
:
TabsProps
[
'items'
]
=
[
{
key
:
'1'
,
label
:
'Sign In'
,
children
:
<
SignIn
/>,
},
{
key
:
'2'
,
label
:
'Registration'
,
children
:
<
Register
/>,
},
]
export
const
Auth
=
()
=>
{
const
navigate
=
useNavigate
()
const
{
user
}
=
useAppSelector
(
state
=>
state
.
users
)
useEffect
(()
=>
{
if
(
user
)
navigate
(
'/'
)
},
[
navigate
,
user
])
return
(
<
Row
align=
{
'middle'
}
justify=
{
'center'
}
style=
{
{
height
:
'90vh'
}
}
>
<
Tabs
defaultActiveKey=
"1"
items=
{
items
}
/>
</
Row
>
)
}
\ No newline at end of file
client/src/containers/Products2.tsx
deleted
100644 → 0
View file @
a2e84f82
import
{
Link
}
from
'react-router-dom'
;
import
{
Typography
,
Grid
,
Button
}
from
'@mui/material'
;
export
function
Products
()
{
return
(
<
Grid
container
direction=
"column"
spacing=
{
2
}
>
<
Grid
item
container
direction=
"row"
justifyContent=
"space-between"
alignItems=
"center"
>
<
Grid
item
>
<
Typography
variant=
"h4"
>
Products
</
Typography
>
</
Grid
>
<
Grid
item
>
<
Button
color=
"primary"
component=
{
Link
}
to=
{
'/products/new'
}
>
Add product
</
Button
>
</
Grid
>
</
Grid
>
</
Grid
>
);
}
client/src/features/usersSlice.ts
0 → 100644
View file @
718404e2
import
{
createAsyncThunk
,
createSlice
}
from
"@reduxjs/toolkit"
;
import
{
axiosApiClient
}
from
"../helpers/axiosApiClient"
;
import
{
FieldTypeRegister
}
from
"@/components/Register"
;
import
{
FieldTypeSignIn
}
from
"@/components/SignIn"
;
export
interface
IUserState
{
id
:
string
;
username
:
string
;
displayName
:
string
;
token
:
string
;
}
interface
State
{
user
:
IUserState
|
null
;
error
:
Error
|
null
;
loading
:
boolean
;
}
const
initialState
:
State
=
{
user
:
null
,
error
:
null
,
loading
:
false
}
export
const
registerUser
=
createAsyncThunk
(
'users/register'
,
async
(
body
:
FieldTypeRegister
)
=>
{
try
{
const
{
data
}
=
await
axiosApiClient
.
post
<
IUserState
>
(
'/user/registration'
,
body
)
localStorage
.
setItem
(
'token'
,
data
.
token
)
return
data
}
catch
(
e
)
{
throw
new
Error
((
e
as
Error
).
message
)
}
}
)
export
const
signInUser
=
createAsyncThunk
(
'users/signInUser'
,
async
(
body
:
FieldTypeSignIn
)
=>
{
try
{
const
{
data
}
=
await
axiosApiClient
.
post
<
IUserState
>
(
'/user/signIn'
,
body
)
localStorage
.
setItem
(
'token'
,
data
.
token
)
return
data
}
catch
(
e
)
{
throw
new
Error
((
e
as
Error
).
message
)
}
}
)
export
const
validateToken
=
createAsyncThunk
(
'users/validateToken'
,
async
()
=>
{
try
{
const
{
data
}
=
await
axiosApiClient
.
get
<
IUserState
>
(
'/user/validateToken'
,
{
headers
:
{
Authorization
:
localStorage
.
getItem
(
'token'
)
}
})
console
.
log
(
data
);
return
data
}
catch
(
e
)
{
throw
new
Error
((
e
as
Error
).
message
)
}
}
)
const
usersSlice
=
createSlice
(
{
name
:
'users'
,
initialState
,
reducers
:
{},
extraReducers
(
builder
)
{
builder
// REGISTRATION
.
addCase
(
registerUser
.
fulfilled
,
(
state
,
action
)
=>
{
state
.
user
=
action
.
payload
;
state
.
loading
=
false
;
})
.
addCase
(
registerUser
.
rejected
,
(
state
,
action
)
=>
{
state
.
loading
=
false
;
state
.
error
=
action
.
error
as
Error
;
})
.
addCase
(
registerUser
.
pending
,
(
state
)
=>
{
state
.
loading
=
true
;
})
// SIGN IN
.
addCase
(
signInUser
.
fulfilled
,
(
state
,
action
)
=>
{
state
.
user
=
action
.
payload
;
state
.
loading
=
false
;
})
.
addCase
(
signInUser
.
rejected
,
(
state
,
action
)
=>
{
state
.
loading
=
false
;
state
.
error
=
action
.
error
as
Error
;
})
.
addCase
(
signInUser
.
pending
,
(
state
)
=>
{
state
.
loading
=
true
;
})
// VALIDATE TOKEN
.
addCase
(
validateToken
.
fulfilled
,
(
state
,
action
)
=>
{
state
.
user
=
action
.
payload
;
state
.
loading
=
false
;
})
.
addCase
(
validateToken
.
rejected
,
(
state
,
action
)
=>
{
state
.
loading
=
false
;
state
.
error
=
action
.
error
as
Error
;
})
.
addCase
(
validateToken
.
pending
,
(
state
)
=>
{
state
.
loading
=
true
;
})
},
}
)
export
default
usersSlice
.
reducer
;
client/src/store/index.ts
View file @
718404e2
import
{
configureStore
}
from
'@reduxjs/toolkit'
;
import
{
configureStore
}
from
'@reduxjs/toolkit'
;
import
productsReducer
from
'../features/productsSlice.ts'
;
import
productsReducer
from
'../features/productsSlice.ts'
;
import
usersReducer
from
'@/features/usersSlice.ts'
;
const
store
=
configureStore
({
const
store
=
configureStore
({
reducer
:
{
reducer
:
{
products
:
productsReducer
,
products
:
productsReducer
,
users
:
usersReducer
}
}
})
})
...
...
server/src/controllers/user.controller.ts
View file @
718404e2
...
@@ -27,4 +27,10 @@ export class UserController {
...
@@ -27,4 +27,10 @@ export class UserController {
const
user
=
await
this
.
service
.
registration
(
registrationInDto
)
const
user
=
await
this
.
service
.
registration
(
registrationInDto
)
res
.
send
(
user
)
res
.
send
(
user
)
}
}
validateToken
:
RequestHandler
=
async
(
req
,
res
):
Promise
<
void
>
=>
{
const
token
=
req
.
headers
.
authorization
const
user
=
await
this
.
service
.
validateToken
(
token
||
''
)
res
.
send
(
user
)
}
}
}
server/src/repositories/user.repository.ts
View file @
718404e2
...
@@ -37,6 +37,13 @@ export class UserRepo {
...
@@ -37,6 +37,13 @@ export class UserRepo {
const
userWithoutPass
=
_
.
omit
(
user
,
'password'
)
const
userWithoutPass
=
_
.
omit
(
user
,
'password'
)
return
userWithoutPass
return
userWithoutPass
}
}
async
validateToken
(
token
:
string
):
Promise
<
IUser
|
null
>
{
const
user
=
await
this
.
repo
.
findOne
({
where
:
{
token
:
token
}})
if
(
!
user
)
return
null
const
userWithoutPass
=
_
.
omit
(
user
,
'password'
)
return
userWithoutPass
}
}
}
export
const
userRepo
=
new
UserRepo
()
export
const
userRepo
=
new
UserRepo
()
\ No newline at end of file
server/src/routes/user.route.ts
View file @
718404e2
...
@@ -15,5 +15,6 @@ export class UserRoute implements IRoute {
...
@@ -15,5 +15,6 @@ export class UserRoute implements IRoute {
private
init
()
{
private
init
()
{
this
.
router
.
post
(
'/registration'
,
this
.
controller
.
registration
);
this
.
router
.
post
(
'/registration'
,
this
.
controller
.
registration
);
this
.
router
.
post
(
'/signIn'
,
this
.
controller
.
signIn
);
this
.
router
.
post
(
'/signIn'
,
this
.
controller
.
signIn
);
this
.
router
.
get
(
'/validateToken'
,
this
.
controller
.
validateToken
);
}
}
}
}
server/src/services/user.service.ts
View file @
718404e2
...
@@ -10,4 +10,8 @@ export class UserService {
...
@@ -10,4 +10,8 @@ export class UserService {
async
registration
(
registrationUserDto
:
RegistrationUserDto
)
{
async
registration
(
registrationUserDto
:
RegistrationUserDto
)
{
return
await
userRepo
.
registration
(
registrationUserDto
)
return
await
userRepo
.
registration
(
registrationUserDto
)
}
}
async
validateToken
(
token
:
string
)
{
return
await
userRepo
.
validateToken
(
token
)
}
}
}
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