Validating the token on the back end

This commit is contained in:
2024-08-19 22:22:40 +02:00
parent 7ef68d030b
commit 4a756e850e
3 changed files with 69 additions and 3 deletions

View File

@ -71,3 +71,27 @@ func (m *DBModel) InsertToken(t *Token, u User) error {
}
return nil
}
func (m *DBModel) GetUserForToken(token string) (*User, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
tokenHash := sha256.Sum256([]byte(token))
var user User
query := `SELECT u.id, u.first_name, u.last_name, u.email
FROM users u
INNER JOIN tokens t on (u.id = t.user_id)
WHERE t.token_hash = ?`
err := m.DB.QueryRowContext(ctx, query, tokenHash[:]).Scan(
&user.ID,
&user.FirstName,
&user.LastName,
&user.Email,
)
if err != nil {
return nil, err
}
return &user, nil
}