Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
treinamento-2025.1-backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
RICARDO PRADO FARIA
treinamento-2025.1-backend
Merge requests
!14
Issue#12 ADD middleware authenticator
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Issue#12 ADD middleware authenticator
issue-12/middleware-authenticator
into
develop
Overview
0
Commits
1
Pipelines
0
Changes
1
Merged
RICARDO PRADO FARIA
requested to merge
issue-12/middleware-authenticator
into
develop
2 months ago
Overview
0
Commits
1
Pipelines
0
Changes
1
Expand
1
0
1
Merge request reports
Viewing commit
c5f2c84a
Show latest version
1 file
+
31
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
c5f2c84a
Issue#12 ADD middleware authenticator
· c5f2c84a
Ricardo
authored
2 months ago
src/middleware/auth.ts
+
31
−
0
Options
import
{
type
Request
,
type
Response
,
type
NextFunction
}
from
"
express
"
;
import
jwt
,
{
type
JwtPayload
}
from
"
jsonwebtoken
"
;
interface
AuthenticatedRequest
extends
Request
{
user
?:
JwtPayload
|
string
;
}
export
const
tokenAuthenticator
=
async
(
req
:
Request
,
res
:
Response
,
next
:
NextFunction
):
Promise
<
void
>
=>
{
if
(
!
req
.
headers
.
authorization
)
{
res
.
status
(
401
).
json
({
message
:
"
Unauthorized
"
});
return
;
}
const
token
=
req
.
headers
.
authorization
?.
split
(
"
"
)[
1
];
//pega o token do cabecalho da requisicao, undefined se nao achar
if
(
!
token
)
{
res
.
status
(
401
).
json
({
message
:
"
Token Not Found
"
});
return
;
}
try
{
const
decoded
=
jwt
.
verify
(
token
,
process
.
env
[
"
APP_SECRET
"
]
as
string
)
as
JwtPayload
;
//valida o token com app_secret, e atribui como jwtpayload
const
reqAuth
=
req
as
AuthenticatedRequest
;
reqAuth
.
user
=
decoded
;
//req recebe o jwtpayload da validacao
next
();
}
catch
(
error
)
{
res
.
status
(
403
).
json
({
message
:
"
Invalid Token
"
});
}
};
\ No newline at end of file
Loading