mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-29 16:24:58 +00:00
code: session,dbal,get (2 files)
This commit is contained in:
@@ -2,38 +2,47 @@
|
||||
* @file create-session.ts
|
||||
* @description Create session operation
|
||||
*/
|
||||
import type { Session, CreateSessionInput, Result } from '../types';
|
||||
import type { InMemoryStore } from '../store/in-memory-store';
|
||||
import type { CreateSessionInput, Result, Session } from '../../types'
|
||||
import type { InMemoryStore } from '../../store/in-memory-store'
|
||||
import { validateSessionCreate } from '../../validation/validate-session-create'
|
||||
|
||||
/**
|
||||
* Create a new session in the store
|
||||
*/
|
||||
export async function createSession(
|
||||
export const createSession = async (
|
||||
store: InMemoryStore,
|
||||
input: CreateSessionInput
|
||||
): Promise<Result<Session>> {
|
||||
if (!input.userId) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'User ID required' } };
|
||||
}
|
||||
if (!store.users.has(input.userId)) {
|
||||
return { success: false, error: { code: 'NOT_FOUND', message: 'User not found' } };
|
||||
}
|
||||
if (input.ttlSeconds <= 0) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'TTL must be positive' } };
|
||||
): Promise<Result<Session>> => {
|
||||
const validationErrors = validateSessionCreate({
|
||||
userId: input.userId,
|
||||
token: input.token,
|
||||
expiresAt: input.expiresAt
|
||||
})
|
||||
|
||||
if (validationErrors.length > 0) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: validationErrors[0] } }
|
||||
}
|
||||
|
||||
if (!store.users.has(input.userId)) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: `User not found: ${input.userId}` } }
|
||||
}
|
||||
|
||||
if (store.sessionTokens.has(input.token)) {
|
||||
return { success: false, error: { code: 'CONFLICT', message: 'Session token already exists' } }
|
||||
}
|
||||
|
||||
const now = new Date()
|
||||
const session: Session = {
|
||||
id: store.generateId('session'),
|
||||
userId: input.userId,
|
||||
token: store.generateToken(),
|
||||
expiresAt: new Date(Date.now() + input.ttlSeconds * 1000),
|
||||
ipAddress: input.ipAddress ?? '',
|
||||
userAgent: input.userAgent ?? '',
|
||||
createdAt: new Date(),
|
||||
};
|
||||
token: input.token,
|
||||
expiresAt: input.expiresAt,
|
||||
createdAt: now,
|
||||
lastActivity: now
|
||||
}
|
||||
|
||||
store.sessions.set(session.id, session);
|
||||
store.sessionTokens.set(session.token, session.id);
|
||||
store.sessions.set(session.id, session)
|
||||
store.sessionTokens.set(session.token, session.id)
|
||||
|
||||
return { success: true, data: session };
|
||||
return { success: true, data: session }
|
||||
}
|
||||
|
||||
@@ -2,37 +2,45 @@
|
||||
* @file get-session.ts
|
||||
* @description Get session operations
|
||||
*/
|
||||
import type { Session, Result } from '../types';
|
||||
import type { InMemoryStore } from '../store/in-memory-store';
|
||||
import type { Result, Session } from '../../types'
|
||||
import type { InMemoryStore } from '../../store/in-memory-store'
|
||||
import { validateId } from '../../validation/validate-id'
|
||||
|
||||
/**
|
||||
* Get a session by ID
|
||||
*/
|
||||
export async function getSession(store: InMemoryStore, id: string): Promise<Result<Session>> {
|
||||
if (!id) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'ID required' } };
|
||||
export const getSession = async (store: InMemoryStore, id: string): Promise<Result<Session>> => {
|
||||
const idErrors = validateId(id)
|
||||
if (idErrors.length > 0) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: idErrors[0] } }
|
||||
}
|
||||
|
||||
const session = store.sessions.get(id);
|
||||
const session = store.sessions.get(id)
|
||||
if (!session) {
|
||||
return { success: false, error: { code: 'NOT_FOUND', message: `Session not found: ${id}` } };
|
||||
return { success: false, error: { code: 'NOT_FOUND', message: `Session not found: ${id}` } }
|
||||
}
|
||||
|
||||
return { success: true, data: session };
|
||||
if (session.expiresAt <= new Date()) {
|
||||
store.sessionTokens.delete(session.token)
|
||||
store.sessions.delete(id)
|
||||
return { success: false, error: { code: 'NOT_FOUND', message: `Session expired: ${id}` } }
|
||||
}
|
||||
|
||||
return { success: true, data: session }
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a session by token
|
||||
*/
|
||||
export async function getSessionByToken(store: InMemoryStore, token: string): Promise<Result<Session>> {
|
||||
export const getSessionByToken = async (store: InMemoryStore, token: string): Promise<Result<Session>> => {
|
||||
if (!token) {
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'Token required' } };
|
||||
return { success: false, error: { code: 'VALIDATION_ERROR', message: 'Token is required' } }
|
||||
}
|
||||
|
||||
const id = store.sessionTokens.get(token);
|
||||
const id = store.sessionTokens.get(token)
|
||||
if (!id) {
|
||||
return { success: false, error: { code: 'NOT_FOUND', message: 'Session not found' } };
|
||||
return { success: false, error: { code: 'NOT_FOUND', message: 'Session not found for token' } }
|
||||
}
|
||||
|
||||
return getSession(store, id);
|
||||
return getSession(store, id)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user