Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: go: migrated /user/email/send-verification-email #485

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions go/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ paths:
summary: Change user email
tags:
- user
- email-and-password
- email
security:
- BearerAuth: []
requestBody:
Expand All @@ -185,13 +185,34 @@ paths:
schema:
$ref: '#/components/schemas/OKResponse'

/user/email/send-verification-email:
post:
summary: Send email verification email
tags:
- user
- email
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/UserEmailSendVerificationEmailRequest'
required: true
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/OKResponse'
description: >-
Email verification email sent successfully

/user/password/reset:
post:
summary: >-
Request a password reset. An email with a verification link will be sent to the user's address
tags:
- user
- email-and-password
- password
requestBody:
content:
application/json:
Expand Down Expand Up @@ -289,6 +310,7 @@ components:
- disabled-endpoint
- disabled-user
- email-already-in-use
- email-already-verified
- forbidden-anonymous
- internal-server-error
- invalid-email-password
Expand Down Expand Up @@ -446,6 +468,20 @@ components:
required:
- newEmail

UserEmailSendVerificationEmailRequest:
type: object
additionalProperties: false
properties:
email:
description: A valid email
example: [email protected]
format: email
type: string
options:
$ref: "#/components/schemas/OptionsRedirectTo"
required:
- email

UserPasswordResetRequest:
type: object
additionalProperties: false
Expand Down
147 changes: 109 additions & 38 deletions go/api/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions go/api/types.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions go/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ package controller
import (
"context"
"fmt"
"time"

"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/nhost/hasura-auth/go/notifications"
"github.com/nhost/hasura-auth/go/sql"
)

const (
In30Days = 720 * time.Hour
)

func deptr[T any](x *T) T { //nolint:ireturn
if x == nil {
return *new(T)
Expand Down
14 changes: 14 additions & 0 deletions go/controller/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
ErrInvalidRequest = &APIError{api.InvalidRequest}
ErrSignupDisabled = &APIError{api.SignupDisabled}
ErrDisabledEndpoint = &APIError{api.DisabledEndpoint}
ErrEmailAlreadyVerified = &APIError{api.EmailAlreadyVerified}
)

func logError(err error) slog.Attr {
Expand Down Expand Up @@ -73,6 +74,12 @@ func (response ErrorResponse) VisitPostUserPasswordResetResponse(w http.Response
return response.visit(w)
}

func (response ErrorResponse) VisitPostUserEmailSendVerificationEmailResponse(
w http.ResponseWriter,
) error {
return response.visit(w)
}

func (response ErrorResponse) VisitPostPatResponse(w http.ResponseWriter) error {
return response.visit(w)
}
Expand All @@ -82,6 +89,7 @@ func isSensitive(err api.ErrorResponseError) bool {
case
api.DisabledUser,
api.EmailAlreadyInUse,
api.EmailAlreadyVerified,
api.ForbiddenAnonymous,
api.InvalidEmailPassword,
api.InvalidPat,
Expand Down Expand Up @@ -141,6 +149,12 @@ func (ctrl *Controller) sendError( //nolint:funlen,cyclop
Error: err.t,
Message: "Email already in use",
}
case api.EmailAlreadyVerified:
return ErrorResponse{
Status: http.StatusBadRequest,
Error: err.t,
Message: "User's email is already verified",
}
case api.ForbiddenAnonymous:
return ErrorResponse{
Status: http.StatusForbidden,
Expand Down
2 changes: 1 addition & 1 deletion go/controller/post_signin_passwordless_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (ctrl *Controller) PostSigninPasswordlessEmail( //nolint:ireturn
"",
logger,
); err != nil {
return nil, err
return ctrl.sendError(err), nil
}

return api.PostSigninPasswordlessEmail200JSONResponse(api.OK), nil
Expand Down
2 changes: 1 addition & 1 deletion go/controller/post_signup_email_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (ctrl *Controller) postSignupEmailPasswordWithEmailVerificationOrUserDisabl
"",
logger,
); err != nil {
return nil, err
return ctrl.sendError(err), nil
}

return api.PostSignupEmailPassword200JSONResponse{Session: nil}, nil
Expand Down
2 changes: 1 addition & 1 deletion go/controller/post_user_email_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (ctrl *Controller) PostUserEmailChange( //nolint:ireturn
string(request.Body.NewEmail),
logger,
); err != nil {
return nil, err
return ctrl.sendError(err), nil
}

return api.PostUserEmailChange200JSONResponse(api.OK), nil
Expand Down
Loading
Loading