mirror of
https://github.com/eduardolat/pgbackweb.git
synced 2026-04-26 06:08:12 -05:00
Add GetUserByToken method to auth service
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
|
||||
)
|
||||
|
||||
func (s *Service) GetUserByToken(
|
||||
ctx context.Context, token string,
|
||||
) (bool, dbgen.AuthServiceGetUserByTokenRow, error) {
|
||||
user, err := s.dbgen.AuthServiceGetUserByToken(
|
||||
ctx, dbgen.AuthServiceGetUserByTokenParams{
|
||||
Token: token,
|
||||
EncryptionKey: *s.env.PBW_ENCRYPTION_KEY,
|
||||
},
|
||||
)
|
||||
if err != nil && errors.Is(err, sql.ErrNoRows) {
|
||||
return false, user, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, user, err
|
||||
}
|
||||
|
||||
return true, user, nil
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
-- name: AuthServiceGetUserByToken :one
|
||||
SELECT
|
||||
users.*,
|
||||
sessions.id as session_id
|
||||
FROM sessions
|
||||
JOIN users ON users.id = sessions.user_id
|
||||
WHERE pgp_sym_decrypt(sessions.token, @encryption_key) = @token;
|
||||
Reference in New Issue
Block a user