module creation
This commit is contained in:
50
apps/website/lib/auth/AuthApiClient.ts
Normal file
50
apps/website/lib/auth/AuthApiClient.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { api } from '../apiClient';
|
||||
import type {
|
||||
AuthenticatedUserDTO,
|
||||
AuthSessionDTO,
|
||||
SignupParams,
|
||||
LoginParams,
|
||||
IracingAuthRedirectResult,
|
||||
LoginWithIracingCallbackParams,
|
||||
} from '../../../apps/api/src/modules/auth/dto/AuthDto'; // Using generated API DTOs
|
||||
|
||||
export class AuthApiClient {
|
||||
async getCurrentSession(): Promise<AuthSessionDTO | null> {
|
||||
try {
|
||||
return await api.get<AuthSessionDTO>('/auth/session');
|
||||
} catch (error) {
|
||||
// Handle error, e.g., if session is not found or API is down
|
||||
console.error('Error fetching current session:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async signupWithEmail(params: SignupParams): Promise<AuthSessionDTO> {
|
||||
return api.post<AuthSessionDTO>('/auth/signup', params);
|
||||
}
|
||||
|
||||
async loginWithEmail(params: LoginParams): Promise<AuthSessionDTO> {
|
||||
return api.post<AuthSessionDTO>('/auth/login', params);
|
||||
}
|
||||
|
||||
async startIracingAuthRedirect(returnTo?: string): Promise<IracingAuthRedirectResult> {
|
||||
const query = returnTo ? `?returnTo=${encodeURIComponent(returnTo)}` : '';
|
||||
return api.get<IracingAuthRedirectResult>(`/auth/iracing/start${query}`);
|
||||
}
|
||||
|
||||
async loginWithIracingCallback(params: LoginWithIracingCallbackParams): Promise<AuthSessionDTO> {
|
||||
const query = new URLSearchParams();
|
||||
query.append('code', params.code);
|
||||
query.append('state', params.state);
|
||||
if (params.returnTo) {
|
||||
query.append('returnTo', params.returnTo);
|
||||
}
|
||||
return await api.get<AuthSessionDTO>(`/auth/iracing/callback?${query.toString()}`);
|
||||
}
|
||||
|
||||
async logout(): Promise<void> {
|
||||
return api.post<void>('/auth/logout', {});
|
||||
}
|
||||
}
|
||||
|
||||
export const authApiClient = new AuthApiClient();
|
||||
@@ -1,36 +0,0 @@
|
||||
import type { AuthenticatedUserDTO } from '@gridpilot/identity/application/dto/AuthenticatedUserDTO';
|
||||
import type { AuthSessionDTO } from '@gridpilot/identity/application/dto/AuthSessionDTO';
|
||||
|
||||
export type AuthUser = AuthenticatedUserDTO;
|
||||
export type AuthSession = AuthSessionDTO;
|
||||
|
||||
export interface SignupParams {
|
||||
email: string;
|
||||
password: string;
|
||||
displayName: string;
|
||||
}
|
||||
|
||||
export interface LoginParams {
|
||||
email: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface AuthService {
|
||||
getCurrentSession(): Promise<AuthSession | null>;
|
||||
|
||||
// Email/password authentication
|
||||
signupWithEmail(params: SignupParams): Promise<AuthSession>;
|
||||
loginWithEmail(params: LoginParams): Promise<AuthSession>;
|
||||
|
||||
// iRacing OAuth (demo)
|
||||
startIracingAuthRedirect(
|
||||
returnTo?: string,
|
||||
): Promise<{ redirectUrl: string; state: string }>;
|
||||
loginWithIracingCallback(params: {
|
||||
code: string;
|
||||
state: string;
|
||||
returnTo?: string;
|
||||
}): Promise<AuthSession>;
|
||||
|
||||
logout(): Promise<void>;
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
import type { AuthService } from './AuthService';
|
||||
import { InMemoryAuthService } from './InMemoryAuthService';
|
||||
import { getDIContainer } from '../di-container';
|
||||
import { DI_TOKENS } from '../di-tokens';
|
||||
|
||||
export function getAuthService(): AuthService {
|
||||
const container = getDIContainer();
|
||||
if (!container.isRegistered(DI_TOKENS.AuthService)) {
|
||||
throw new Error(
|
||||
`${DI_TOKENS.AuthService.description} not registered in DI container.`,
|
||||
);
|
||||
}
|
||||
return container.resolve<AuthService>(DI_TOKENS.AuthService);
|
||||
}
|
||||
Reference in New Issue
Block a user