Skip to content

Commit 12e1ffd

Browse files
committed
refactor login & prevent login when not activated
1 parent e6ece0e commit 12e1ffd

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

cmd/api/tokens.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func (app *application) createAuthenticationTokenHandler(c echo.Context) error {
1616

1717
var input struct {
18-
Phone string `json:"phone" validate:"required,len=10"`
18+
Email string `json:"email" validate:"required,email"`
1919
Password string `json:"password" validate:"required,min=8"`
2020
}
2121

@@ -27,19 +27,23 @@ func (app *application) createAuthenticationTokenHandler(c echo.Context) error {
2727
return c.JSON(http.StatusBadRequest, envelope{"error": err.Error()})
2828
}
2929

30-
admin, err := app.store.GetAdminByPhone(c.Request().Context(), input.Phone)
30+
admin, err := app.store.GetAdminByPhone(c.Request().Context(), input.Email)
3131

3232
if err != nil {
3333
switch {
3434
case errors.Is(err, sql.ErrNoRows):
35-
slog.Error("error fetching admin by phone", "error", err)
36-
return c.JSON(http.StatusNotFound, envelope{"error": "invalid phone number or password"})
35+
slog.Error("error fetching admin by email", "error", err)
36+
return c.JSON(http.StatusNotFound, envelope{"error": "invalid email number or password"})
3737
default:
3838
slog.Error("error fetching admin by phone number", "error", err)
3939
return c.JSON(http.StatusInternalServerError, envelope{"error": "internal server error"})
4040
}
4141
}
4242

43+
if !admin.Activated {
44+
return c.JSON(http.StatusBadRequest, envelope{"error": "admin not activated"})
45+
}
46+
4347
pwd := db.Password{
4448
Hash: admin.PasswordHash,
4549
Plaintext: input.Password,

0 commit comments

Comments
 (0)