UPDATE: Auth module, User module
Some checks failed
Build and Release / release (push) Failing after 1m25s

This commit is contained in:
2026-03-30 00:27:57 +07:00
parent 92d44bb00c
commit f04441bf2a
59 changed files with 4246 additions and 521 deletions

View File

@@ -6,7 +6,6 @@ CREATE TABLE IF NOT EXISTS users (
password_hash TEXT,
google_id VARCHAR(255) UNIQUE,
auth_provider VARCHAR(50) NOT NULL DEFAULT 'local',
is_verified BOOLEAN NOT NULL DEFAULT false,
is_deleted BOOLEAN NOT NULL DEFAULT false,
token_version INT NOT NULL DEFAULT 1,
refresh_token TEXT,
@@ -22,10 +21,6 @@ CREATE INDEX idx_users_email_active
ON users (email)
WHERE is_deleted = false;
CREATE INDEX idx_users_verified
ON users (is_verified)
WHERE is_deleted = false;
CREATE OR REPLACE FUNCTION update_updated_at()
RETURNS TRIGGER AS $$
BEGIN

View File

@@ -1 +0,0 @@
DROP TABLE IF EXISTS user_tokens;

View File

@@ -1,25 +0,0 @@
CREATE TABLE IF NOT EXISTS user_tokens (
id UUID PRIMARY KEY DEFAULT uuidv7(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
token VARCHAR(255) NOT NULL UNIQUE,
token_type SMALLINT NOT NULL,
is_deleted BOOLEAN NOT NULL DEFAULT false,
expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE INDEX idx_user_tokens_token
ON user_tokens(token)
WHERE is_deleted = false;
CREATE INDEX idx_user_tokens_user_id
ON user_tokens(user_id)
WHERE is_deleted = false;
CREATE INDEX idx_user_tokens_type
ON user_tokens(token_type)
WHERE is_deleted = false;
CREATE INDEX idx_user_tokens_expires_at
ON user_tokens(expires_at)
WHERE is_deleted = false;

View File

@@ -11,11 +11,14 @@ WHERE name = $1 AND is_deleted = false;
SELECT id, name, is_deleted, created_at, updated_at FROM roles
WHERE id = $1 AND is_deleted = false;
-- name: GetRolesByIDs :many
SELECT id, name, is_deleted, created_at, updated_at
FROM roles
WHERE id = ANY($1::uuid[]) AND is_deleted = false;
-- name: AddUserRole :exec
INSERT INTO user_roles (user_id, role_id)
SELECT $1, r.id
FROM roles r
WHERE r.name = $2
SELECT $1, unnest($2::uuid[])
ON CONFLICT DO NOTHING;
-- name: RemoveUserRole :exec

View File

@@ -3,17 +3,14 @@ INSERT INTO users (
email,
password_hash,
google_id,
auth_provider,
is_verified
auth_provider
) VALUES (
$1, $2, $3, $4, $5
$1, $2, $3, $4
)
ON CONFLICT (email)
DO UPDATE SET
google_id = EXCLUDED.google_id,
auth_provider = EXCLUDED.auth_provider,
is_verified = users.is_verified OR EXCLUDED.is_verified,
updated_at = now()
auth_provider = EXCLUDED.auth_provider
RETURNING *;
-- name: CreateUserProfile :one
@@ -55,12 +52,6 @@ SET
WHERE id = $1
AND is_deleted = false;
-- name: VerifyUser :exec
UPDATE users
SET
is_verified = true
WHERE id = $1
AND is_deleted = false;
-- name: DeleteUser :exec
UPDATE users
@@ -79,7 +70,6 @@ SELECT
u.id,
u.email,
u.password_hash,
u.is_verified,
u.token_version,
u.refresh_token,
u.is_deleted,
@@ -116,6 +106,47 @@ SELECT
FROM users u
WHERE u.id = $1 AND u.is_deleted = false;
-- name: GetUserByIDWithoutDeleted :one
SELECT
u.id,
u.email,
u.password_hash,
u.token_version,
u.refresh_token,
u.is_deleted,
u.created_at,
u.updated_at,
-- profile JSON
(
SELECT json_build_object(
'display_name', p.display_name,
'full_name', p.full_name,
'avatar_url', p.avatar_url,
'bio', p.bio,
'location', p.location,
'website', p.website,
'country_code', p.country_code,
'phone', p.phone
)
FROM user_profiles p
WHERE p.user_id = u.id
) AS profile,
-- roles JSON
(
SELECT COALESCE(
json_agg(json_build_object('id', r.id, 'name', r.name)),
'[]'
)::json
FROM user_roles ur
JOIN roles r ON ur.role_id = r.id
WHERE ur.user_id = u.id
) AS roles
FROM users u
WHERE u.id = $1;
-- name: GetTokenVersion :one
SELECT token_version
FROM users
@@ -131,7 +162,6 @@ SELECT
u.id,
u.email,
u.password_hash,
u.is_verified,
u.token_version,
u.is_deleted,
u.created_at,
@@ -170,7 +200,59 @@ SELECT
u.id,
u.email,
u.password_hash,
u.is_verified,
u.token_version,
u.refresh_token,
u.is_deleted,
u.created_at,
u.updated_at,
-- profile JSON
(
SELECT json_build_object(
'display_name', p.display_name,
'full_name', p.full_name,
'avatar_url', p.avatar_url,
'bio', p.bio,
'location', p.location,
'website', p.website,
'country_code', p.country_code,
'phone', p.phone
)
FROM user_profiles p
WHERE p.user_id = u.id
) AS profile,
-- roles JSON
(
SELECT COALESCE(
json_agg(json_build_object('id', r.id, 'name', r.name)),
'[]'
)::json
FROM user_roles ur
JOIN roles r ON ur.role_id = r.id
WHERE ur.user_id = u.id
) AS roles
FROM users u
WHERE
(sqlc.narg('cursor')::uuid IS NULL OR u.id > sqlc.narg('cursor')::uuid)
AND (sqlc.narg('is_deleted')::boolean IS NULL OR u.is_deleted = sqlc.narg('is_deleted')::boolean)
AND (
sqlc.narg('role_ids')::uuid[] IS NULL OR
EXISTS (
SELECT 1 FROM user_roles ur2
WHERE ur2.user_id = u.id AND ur2.role_id = ANY(sqlc.narg('role_ids')::uuid[])
)
)
ORDER BY u.id ASC
LIMIT sqlc.arg('limit');
-- name: SearchUsers :many
SELECT
u.id,
u.email,
u.password_hash,
u.token_version,
u.refresh_token,
u.is_deleted,
@@ -203,4 +285,26 @@ SELECT
) AS roles
FROM users u
WHERE u.is_deleted = false;
WHERE
(sqlc.narg('cursor')::uuid IS NULL OR u.id > sqlc.narg('cursor')::uuid)
AND (sqlc.narg('is_deleted')::boolean IS NULL OR u.is_deleted = sqlc.narg('is_deleted')::boolean)
AND (
sqlc.narg('role_ids')::uuid[] IS NULL OR
EXISTS (
SELECT 1 FROM user_roles ur2
WHERE ur2.user_id = u.id AND ur2.role_id = ANY(sqlc.narg('role_ids')::uuid[])
)
)
AND (sqlc.narg('search_id')::uuid IS NULL OR u.id = sqlc.narg('search_id')::uuid)
AND (
sqlc.narg('search_text')::text IS NULL OR
u.email ILIKE '%' || sqlc.narg('search_text')::text || '%' OR
EXISTS (
SELECT 1 FROM user_profiles p
WHERE p.user_id = u.id AND p.display_name ILIKE '%' || sqlc.narg('search_text')::text || '%'
)
)
ORDER BY u.id ASC
LIMIT sqlc.arg('limit');

View File

@@ -4,7 +4,6 @@ CREATE TABLE IF NOT EXISTS users (
password_hash TEXT,
google_id VARCHAR(255) UNIQUE,
auth_provider VARCHAR(50) NOT NULL DEFAULT 'local',
is_verified BOOLEAN NOT NULL DEFAULT false,
is_deleted BOOLEAN NOT NULL DEFAULT false,
token_version INT NOT NULL DEFAULT 1,
refresh_token TEXT,
@@ -49,14 +48,4 @@ CREATE TABLE IF NOT EXISTS user_verifications (
reviewed_by UUID REFERENCES users(id),
reviewed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE IF NOT EXISTS user_tokens (
id UUID PRIMARY KEY DEFAULT uuidv7(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
token VARCHAR(255) NOT NULL UNIQUE,
is_deleted BOOLEAN NOT NULL DEFAULT false,
token_type SMALLINT NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ DEFAULT now()
);
);