Separate login and display identities
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
import type { Snippet } from 'svelte';
|
||||
|
||||
let { user, isacAccess, children }: {
|
||||
user: { username: string; role: string } | null;
|
||||
user: { username: string; displayName: string; role: string } | null;
|
||||
isacAccess: boolean;
|
||||
children: Snippet;
|
||||
} = $props();
|
||||
@@ -33,7 +33,7 @@
|
||||
</nav>
|
||||
{#if user}
|
||||
<div class="account">
|
||||
<a href="/account"><small><i></i> AGENT ONLINE</small>{user.username}</a>
|
||||
<a href="/account"><small><i></i> AGENT ONLINE</small>{user.displayName}</a>
|
||||
<PendingForm action="/sign-in?/logout">{#snippet children(pending)}<button aria-busy={pending}>SIGN OUT →</button>{/snippet}</PendingForm>
|
||||
</div>
|
||||
{:else}
|
||||
|
||||
@@ -8,8 +8,9 @@ export async function softDeleteAccount(
|
||||
reason: string | null
|
||||
): Promise<boolean> {
|
||||
const user = await (await database.prepare(`
|
||||
SELECT users.username,deleted_accounts.deleted_at
|
||||
FROM users LEFT JOIN deleted_accounts ON deleted_accounts.user_id=users.id
|
||||
SELECT users.username,user_profiles.display_name,deleted_accounts.deleted_at
|
||||
FROM users LEFT JOIN user_profiles ON user_profiles.user_id=users.id
|
||||
LEFT JOIN deleted_accounts ON deleted_accounts.user_id=users.id
|
||||
WHERE users.id=?
|
||||
`)).get(userId);
|
||||
if (!user || user.deleted_at) return false;
|
||||
@@ -22,8 +23,13 @@ export async function softDeleteAccount(
|
||||
await (await database.prepare(
|
||||
`UPDATE users SET username=?,password_hash=NULL,discord_id=NULL,role='user' WHERE id=?`
|
||||
)).run(deletedUsername, userId);
|
||||
await (await database.prepare(
|
||||
`INSERT INTO user_profiles(user_id,display_name,updated_at) VALUES(?,?,?)
|
||||
ON CONFLICT(user_id) DO UPDATE SET display_name=excluded.display_name,updated_at=excluded.updated_at`
|
||||
)).run(userId, 'Deleted Agent', now);
|
||||
await (await database.prepare('DELETE FROM sessions WHERE user_id=?')).run(userId);
|
||||
await (await database.prepare('DELETE FROM oauth_states WHERE user_id=?')).run(userId);
|
||||
await (await database.prepare('DELETE FROM discord_profiles WHERE user_id=?')).run(userId);
|
||||
await (await database.prepare('DELETE FROM user_roles WHERE user_id=?')).run(userId);
|
||||
await (await database.prepare('DELETE FROM user_permissions WHERE user_id=?')).run(userId);
|
||||
await (await database.prepare(
|
||||
@@ -34,8 +40,8 @@ export async function softDeleteAccount(
|
||||
'user',
|
||||
userId,
|
||||
'account.soft_deleted',
|
||||
JSON.stringify({ username: user.username }),
|
||||
JSON.stringify({ username: deletedUsername, reason }),
|
||||
JSON.stringify({ username: user.username, displayName: user.display_name }),
|
||||
JSON.stringify({ username: deletedUsername, displayName: 'Deleted Agent', reason }),
|
||||
now
|
||||
);
|
||||
return true;
|
||||
|
||||
@@ -9,6 +9,7 @@ const SESSION_SECONDS = 60 * 60 * 24 * 30;
|
||||
export type AuthUser = {
|
||||
id: string;
|
||||
username: string;
|
||||
displayName: string;
|
||||
role: string;
|
||||
discordLinked: boolean;
|
||||
};
|
||||
@@ -55,11 +56,13 @@ export async function getSessionUser(cookies: Cookies): Promise<AuthUser | null>
|
||||
|
||||
const database = await getDatabase();
|
||||
const statement = await database.prepare(`
|
||||
SELECT users.id, users.username, users.role, users.discord_id,
|
||||
SELECT users.id, users.username, COALESCE(user_profiles.display_name,users.username) AS display_name,
|
||||
users.role, users.discord_id,
|
||||
user_status.last_active_at,user_status.blocked_at,user_status.banned_until,
|
||||
deleted_accounts.deleted_at
|
||||
FROM sessions
|
||||
JOIN users ON users.id = sessions.user_id
|
||||
LEFT JOIN user_profiles ON user_profiles.user_id=users.id
|
||||
LEFT JOIN user_status ON user_status.user_id=users.id
|
||||
LEFT JOIN deleted_accounts ON deleted_accounts.user_id=users.id
|
||||
WHERE sessions.token_hash = ? AND sessions.expires_at > ?
|
||||
@@ -86,6 +89,7 @@ export async function getSessionUser(cookies: Cookies): Promise<AuthUser | null>
|
||||
return {
|
||||
id: String(user.id),
|
||||
username: String(user.username),
|
||||
displayName: String(user.display_name),
|
||||
role: String(user.role),
|
||||
discordLinked: typeof user.discord_id === 'string'
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user