implement JWT tokens, regenerate docs and sqlc

This commit is contained in:
zardzul
2026-03-14 18:46:48 +01:00
parent 131aee8638
commit d5e608feeb
18 changed files with 441 additions and 82 deletions
+25 -48
View File
@@ -11,17 +11,6 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
const checkUserExistsByEmail = `-- name: CheckUserExistsByEmail :one
SELECT id FROM users WHERE user_mail = $1
`
func (q *Queries) CheckUserExistsByEmail(ctx context.Context, userMail string) (pgtype.UUID, error) {
row := q.db.QueryRow(ctx, checkUserExistsByEmail, userMail)
var id pgtype.UUID
err := row.Scan(&id)
return id, err
}
const createUser = `-- name: CreateUser :one
INSERT INTO users (id, user_name, user_mail, password) VALUES ($1, $2, $3, $4) RETURNING id
`
@@ -46,6 +35,31 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (pgtype.
return id, err
}
const getUserAuthByEmail = `-- name: GetUserAuthByEmail :one
SELECT id, user_name, user_mail, password
FROM users
WHERE user_mail = $1
`
type GetUserAuthByEmailRow struct {
ID pgtype.UUID `json:"id"`
UserName string `json:"user_name"`
UserMail string `json:"user_mail"`
Password string `json:"password"`
}
func (q *Queries) GetUserAuthByEmail(ctx context.Context, userMail string) (GetUserAuthByEmailRow, error) {
row := q.db.QueryRow(ctx, getUserAuthByEmail, userMail)
var i GetUserAuthByEmailRow
err := row.Scan(
&i.ID,
&i.UserName,
&i.UserMail,
&i.Password,
)
return i, err
}
const getUserByID = `-- name: GetUserByID :one
SELECT id, user_name, user_mail, created_at FROM users WHERE id = $1
`
@@ -80,28 +94,6 @@ func (q *Queries) GetUsernameByID(ctx context.Context, id pgtype.UUID) (string,
return user_name, err
}
const loginUser = `-- name: LoginUser :one
SELECT id, user_name, user_mail FROM users WHERE user_mail = $1 AND password = $2
`
type LoginUserParams struct {
UserMail string `json:"user_mail"`
Password string `json:"password"`
}
type LoginUserRow struct {
ID pgtype.UUID `json:"id"`
UserName string `json:"user_name"`
UserMail string `json:"user_mail"`
}
func (q *Queries) LoginUser(ctx context.Context, arg LoginUserParams) (LoginUserRow, error) {
row := q.db.QueryRow(ctx, loginUser, arg.UserMail, arg.Password)
var i LoginUserRow
err := row.Scan(&i.ID, &i.UserName, &i.UserMail)
return i, err
}
const updateUser = `-- name: UpdateUser :exec
UPDATE users SET user_name = $2, user_mail = $3, password = $4 WHERE id = $1
`
@@ -122,18 +114,3 @@ func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) error {
)
return err
}
const updateUserSession = `-- name: UpdateUserSession :exec
UPDATE users SET session_token = $2, session_expiry = $3 WHERE id = $1
`
type UpdateUserSessionParams struct {
ID pgtype.UUID `json:"id"`
SessionToken pgtype.Text `json:"session_token"`
SessionExpiry pgtype.Timestamp `json:"session_expiry"`
}
func (q *Queries) UpdateUserSession(ctx context.Context, arg UpdateUserSessionParams) error {
_, err := q.db.Exec(ctx, updateUserSession, arg.ID, arg.SessionToken, arg.SessionExpiry)
return err
}