Skip to content

Commit

Permalink
add route to check user email verified status (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
juancwu authored Dec 6, 2024
2 parents 98d9fe8 + 32ed948 commit 429f0a4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
23 changes: 23 additions & 0 deletions backend/internal/server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"KonferCA/SPUR/db"
"KonferCA/SPUR/internal/jwt"
mw "KonferCA/SPUR/internal/middleware"

"github.com/jackc/pgx/v5/pgtype"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"golang.org/x/crypto/bcrypt"
)

Expand All @@ -18,6 +20,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 @@ -111,6 +114,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 429f0a4

Please sign in to comment.