website refactor
This commit is contained in:
64
apps/website/lib/services/auth/AuthPageService.ts
Normal file
64
apps/website/lib/services/auth/AuthPageService.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* Auth Page Service
|
||||
*
|
||||
* Processes URL parameters for auth pages and provides structured data.
|
||||
* This is a composition service, not an API service.
|
||||
*/
|
||||
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { LoginPageDTO } from './types/LoginPageDTO';
|
||||
import { ForgotPasswordPageDTO } from './types/ForgotPasswordPageDTO';
|
||||
import { ResetPasswordPageDTO } from './types/ResetPasswordPageDTO';
|
||||
import { SignupPageDTO } from './types/SignupPageDTO';
|
||||
|
||||
export interface AuthPageParams {
|
||||
returnTo?: string | null;
|
||||
token?: string | null;
|
||||
}
|
||||
|
||||
export class AuthPageService {
|
||||
async processLoginParams(params: AuthPageParams): Promise<Result<LoginPageDTO, string>> {
|
||||
try {
|
||||
const returnTo = params.returnTo ?? '/dashboard';
|
||||
const hasInsufficientPermissions = params.returnTo !== null;
|
||||
|
||||
return Result.ok({
|
||||
returnTo,
|
||||
hasInsufficientPermissions,
|
||||
});
|
||||
} catch (error) {
|
||||
return Result.err('Failed to process login parameters');
|
||||
}
|
||||
}
|
||||
|
||||
async processForgotPasswordParams(params: AuthPageParams): Promise<Result<ForgotPasswordPageDTO, string>> {
|
||||
try {
|
||||
const returnTo = params.returnTo ?? '/auth/login';
|
||||
return Result.ok({ returnTo });
|
||||
} catch (error) {
|
||||
return Result.err('Failed to process forgot password parameters');
|
||||
}
|
||||
}
|
||||
|
||||
async processResetPasswordParams(params: AuthPageParams): Promise<Result<ResetPasswordPageDTO, string>> {
|
||||
try {
|
||||
const token = params.token;
|
||||
if (!token) {
|
||||
return Result.err('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');
|
||||
}
|
||||
}
|
||||
|
||||
async processSignupParams(params: AuthPageParams): Promise<Result<SignupPageDTO, string>> {
|
||||
try {
|
||||
const returnTo = params.returnTo ?? '/onboarding';
|
||||
return Result.ok({ returnTo });
|
||||
} catch (error) {
|
||||
return Result.err('Failed to process signup parameters');
|
||||
}
|
||||
}
|
||||
}
|
||||
53
apps/website/lib/services/auth/AuthService.ts
Normal file
53
apps/website/lib/services/auth/AuthService.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* Auth Service
|
||||
*
|
||||
* Orchestrates authentication operations.
|
||||
* Calls AuthApiClient for API calls.
|
||||
*/
|
||||
|
||||
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 {
|
||||
private apiClient: AuthApiClient;
|
||||
|
||||
constructor() {
|
||||
const baseUrl = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3001';
|
||||
const logger = new ConsoleLogger();
|
||||
const errorReporter = new EnhancedErrorReporter(logger, {
|
||||
showUserNotifications: false,
|
||||
logToConsole: true,
|
||||
reportToExternal: process.env.NODE_ENV === 'production',
|
||||
});
|
||||
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 signup(params: SignupParamsDTO): Promise<SessionViewModel> {
|
||||
const dto = await this.apiClient.signup(params);
|
||||
return new SessionViewModel(dto.user);
|
||||
}
|
||||
|
||||
async logout(): Promise<void> {
|
||||
await this.apiClient.logout();
|
||||
}
|
||||
|
||||
async forgotPassword(params: ForgotPasswordDTO): Promise<{ message: string; magicLink?: string }> {
|
||||
return await this.apiClient.forgotPassword(params);
|
||||
}
|
||||
|
||||
async resetPassword(params: ResetPasswordDTO): Promise<{ message: string }> {
|
||||
return await this.apiClient.resetPassword(params);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
import { SessionViewModel } from '@/lib/view-models/SessionViewModel';
|
||||
import { AuthApiClient } from '@/lib/api/auth/AuthApiClient';
|
||||
import type { AuthSessionDTO } from '@/lib/types/generated/AuthSessionDTO';
|
||||
import { SessionViewModel } from '@/lib/view-models/SessionViewModel';
|
||||
|
||||
/**
|
||||
* Session Service
|
||||
*
|
||||
* Orchestrates session operations by coordinating API calls and view model creation.
|
||||
* All dependencies are injected via constructor.
|
||||
* Returns SessionViewModel for client consumption.
|
||||
*/
|
||||
export class SessionService {
|
||||
constructor(
|
||||
@@ -13,10 +13,11 @@ export class SessionService {
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Get current user session with view model transformation
|
||||
* Get current user session (returns ViewModel)
|
||||
*/
|
||||
async getSession(): Promise<SessionViewModel | null> {
|
||||
const dto = await this.apiClient.getSession();
|
||||
return dto ? new SessionViewModel(dto.user) : null;
|
||||
if (!dto) return null;
|
||||
return new SessionViewModel(dto.user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Forgot Password Page DTO
|
||||
*
|
||||
* Data transfer object for forgot password page composition.
|
||||
* Used by AuthPageService and ForgotPasswordViewDataBuilder.
|
||||
*/
|
||||
|
||||
export interface ForgotPasswordPageDTO {
|
||||
returnTo: string;
|
||||
}
|
||||
11
apps/website/lib/services/auth/types/LoginPageDTO.ts
Normal file
11
apps/website/lib/services/auth/types/LoginPageDTO.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Login Page DTO
|
||||
*
|
||||
* Data transfer object for login page composition.
|
||||
* Used by AuthPageService and LoginViewDataBuilder.
|
||||
*/
|
||||
|
||||
export interface LoginPageDTO {
|
||||
returnTo: string;
|
||||
hasInsufficientPermissions: boolean;
|
||||
}
|
||||
11
apps/website/lib/services/auth/types/ResetPasswordPageDTO.ts
Normal file
11
apps/website/lib/services/auth/types/ResetPasswordPageDTO.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Reset Password Page DTO
|
||||
*
|
||||
* Data transfer object for reset password page composition.
|
||||
* Used by AuthPageService and ResetPasswordViewDataBuilder.
|
||||
*/
|
||||
|
||||
export interface ResetPasswordPageDTO {
|
||||
token: string;
|
||||
returnTo: string;
|
||||
}
|
||||
10
apps/website/lib/services/auth/types/SignupPageDTO.ts
Normal file
10
apps/website/lib/services/auth/types/SignupPageDTO.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Signup Page DTO
|
||||
*
|
||||
* Data transfer object for signup page composition.
|
||||
* Used by AuthPageService and SignupViewDataBuilder.
|
||||
*/
|
||||
|
||||
export interface SignupPageDTO {
|
||||
returnTo: string;
|
||||
}
|
||||
Reference in New Issue
Block a user