mirror of
https://github.com/spchcap/speech.capital.git
synced 2026-01-14 16:48:44 +00:00
Feat: Add user role to auth cookie
This commit is contained in:
@@ -10,7 +10,7 @@ export async function onRequestPost({ request, env }) {
|
|||||||
const { username, pass_hash } = await request.json();
|
const { username, pass_hash } = await request.json();
|
||||||
if (!username || !pass_hash) return json({ error: 'Missing fields' }, { status: 400 });
|
if (!username || !pass_hash) return json({ error: 'Missing fields' }, { status: 400 });
|
||||||
|
|
||||||
const user = await env.D1_SPCHCAP.prepare('SELECT id, pass_hash FROM users WHERE username = ?').bind(username).first();
|
const user = await env.D1_SPCHCAP.prepare('SELECT id, pass_hash, role FROM users WHERE username = ?').bind(username).first();
|
||||||
if (!user || !tsEq(user.pass_hash, pass_hash)) return json({ error: 'Invalid credentials' }, { status: 401 });
|
if (!user || !tsEq(user.pass_hash, pass_hash)) return json({ error: 'Invalid credentials' }, { status: 401 });
|
||||||
|
|
||||||
const exp = new Date(Date.now() + 2592e6); // 30 days
|
const exp = new Date(Date.now() + 2592e6); // 30 days
|
||||||
@@ -18,6 +18,7 @@ export async function onRequestPost({ request, env }) {
|
|||||||
const headers = new Headers();
|
const headers = new Headers();
|
||||||
headers.append('Set-Cookie', `auth_user=${username}; ${opts}`);
|
headers.append('Set-Cookie', `auth_user=${username}; ${opts}`);
|
||||||
headers.append('Set-Cookie', `auth_hash=${user.pass_hash}; ${opts}`);
|
headers.append('Set-Cookie', `auth_hash=${user.pass_hash}; ${opts}`);
|
||||||
|
headers.append('Set-Cookie', `auth_role=${user.role}; ${opts}`);
|
||||||
|
|
||||||
return json({ success: true }, { headers });
|
return json({ success: true }, { headers });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user