1package server
2
3import (
4 "time"
5
6 "github.com/Azure/go-autorest/autorest/to"
7 "github.com/haileyok/cocoon/internal/helpers"
8 "github.com/haileyok/cocoon/models"
9 "github.com/labstack/echo/v4"
10)
11
12type ComAtprotoServerConfirmEmailRequest struct {
13 Email string `json:"email" validate:"required"`
14 Token string `json:"token" validate:"required"`
15}
16
17func (s *Server) handleServerConfirmEmail(e echo.Context) error {
18 ctx := e.Request().Context()
19 logger := s.logger.With("name", "handleServerConfirmEmail")
20
21 urepo := e.Get("repo").(*models.RepoActor)
22
23 var req ComAtprotoServerConfirmEmailRequest
24 if err := e.Bind(&req); err != nil {
25 logger.Error("error binding", "error", err)
26 return helpers.ServerError(e, nil)
27 }
28
29 if err := e.Validate(req); err != nil {
30 return helpers.InputError(e, nil)
31 }
32
33 if urepo.EmailVerificationCode == nil || urepo.EmailVerificationCodeExpiresAt == nil {
34 return helpers.ExpiredTokenError(e)
35 }
36
37 if *urepo.EmailVerificationCode != req.Token {
38 return helpers.InputError(e, to.StringPtr("InvalidToken"))
39 }
40
41 if time.Now().UTC().After(*urepo.EmailVerificationCodeExpiresAt) {
42 return helpers.ExpiredTokenError(e)
43 }
44
45 now := time.Now().UTC()
46
47 if err := s.db.Exec(ctx, "UPDATE repos SET email_verification_code = NULL, email_verification_code_expires_at = NULL, email_confirmed_at = ? WHERE did = ?", nil, now, urepo.Repo.Did).Error; err != nil {
48 logger.Error("error updating user", "error", err)
49 return helpers.ServerError(e, nil)
50 }
51
52 return e.NoContent(200)
53}