Skip to content

Commit

Permalink
Merge branch 'main' into feat/77/send-verification-email
Browse files Browse the repository at this point in the history
Signed-off-by: Jc <[email protected]>
  • Loading branch information
juancwu authored Dec 6, 2024
2 parents dd6b813 + 429f0a4 commit 92601dc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
21 changes: 21 additions & 0 deletions backend/internal/server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (s *Server) setupAuthRoutes() {
auth.Use(s.authLimiter.RateLimit()) // special rate limit for auth routes
auth.POST("/signup", s.handleSignup, mw.ValidateRequestBody(reflect.TypeOf(SignupRequest{})))
auth.POST("/signin", s.handleSignin, mw.ValidateRequestBody(reflect.TypeOf(SigninRequest{})))
auth.GET("/ami-verified", s.handleEmailVerifiedStatus)
}

func (s *Server) handleSignup(c echo.Context) error {
Expand Down Expand Up @@ -144,6 +145,26 @@ func (s *Server) handleSignin(c echo.Context) error {
})
}

/*
handleEmailVerifiedStatus checks for the email_verified column of the given email.
If the email does not exist in the users table, it returns false. The same goes
for any error encountered.
*/
func (s *Server) handleEmailVerifiedStatus(c echo.Context) error {
email := c.QueryParam("email")
if email == "" {
return echo.NewHTTPError(http.StatusBadRequest, "Missing email in query param.")
}

user, err := db.New(s.DBPool).GetUserByEmail(c.Request().Context(), email)
if err != nil {
log.Error().Err(err).Msg("Failed to fetch user when checking email verified status.")
return c.JSON(http.StatusOK, EmailVerifiedStatusResponse{Verified: false})
}

return c.JSON(http.StatusOK, EmailVerifiedStatusResponse{Verified: user.EmailVerified})
}

// helper function to convert pgtype.Text to *string
func getStringPtr(t pgtype.Text) *string {
if !t.Valid {
Expand Down
22 changes: 22 additions & 0 deletions backend/internal/server/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,26 @@ func TestAuth(t *testing.T) {
s.echoInstance.ServeHTTP(rec, req)
assert.Equal(t, http.StatusUnauthorized, rec.Code)
})

// TODO: re-order this test when the verification for the email tests are merged.
// this tets has to run before it to pass
// Then a new test should be added to cover the status when it is 'true'
t.Run("email verified status", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/api/v1/auth/[email protected]", nil)
rec := httptest.NewRecorder()
s.echoInstance.ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)

var response EmailVerifiedStatusResponse
err := json.Unmarshal(rec.Body.Bytes(), &response)
assert.Nil(t, err)
assert.False(t, response.Verified)
})

t.Run("email verified status - missing email query param", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/api/v1/auth/ami-verified", nil)
rec := httptest.NewRecorder()
s.echoInstance.ServeHTTP(rec, req)
assert.Equal(t, http.StatusBadRequest, rec.Code)
})
}
4 changes: 4 additions & 0 deletions backend/internal/server/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ type UpdateMeetingRequest struct {
Notes *string `json:"notes"`
}

type EmailVerifiedStatusResponse struct {
Verified bool `json:"verified"`
}

type CreateProjectFileRequest struct {
FileType string `json:"file_type" validate:"required"`
FileURL string `json:"file_url" validate:"required,url,s3_url"`
Expand Down

0 comments on commit 92601dc

Please sign in to comment.