Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I_Helmet
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
21_22-J 62
I_Helmet
Commits
61a8047c
Commit
61a8047c
authored
Apr 30, 2022
by
Balasuriya D.A.M.
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Authorization Middleware
parent
5a44a3ca
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
4 deletions
+44
-4
IT18021080/Telemedicine-Chat-App/backend/controllers/userControllers.js
...emedicine-Chat-App/backend/controllers/userControllers.js
+3
-2
IT18021080/Telemedicine-Chat-App/backend/middleware/authMiddleware.js
...elemedicine-Chat-App/backend/middleware/authMiddleware.js
+38
-0
IT18021080/Telemedicine-Chat-App/backend/routes/userRoutes.js
...021080/Telemedicine-Chat-App/backend/routes/userRoutes.js
+3
-2
No files found.
IT18021080/Telemedicine-Chat-App/backend/controllers/userControllers.js
View file @
61a8047c
...
@@ -66,7 +66,8 @@ const authUser = asyncHandler(async (req, res) => {
...
@@ -66,7 +66,8 @@ const authUser = asyncHandler(async (req, res) => {
// /api/user?search = minosh - goint to creat search query
// /api/user?search = minosh - goint to creat search query
//This is how to access the query (search query)
//This is how to access the query (search query)
const
allUsers
=
asyncHandler
(
async
(
req
,
res
)
=>
{
const
allUsers
=
asyncHandler
(
async
(
req
,
res
)
=>
{
const
keyword
=
req
.
query
.
search
?
{
const
keyword
=
req
.
query
.
search
?
{
//use $or operation
//use $or operation
$or
:
[
$or
:
[
//references from MongoDB pages.can get more information from that about $regex
//references from MongoDB pages.can get more information from that about $regex
...
@@ -78,7 +79,7 @@ const allUsers = asyncHandler(async (req, res) => {
...
@@ -78,7 +79,7 @@ const allUsers = asyncHandler(async (req, res) => {
:
{};
:
{};
//query write to database
//query write to database
const
users
=
await
(
await
User
.
find
(
keyword
));
//
.find({ _id: { $ne: req.user._id } });
const
users
=
await
User
.
find
(
keyword
)
.
find
({
_id
:
{
$ne
:
req
.
user
.
_id
}
});
// find({_id:{$ne:req.user._id}}) - current id user loged in
// find({_id:{$ne:req.user._id}}) - current id user loged in
res
.
send
(
users
);
//to return
res
.
send
(
users
);
//to return
...
...
IT18021080/Telemedicine-Chat-App/backend/middleware/authMiddleware.js
0 → 100644
View file @
61a8047c
//import jwt
const
jwt
=
require
(
"
jsonwebtoken
"
);
const
User
=
require
(
"
../models/userModel.js
"
);
const
asyncHandler
=
require
(
"
express-async-handler
"
);
//asyncHandler to handle all this errors
//use next to move on the other operations
const
protect
=
asyncHandler
(
async
(
req
,
res
,
next
)
=>
{
let
token
;
if
(
req
.
headers
.
authorization
&&
req
.
headers
.
authorization
.
startsWith
(
"
Bearer
"
)
)
{
try
{
token
=
req
.
headers
.
authorization
.
split
(
"
"
)[
1
];
//decodes token id
const
decoded
=
jwt
.
verify
(
token
,
process
.
env
.
JWT_SECRET
);
req
.
user
=
await
User
.
findById
(
decoded
.
id
).
select
(
"
-password
"
);
next
();
}
catch
(
error
)
{
res
.
status
(
401
);
throw
new
Error
(
"
Not authorized, token failed
"
);
}
}
//if uper bearer condtion is not satisfied this one will use
if
(
!
token
)
{
res
.
status
(
401
);
throw
new
Error
(
"
Not authorized, no token
"
);
}
});
module
.
exports
=
{
protect
};
\ No newline at end of file
IT18021080/Telemedicine-Chat-App/backend/routes/userRoutes.js
View file @
61a8047c
const
express
=
require
(
"
express
"
);
const
express
=
require
(
"
express
"
);
const
{
registerUser
,
authUser
,
allUsers
}
=
require
(
"
../controllers/userControllers
"
);
const
{
registerUser
,
authUser
,
allUsers
}
=
require
(
"
../controllers/userControllers
"
);
const
{
protect
}
=
require
(
"
../middleware/authMiddleware
"
);
const
router
=
express
.
Router
();
const
router
=
express
.
Router
();
router
.
route
(
"
/
"
).
post
(
registerUser
).
get
(
allUsers
);
//User searching API end point
router
.
route
(
"
/
"
).
post
(
registerUser
).
get
(
protect
,
allUsers
);
//User searching API end point
router
.
post
(
"
/login
"
,
authUser
);
router
.
post
(
"
/login
"
,
authUser
);
...
...
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