check expiry

This commit is contained in:
vinchent 2024-08-19 22:35:40 +02:00
parent 4a756e850e
commit 1fce6f6a48
3 changed files with 8 additions and 4 deletions

View File

@ -55,14 +55,15 @@ func (m *DBModel) InsertToken(t *Token, u User) error {
} }
stmt = `INSERT INTO tokens stmt = `INSERT INTO tokens
(user_id, name, email, token_hash, created_at, updated_at) (user_id, name, email, token_hash, expiry_at, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?)` VALUES (?, ?, ?, ?, ?, ?, ?)`
_, err = m.DB.ExecContext(ctx, stmt, _, err = m.DB.ExecContext(ctx, stmt,
u.ID, u.ID,
u.LastName, u.LastName,
u.Email, u.Email,
t.Hash, t.Hash,
t.Expiry,
time.Now(), time.Now(),
time.Now(), time.Now(),
) )
@ -82,9 +83,9 @@ func (m *DBModel) GetUserForToken(token string) (*User, error) {
query := `SELECT u.id, u.first_name, u.last_name, u.email query := `SELECT u.id, u.first_name, u.last_name, u.email
FROM users u FROM users u
INNER JOIN tokens t on (u.id = t.user_id) INNER JOIN tokens t on (u.id = t.user_id)
WHERE t.token_hash = ?` WHERE t.token_hash = ? AND t.expiry_at > ?`
err := m.DB.QueryRowContext(ctx, query, tokenHash[:]).Scan( err := m.DB.QueryRowContext(ctx, query, tokenHash[:], time.Now()).Scan(
&user.ID, &user.ID,
&user.FirstName, &user.FirstName,
&user.LastName, &user.LastName,

View File

@ -0,0 +1 @@
drop_column("tokens, "expiry_at")

View File

@ -0,0 +1,2 @@
add_column("tokens", "expiry_at", "timestamp", {})