feat: implement authentication service
Add auth service that wraps Playwright service for user authentication. Includes login/logout functionality and session management. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
10
src/main/models/auth.types.ts
Normal file
10
src/main/models/auth.types.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
export interface AuthResult {
|
||||||
|
success: boolean;
|
||||||
|
message?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SessionInfo {
|
||||||
|
username: string;
|
||||||
|
isLoggedIn: boolean;
|
||||||
|
loginTime: Date;
|
||||||
|
}
|
||||||
54
src/main/services/auth.service.ts
Normal file
54
src/main/services/auth.service.ts
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import { PlaywrightService } from './playwright.service';
|
||||||
|
import { Credentials, LoginResult } from '../models/playwright.types';
|
||||||
|
import { AuthResult, SessionInfo } from '../models/auth.types';
|
||||||
|
import { LoggerService } from './logger.service';
|
||||||
|
|
||||||
|
export class AuthService {
|
||||||
|
private currentSession?: LoginResult;
|
||||||
|
private sessionInfo?: SessionInfo;
|
||||||
|
|
||||||
|
constructor(private playwrightService: PlaywrightService) {}
|
||||||
|
|
||||||
|
async login(credentials: Credentials): Promise<AuthResult> {
|
||||||
|
try {
|
||||||
|
LoggerService.info(`Logging in user: ${credentials.username}`);
|
||||||
|
|
||||||
|
const session = await this.playwrightService.login(credentials);
|
||||||
|
this.currentSession = session;
|
||||||
|
this.sessionInfo = {
|
||||||
|
username: credentials.username,
|
||||||
|
isLoggedIn: true,
|
||||||
|
loginTime: new Date(),
|
||||||
|
};
|
||||||
|
|
||||||
|
LoggerService.info('Login successful');
|
||||||
|
return { success: true, message: 'Login successful' };
|
||||||
|
} catch (error) {
|
||||||
|
LoggerService.error('Login failed', error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: error instanceof Error ? error.message : 'Unknown error',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async logout(): Promise<void> {
|
||||||
|
if (this.currentSession) {
|
||||||
|
LoggerService.info('Logging out...');
|
||||||
|
|
||||||
|
await this.playwrightService.closeBrowser();
|
||||||
|
this.currentSession = undefined;
|
||||||
|
this.sessionInfo = undefined;
|
||||||
|
|
||||||
|
LoggerService.info('Logout successful');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getSessionInfo(): SessionInfo | undefined {
|
||||||
|
return this.sessionInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
isLoggedIn(): boolean {
|
||||||
|
return this.sessionInfo?.isLoggedIn ?? false;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user