website refactor
This commit is contained in:
@@ -6,14 +6,15 @@
|
||||
*/
|
||||
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { DomainError, Service } from '@/lib/contracts/services/Service';
|
||||
import { LoginPageDTO } from './types/LoginPageDTO';
|
||||
import { ForgotPasswordPageDTO } from './types/ForgotPasswordPageDTO';
|
||||
import { ResetPasswordPageDTO } from './types/ResetPasswordPageDTO';
|
||||
import { SignupPageDTO } from './types/SignupPageDTO';
|
||||
import { AuthPageParams } from './AuthPageParams';
|
||||
|
||||
export class AuthPageService {
|
||||
async processLoginParams(params: AuthPageParams): Promise<Result<LoginPageDTO, string>> {
|
||||
export class AuthPageService implements Service {
|
||||
async processLoginParams(params: AuthPageParams): Promise<Result<LoginPageDTO, DomainError>> {
|
||||
try {
|
||||
const returnTo = params.returnTo ?? '/dashboard';
|
||||
const hasInsufficientPermissions = params.returnTo !== null;
|
||||
@@ -23,38 +24,38 @@ export class AuthPageService {
|
||||
hasInsufficientPermissions,
|
||||
});
|
||||
} catch (error) {
|
||||
return Result.err('Failed to process login parameters');
|
||||
return Result.err({ type: 'unknown', message: 'Failed to process login parameters' });
|
||||
}
|
||||
}
|
||||
|
||||
async processForgotPasswordParams(params: AuthPageParams): Promise<Result<ForgotPasswordPageDTO, string>> {
|
||||
async processForgotPasswordParams(params: AuthPageParams): Promise<Result<ForgotPasswordPageDTO, DomainError>> {
|
||||
try {
|
||||
const returnTo = params.returnTo ?? '/auth/login';
|
||||
return Result.ok({ returnTo });
|
||||
} catch (error) {
|
||||
return Result.err('Failed to process forgot password parameters');
|
||||
return Result.err({ type: 'unknown', message: 'Failed to process forgot password parameters' });
|
||||
}
|
||||
}
|
||||
|
||||
async processResetPasswordParams(params: AuthPageParams): Promise<Result<ResetPasswordPageDTO, string>> {
|
||||
async processResetPasswordParams(params: AuthPageParams): Promise<Result<ResetPasswordPageDTO, DomainError>> {
|
||||
try {
|
||||
const token = params.token;
|
||||
if (!token) {
|
||||
return Result.err('Missing reset token');
|
||||
return Result.err({ type: 'validation', message: 'Missing reset token' });
|
||||
}
|
||||
const returnTo = params.returnTo ?? '/auth/login';
|
||||
return Result.ok({ token, returnTo });
|
||||
} catch (error) {
|
||||
return Result.err('Failed to process reset password parameters');
|
||||
return Result.err({ type: 'unknown', message: 'Failed to process reset password parameters' });
|
||||
}
|
||||
}
|
||||
|
||||
async processSignupParams(params: AuthPageParams): Promise<Result<SignupPageDTO, string>> {
|
||||
async processSignupParams(params: AuthPageParams): Promise<Result<SignupPageDTO, DomainError>> {
|
||||
try {
|
||||
const returnTo = params.returnTo ?? '/onboarding';
|
||||
return Result.ok({ returnTo });
|
||||
} catch (error) {
|
||||
return Result.err('Failed to process signup parameters');
|
||||
return Result.err({ type: 'unknown', message: 'Failed to process signup parameters' });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,53 +1,87 @@
|
||||
import { AuthApiClient } from '@/lib/api/auth/AuthApiClient';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { DomainError, Service } from '@/lib/contracts/services/Service';
|
||||
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
||||
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
|
||||
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
||||
import type { AuthSessionDTO } from '@/lib/types/generated/AuthSessionDTO';
|
||||
import type { LoginParamsDTO } from '@/lib/types/generated/LoginParamsDTO';
|
||||
import type { SignupParamsDTO } from '@/lib/types/generated/SignupParamsDTO';
|
||||
import type { ForgotPasswordDTO } from '@/lib/types/generated/ForgotPasswordDTO';
|
||||
import type { ResetPasswordDTO } from '@/lib/types/generated/ResetPasswordDTO';
|
||||
import { isProductionEnvironment } from '@/lib/config/env';
|
||||
|
||||
/**
|
||||
* Auth Service
|
||||
*
|
||||
* Orchestrates authentication operations.
|
||||
* Calls AuthApiClient for API calls.
|
||||
* Returns raw API DTOs. No ViewModels or UX logic.
|
||||
*/
|
||||
|
||||
import { AuthApiClient } from '@/lib/api/auth/AuthApiClient';
|
||||
import { SessionViewModel } from '@/lib/view-models/SessionViewModel';
|
||||
import { AuthSessionDTO } from '@/lib/types/generated/AuthSessionDTO';
|
||||
import { LoginParamsDTO } from '@/lib/types/generated/LoginParamsDTO';
|
||||
import { SignupParamsDTO } from '@/lib/types/generated/SignupParamsDTO';
|
||||
import { ForgotPasswordDTO } from '@/lib/types/generated/ForgotPasswordDTO';
|
||||
import { ResetPasswordDTO } from '@/lib/types/generated/ResetPasswordDTO';
|
||||
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
||||
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
|
||||
|
||||
export class AuthService {
|
||||
export class AuthService implements Service {
|
||||
private apiClient: AuthApiClient;
|
||||
|
||||
constructor() {
|
||||
const baseUrl = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3001';
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const logger = new ConsoleLogger();
|
||||
const errorReporter = new EnhancedErrorReporter(logger, {
|
||||
showUserNotifications: false,
|
||||
logToConsole: true,
|
||||
reportToExternal: process.env.NODE_ENV === 'production',
|
||||
reportToExternal: isProductionEnvironment(),
|
||||
});
|
||||
this.apiClient = new AuthApiClient(baseUrl, errorReporter, logger);
|
||||
}
|
||||
|
||||
async login(params: LoginParamsDTO): Promise<SessionViewModel> {
|
||||
const dto = await this.apiClient.login(params);
|
||||
return new SessionViewModel(dto.user);
|
||||
async login(params: LoginParamsDTO): Promise<Result<AuthSessionDTO, DomainError>> {
|
||||
try {
|
||||
const dto = await this.apiClient.login(params);
|
||||
return Result.ok(dto);
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'unauthorized', message: (error as Error).message || 'Login failed' });
|
||||
}
|
||||
}
|
||||
|
||||
async signup(params: SignupParamsDTO): Promise<SessionViewModel> {
|
||||
const dto = await this.apiClient.signup(params);
|
||||
return new SessionViewModel(dto.user);
|
||||
async signup(params: SignupParamsDTO): Promise<Result<AuthSessionDTO, DomainError>> {
|
||||
try {
|
||||
const dto = await this.apiClient.signup(params);
|
||||
return Result.ok(dto);
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Signup failed' });
|
||||
}
|
||||
}
|
||||
|
||||
async logout(): Promise<void> {
|
||||
await this.apiClient.logout();
|
||||
async logout(): Promise<Result<void, DomainError>> {
|
||||
try {
|
||||
await this.apiClient.logout();
|
||||
return Result.ok(undefined);
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Logout failed' });
|
||||
}
|
||||
}
|
||||
|
||||
async forgotPassword(params: ForgotPasswordDTO): Promise<{ message: string; magicLink?: string }> {
|
||||
return await this.apiClient.forgotPassword(params);
|
||||
async forgotPassword(params: ForgotPasswordDTO): Promise<Result<{ message: string; magicLink?: string }, DomainError>> {
|
||||
try {
|
||||
const result = await this.apiClient.forgotPassword(params);
|
||||
return Result.ok(result);
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Forgot password request failed' });
|
||||
}
|
||||
}
|
||||
|
||||
async resetPassword(params: ResetPasswordDTO): Promise<{ message: string }> {
|
||||
return await this.apiClient.resetPassword(params);
|
||||
async resetPassword(params: ResetPasswordDTO): Promise<Result<{ message: string }, DomainError>> {
|
||||
try {
|
||||
const result = await this.apiClient.resetPassword(params);
|
||||
return Result.ok(result);
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Reset password failed' });
|
||||
}
|
||||
}
|
||||
|
||||
async getSession(): Promise<Result<AuthSessionDTO | null, DomainError>> {
|
||||
try {
|
||||
const dto = await this.apiClient.getSession();
|
||||
return Result.ok(dto);
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to fetch session' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,25 @@
|
||||
import { AuthApiClient } from '@/lib/api/auth/AuthApiClient';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { DomainError, Service } from '@/lib/contracts/services/Service';
|
||||
import { AuthService } from './AuthService';
|
||||
import type { AuthSessionDTO } from '@/lib/types/generated/AuthSessionDTO';
|
||||
import { SessionViewModel } from '@/lib/view-models/SessionViewModel';
|
||||
|
||||
/**
|
||||
* Session Service
|
||||
*
|
||||
* Returns SessionViewModel for client consumption.
|
||||
* Orchestrates session-related operations.
|
||||
* Returns raw API DTOs. No ViewModels or UX logic.
|
||||
*/
|
||||
export class SessionService {
|
||||
constructor(
|
||||
private readonly apiClient: AuthApiClient
|
||||
) {}
|
||||
export class SessionService implements Service {
|
||||
private authService: AuthService;
|
||||
|
||||
constructor() {
|
||||
this.authService = new AuthService();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current user session (returns ViewModel)
|
||||
* Get current user session
|
||||
*/
|
||||
async getSession(): Promise<SessionViewModel | null> {
|
||||
const dto = await this.apiClient.getSession();
|
||||
if (!dto) return null;
|
||||
return new SessionViewModel(dto.user);
|
||||
async getSession(): Promise<Result<AuthSessionDTO | null, DomainError>> {
|
||||
return this.authService.getSession();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user