Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hospital-management system
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AF7626
hospital-management system
Commits
384e0b75
Commit
384e0b75
authored
1 month ago
by
AF7626
Browse files
Options
Downloads
Patches
Plain Diff
authorization working for admin
parent
256ad2fc
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
backend/controllers/adminController.js
+1
-1
1 addition, 1 deletion
backend/controllers/adminController.js
backend/middlewares/authAdmin.js
+41
-0
41 additions, 0 deletions
backend/middlewares/authAdmin.js
backend/routes/adminRoute.js
+3
-2
3 additions, 2 deletions
backend/routes/adminRoute.js
with
45 additions
and
3 deletions
backend/controllers/adminController.js
+
1
−
1
View file @
384e0b75
...
...
@@ -17,7 +17,7 @@ const loginAdmin = async (req, res) => {
if
(
email
===
process
.
env
.
ADMIN_EMAIL
&&
password
===
process
.
env
.
ADMIN_PASSWORD
)
{
//store the web token in the variable called with JWT secret token
const
token
=
jwt
.
sign
(
email
+
password
,
process
.
env
.
JWT_SECRET
)
console
.
log
(
token
);
//
console.log(token);
//send a response back json format
res
.
json
({
success
:
true
,
token
})
}
...
...
This diff is collapsed.
Click to expand it.
backend/middlewares/authAdmin.js
0 → 100644
+
41
−
0
View file @
384e0b75
import
jwt
from
'
jsonwebtoken
'
;
//create a function that authenticates the admin using token
const
authAdmin
=
async
(
req
,
res
,
next
)
=>
{
// get authorization token
const
authorization
=
req
.
headers
.
authorization
//If no token is provided, return an error message
if
(
!
authorization
||
!
authorization
.
startsWith
(
'
Bearer
'
))
{
return
res
.
json
({
success
:
false
,
message
:
'
User token missing or invalid!
'
})
}
else
{
const
AdminToken
=
authorization
.
split
(
'
'
)[
1
];
try
{
// verity token
const
decode_Token
=
jwt
.
verify
(
AdminToken
,
process
.
env
.
JWT_SECRET
)
req
.
decode_Token
=
decode_Token
;
//callback function
next
();
}
catch
(
error
)
{
console
.
log
(
error
);
res
.
json
({
success
:
false
,
message
:
error
.
message
})
}
}
}
//export the middleware
export
default
authAdmin
;
This diff is collapsed.
Click to expand it.
backend/routes/adminRoute.js
+
3
−
2
View file @
384e0b75
import
express
from
'
express
'
;
import
{
addDoctor
,
loginAdmin
}
from
'
../controllers/adminController.js
'
;
import
{
addDoctor
,
loginAdmin
}
from
'
../controllers/adminController.js
'
;
import
authAdmin
from
'
../middlewares/authAdmin.js
'
;
// Create a new router instance for handling admin-related routes
const
adminRouter
=
express
.
Router
();
// This handles POST requests to the endpoint: http://localhost:3000/api/v1/admin/add-doctor
adminRouter
.
post
(
'
/add-doctor
'
,
addDoctor
);
adminRouter
.
post
(
'
/add-doctor
'
,
authAdmin
,
addDoctor
);
adminRouter
.
post
(
'
/login
'
,
loginAdmin
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment