feat: creates backend for the project
This commit is contained in:
30
backend/src/services/auth.ts
Normal file
30
backend/src/services/auth.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { pool } from '../db/pool';
|
||||
import { config } from '../config';
|
||||
import type { LoginRequest, MeResponse } from '@family-budget/shared';
|
||||
|
||||
export async function login(
|
||||
body: LoginRequest,
|
||||
): Promise<{ sessionId: string } | null> {
|
||||
if (body.login !== config.appUserLogin || body.password !== config.appUserPassword) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const sid = uuidv4();
|
||||
await pool.query(
|
||||
'INSERT INTO sessions (id) VALUES ($1)',
|
||||
[sid],
|
||||
);
|
||||
return { sessionId: sid };
|
||||
}
|
||||
|
||||
export async function logout(sessionId: string): Promise<void> {
|
||||
await pool.query(
|
||||
'UPDATE sessions SET is_active = FALSE WHERE id = $1',
|
||||
[sessionId],
|
||||
);
|
||||
}
|
||||
|
||||
export async function me(sessionId: string): Promise<MeResponse> {
|
||||
return { login: config.appUserLogin };
|
||||
}
|
||||
Reference in New Issue
Block a user