Skip to content

Commit c372571

Browse files
committed
include expiry field on login handler
1 parent 12e1ffd commit c372571

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

cmd/api/admin.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ func (app *application) registerAdminHandler(c echo.Context) error {
7676

7777
}
7878

79-
token, err := app.store.NewToken(a.ID, 3*24*time.Hour, db.ScopeActivation)
79+
expiry := time.Now().Add(3 * 24 * time.Hour)
80+
81+
token, err := app.store.NewToken(a.ID, expiry, db.ScopeActivation)
8082
if err != nil {
8183
slog.Error("error generating new token", "error", err)
8284
return c.JSON(http.StatusInternalServerError, envelope{"error": "internal server error"})

cmd/api/tokens.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (app *application) createAuthenticationTokenHandler(c echo.Context) error {
4343
if !admin.Activated {
4444
return c.JSON(http.StatusBadRequest, envelope{"error": "admin not activated"})
4545
}
46-
46+
4747
pwd := db.Password{
4848
Hash: admin.PasswordHash,
4949
Plaintext: input.Password,
@@ -62,13 +62,14 @@ func (app *application) createAuthenticationTokenHandler(c echo.Context) error {
6262
return c.JSON(http.StatusUnauthorized, envelope{"error": "invalid phone number or password"})
6363
}
6464

65-
token, err := app.store.NewToken(admin.ID, 3*24*time.Hour, db.ScopeAuthentication)
65+
expiry := time.Now().Add(3 * 24 * time.Hour)
66+
token, err := app.store.NewToken(admin.ID, expiry, db.ScopeAuthentication)
6667
if err != nil {
6768
slog.Error("error generating new token", "error", err)
6869
return c.JSON(http.StatusInternalServerError, envelope{"error": "internal server error"})
6970
}
7071

71-
return c.JSON(http.StatusCreated, envelope{"token": token.Plaintext})
72+
return c.JSON(http.StatusCreated, envelope{"token": token.Plaintext, "expiry": expiry})
7273
}
7374

7475
func (app *application) createPasswordResetTokenHandler(c echo.Context) error {
@@ -102,7 +103,9 @@ func (app *application) createPasswordResetTokenHandler(c echo.Context) error {
102103
return c.JSON(http.StatusForbidden, envelope{"errors": "account not activated"})
103104
}
104105

105-
token, err := app.store.NewToken(admin.ID, 45*time.Minute, db.ScopePasswordReset)
106+
expiry := time.Now().Add(45 * time.Minute)
107+
108+
token, err := app.store.NewToken(admin.ID, expiry, db.ScopePasswordReset)
106109
if err != nil {
107110
slog.Error("error generating new token", "error", err)
108111
return c.JSON(http.StatusInternalServerError, envelope{"error": "internal server error"})
@@ -151,7 +154,9 @@ func (app *application) createActivationTokenHandler(c echo.Context) error {
151154
return c.JSON(http.StatusForbidden, envelope{"errors": "account already activated"})
152155
}
153156

154-
token, err := app.store.NewToken(admin.ID, 3*24*time.Hour, db.ScopeActivation)
157+
expiry := time.Now().Add(3 * 24 * time.Hour)
158+
159+
token, err := app.store.NewToken(admin.ID, expiry, db.ScopeActivation)
155160
if err != nil {
156161
slog.Error("error generating new token", "error", err)
157162
return c.JSON(http.StatusInternalServerError, envelope{"error": "internal server error"})

db/sqlc/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
type Store interface {
1212
Querier
13-
NewToken(id uuid.UUID, ttl time.Duration, scope string) (*TokenLoc, error)
13+
NewToken(id uuid.UUID, expiry time.Time, scope string) (*TokenLoc, error)
1414
BulkInsert(ctx context.Context, houses []HouseBulk) error
1515
TxnCreateTenant(ctx context.Context, args CreateTenantParams) error
1616
TxnUpdateTenantHouse(ctx context.Context, args UpdateTenantParams, prev_house_id uuid.UUID) error

db/sqlc/utils.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ type TokenLoc struct {
3636
Scope string `json:"-"`
3737
}
3838

39-
func generateToken(id uuid.UUID, ttl time.Duration, scope string) (*TokenLoc, error) {
39+
func generateToken(id uuid.UUID, expiry time.Time, scope string) (*TokenLoc, error) {
4040

4141
token := &TokenLoc{
4242
AdminID: id,
43-
Expiry: time.Now().Add(ttl),
43+
Expiry: expiry,
4444
Scope: scope,
4545
}
4646

@@ -68,8 +68,8 @@ func IsValidTokenPlaintext(tokenPlaintext string) (bool, error) {
6868
return true, nil
6969
}
7070

71-
func (s *SQLStore) NewToken(id uuid.UUID, ttl time.Duration, scope string) (*TokenLoc, error) {
72-
token, err := generateToken(id, ttl, scope)
71+
func (s *SQLStore) NewToken(id uuid.UUID, expiry time.Time, scope string) (*TokenLoc, error) {
72+
token, err := generateToken(id, expiry, scope)
7373
if err != nil {
7474
return nil, err
7575
}

0 commit comments

Comments
 (0)