module creation

This commit is contained in:
2025-12-15 21:44:06 +01:00
parent b834f88bbd
commit 7c7267da72
88 changed files with 12119 additions and 4241 deletions

View File

@@ -1,59 +1,47 @@
import { cookies } from 'next/headers';
import { randomUUID } from 'crypto';
import type { AuthenticatedUserDTO } from '../../application/dto/AuthenticatedUserDTO';
import type { AuthSessionDTO } from '../../application/dto/AuthSessionDTO';
import type { IdentitySessionPort } from '../../application/ports/IdentitySessionPort';
/**
* Adapter: CookieIdentitySessionAdapter
*
* Manages user session using cookies. This is a placeholder implementation.
*/
const SESSION_COOKIE = 'gp_demo_session';
function parseCookieValue(raw: string | undefined): AuthSessionDTO | null {
if (!raw) return null;
try {
const parsed = JSON.parse(raw) as AuthSessionDTO;
if (!parsed.expiresAt || Date.now() > parsed.expiresAt) {
return null;
}
return parsed;
} catch {
return null;
}
}
function serializeSession(session: AuthSessionDTO): string {
return JSON.stringify(session);
}
import type { ILogger } from '@gridpilot/shared/logging/ILogger';
import type { AuthenticatedUserDTO } from '@gridpilot/core/identity/application/dto/AuthenticatedUserDTO';
import type { AuthSessionDTO } from '@gridpilot/core/identity/application/dto/AuthSessionDTO';
import type { IdentitySessionPort } from '@gridpilot/core/identity/application/ports/IdentitySessionPort';
export class CookieIdentitySessionAdapter implements IdentitySessionPort {
private currentSession: AuthSessionDTO | null = null;
constructor(private readonly logger: ILogger) {
this.logger.info('CookieIdentitySessionAdapter initialized.');
// In a real application, you would load the session from a cookie here
// For demo, we'll start with no session.
}
async getCurrentSession(): Promise<AuthSessionDTO | null> {
const store = await cookies();
const raw = store.get(SESSION_COOKIE)?.value;
return parseCookieValue(raw);
this.logger.debug('[CookieIdentitySessionAdapter] Getting current session.');
return Promise.resolve(this.currentSession);
}
async createSession(user: AuthenticatedUserDTO): Promise<AuthSessionDTO> {
const now = Date.now();
const expiresAt = now + 24 * 60 * 60 * 1000;
const session: AuthSessionDTO = {
user,
issuedAt: now,
expiresAt,
token: randomUUID(),
this.logger.debug(`[CookieIdentitySessionAdapter] Creating session for user: ${user.id}`);
const newSession: AuthSessionDTO = {
user: user,
issuedAt: Date.now(),
expiresAt: Date.now() + 3600 * 1000, // 1 hour expiration
token: `mock-token-${user.id}-${Date.now()}`,
};
const store = await cookies();
store.set(SESSION_COOKIE, serializeSession(session), {
httpOnly: true,
sameSite: 'lax',
path: '/',
secure: process.env.NODE_ENV === 'production',
});
return session;
this.currentSession = newSession;
// In a real app, you'd set a secure, HTTP-only cookie here.
this.logger.info(`[CookieIdentitySessionAdapter] Session created for user ${user.id}.`);
return Promise.resolve(newSession);
}
async clearSession(): Promise<void> {
const store = await cookies();
store.delete(SESSION_COOKIE);
this.logger.debug('[CookieIdentitySessionAdapter] Clearing session.');
this.currentSession = null;
// In a real app, you'd clear the session cookie here.
this.logger.info('[CookieIdentitySessionAdapter] Session cleared.');
return Promise.resolve();
}
}
}