From d1829d24eb1ff6010ad20cbb1dda19d0d46570d6 Mon Sep 17 00:00:00 2001 From: Misaka Date: Sat, 28 Feb 2026 22:38:55 +0800 Subject: [PATCH] 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 --- src/main/models/auth.types.ts | 10 ++++++ src/main/services/auth.service.ts | 54 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/main/models/auth.types.ts create mode 100644 src/main/services/auth.service.ts diff --git a/src/main/models/auth.types.ts b/src/main/models/auth.types.ts new file mode 100644 index 0000000..a75e5d5 --- /dev/null +++ b/src/main/models/auth.types.ts @@ -0,0 +1,10 @@ +export interface AuthResult { + success: boolean; + message?: string; +} + +export interface SessionInfo { + username: string; + isLoggedIn: boolean; + loginTime: Date; +} diff --git a/src/main/services/auth.service.ts b/src/main/services/auth.service.ts new file mode 100644 index 0000000..3d2164e --- /dev/null +++ b/src/main/services/auth.service.ts @@ -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 { + 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 { + 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; + } +}