Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
H
hw87AlenBolatov
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
Болатов Ален
hw87AlenBolatov
Commits
1d47c276
Commit
1d47c276
authored
Apr 06, 2023
by
Болатов Ален
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#11
made logout on backend
parent
de9a973f
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
21 additions
and
1 deletion
+21
-1
user.ts
backend/src/controllers/user.ts
+14
-0
IUser.ts
backend/src/interfaces/IUser.ts
+1
-0
user.ts
backend/src/routes/user.ts
+1
-0
user.ts
backend/src/services/user.ts
+5
-1
No files found.
backend/src/controllers/user.ts
View file @
1d47c276
import
{
Request
,
Response
}
from
'express'
;
import
{
getErrorMessage
}
from
'../utils/errors.util'
;
import
*
as
userServices
from
'../services/user'
;
import
jwt
from
'jsonwebtoken'
;
export
const
loginOne
=
async
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
...
...
@@ -19,3 +20,16 @@ export const registerOne = async (req: Request, res: Response) => {
return
res
.
status
(
500
).
send
(
getErrorMessage
(
error
));
}
};
export
const
logout
=
async
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
const
authHeader
=
req
.
headers
[
'authorization'
];
jwt
.
sign
(
authHeader
!
,
''
,
{
expiresIn
:
1
},
(
logout
,
err
)
=>
{
if
(
logout
)
{
res
.
send
(
'You have been logged out'
);
}
});
}
catch
(
err
:
unknown
)
{
throw
err
;
}
};
backend/src/interfaces/IUser.ts
View file @
1d47c276
import
mongoose
from
'mongoose'
;
export
default
interface
IUser
extends
mongoose
.
Document
{
_id
:
string
;
username
:
string
;
password
:
string
;
token
:
string
;
...
...
backend/src/routes/user.ts
View file @
1d47c276
...
...
@@ -5,5 +5,6 @@ const router: Router = Router();
router
.
post
(
'/'
,
userController
.
registerOne
);
router
.
post
(
'/sessions'
,
userController
.
loginOne
);
router
.
put
(
'/logout'
,
userController
.
logout
);
export
{
router
as
UsersRouter
};
backend/src/services/user.ts
View file @
1d47c276
...
...
@@ -30,7 +30,11 @@ export async function login(user: IUser) {
}
);
await
UserModel
.
findOneAndUpdate
({
username
:
user
.
username
},
{
token
});
return
{
token
:
token
};
return
{
token
:
token
,
username
:
user
.
username
,
id
:
foundUser
.
_id
.
toString
(),
};
}
else
{
throw
new
Error
(
'Password is not correct'
);
}
...
...
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