Skip to main content
Homepage
Explore
Search or go to…
/
Sign in
Explore
Primary navigation
Project
O
OCW Backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Confluence
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
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
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Collapse sidebar
Snippets
Groups
Projects
Show more breadcrumbs
IF3250_2023_K03_24_OPEN-COURSEWARE
OCW Backend
Merge requests
!12
Feat/s1 sb16 implementing reset password endpoint
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
Feat/s1 sb16 implementing reset password endpoint
feat/s1-sb16-implementing-reset-password-endpoint
into
staging
Overview
0
Commits
3
Pipelines
1
Changes
20
Merged
Feat/s1 sb16 implementing reset password endpoint
Nathanael Santoso
requested to merge
feat/s1-sb16-implementing-reset-password-endpoint
into
staging
Feb 19, 2023
Overview
0
Commits
3
Pipelines
1
Changes
20
0
0
Merge request reports
Compare
staging
staging (base)
and
latest version
latest version
9e10fe49
3 commits,
Feb 19, 2023
20 files
+
367
−
1
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
20
handler/reset/confirm.go
0 → 100644
+
91
−
0
View file @ 9e10fe49
Edit in single-file editor
Open in Web IDE
package
reset
import
(
"fmt"
"net/http"
"strings"
"github.com/go-playground/validator/v10"
"gitlab.informatika.org/ocw/ocw-backend/model/web"
"gitlab.informatika.org/ocw/ocw-backend/model/web/reset/confirm"
)
func
(
rs
ResetHandlerImpl
)
Confirm
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
payload
:=
confirm
.
ConfirmRequestPayload
{}
validate
:=
validator
.
New
()
// Validate payload
if
r
.
Header
.
Get
(
"Content-Type"
)
!=
"application/json"
{
payload
:=
rs
.
WrapperUtil
.
ErrorResponseWrap
(
"this service only receive json input"
,
nil
)
rs
.
HttpUtil
.
WriteJson
(
w
,
http
.
StatusUnsupportedMediaType
,
payload
)
return
}
if
err
:=
rs
.
HttpUtil
.
ParseJson
(
r
,
&
payload
);
err
!=
nil
{
payload
:=
rs
.
WrapperUtil
.
ErrorResponseWrap
(
"invalid json input"
,
err
.
Error
())
rs
.
HttpUtil
.
WriteJson
(
w
,
http
.
StatusUnprocessableEntity
,
payload
)
return
}
if
err
:=
validate
.
Struct
(
payload
);
err
!=
nil
{
if
_
,
ok
:=
err
.
(
*
validator
.
InvalidValidationError
);
ok
{
payload
:=
rs
.
WrapperUtil
.
ErrorResponseWrap
(
err
.
Error
(),
nil
)
rs
.
HttpUtil
.
WriteJson
(
w
,
http
.
StatusBadRequest
,
payload
)
return
}
errPayload
:=
web
.
NewResponseErrorFromValidator
(
err
.
(
validator
.
ValidationErrors
))
payload
:=
rs
.
WrapperUtil
.
ErrorResponseWrap
(
errPayload
.
Error
(),
errPayload
)
rs
.
HttpUtil
.
WriteJson
(
w
,
http
.
StatusBadRequest
,
payload
)
return
}
// Confirm Valid Website Token
confirmTokenHeader
:=
r
.
Header
.
Get
(
"Authorization"
)
if
confirmTokenHeader
==
""
{
payload
:=
rs
.
WrapperUtil
.
ErrorResponseWrap
(
"token is required"
,
nil
)
rs
.
HttpUtil
.
WriteJson
(
w
,
http
.
StatusForbidden
,
payload
)
return
}
token
:=
strings
.
Split
(
confirmTokenHeader
,
" "
)
if
len
(
token
)
!=
2
{
payload
:=
rs
.
WrapperUtil
.
ErrorResponseWrap
(
"invalid token"
,
nil
)
rs
.
HttpUtil
.
WriteJson
(
w
,
http
.
StatusBadRequest
,
payload
)
return
}
if
token
[
0
]
!=
"Bearer"
{
payload
:=
rs
.
WrapperUtil
.
ErrorResponseWrap
(
"invalid token"
,
nil
)
rs
.
HttpUtil
.
WriteJson
(
w
,
http
.
StatusBadRequest
,
payload
)
return
}
// Service to handle
payload
.
ConfirmToken
=
token
[
1
]
err
:=
rs
.
ResetService
.
Confirm
(
payload
)
if
err
!=
nil
{
if
strings
.
Contains
(
err
.
Error
(),
"expired/not exist"
)
{
err
=
web
.
NewResponseErrorFromError
(
err
,
web
.
EmailNotExist
)
}
respErr
,
ok
:=
err
.
(
web
.
ResponseError
)
if
ok
{
payload
:=
rs
.
WrapperUtil
.
ErrorResponseWrap
(
"email was not found"
,
respErr
)
rs
.
HttpUtil
.
WriteJson
(
w
,
http
.
StatusBadRequest
,
payload
)
}
else
{
rs
.
Logger
.
Error
(
fmt
.
Sprintf
(
"[RESET] some error happened when requesting password reset: %s"
,
err
.
Error
()),
)
payload
:=
rs
.
WrapperUtil
.
ErrorResponseWrap
(
"internal server error"
,
nil
)
rs
.
HttpUtil
.
WriteJson
(
w
,
http
.
StatusInternalServerError
,
payload
)
}
return
}
responsePayload
:=
rs
.
WrapperUtil
.
SuccessResponseWrap
(
nil
)
rs
.
HttpUtil
.
WriteSuccessJson
(
w
,
responsePayload
)
}
\ No newline at end of file
Loading