(
@Headers('authorization') authHeader: string | undefined,
@Res({ passthrough: true }) res: Response,
)
| 94 | @Public() |
| 95 | @Post('/auth/login') |
| 96 | login( |
| 97 | @Headers('authorization') authHeader: string | undefined, |
| 98 | @Res({ passthrough: true }) res: Response, |
| 99 | ) { |
| 100 | // Only for local auth provider |
| 101 | if (this.authCfg.provider !== 'local') { |
| 102 | throw new UnauthorizedException('Login endpoint only available for local auth'); |
| 103 | } |
| 104 | |
| 105 | // Validate Basic auth header |
| 106 | if (!authHeader || !authHeader.startsWith('Basic ')) { |
| 107 | throw new UnauthorizedException('Missing Basic Auth credentials'); |
| 108 | } |
| 109 | |
| 110 | const base64Credentials = authHeader.slice(6); |
| 111 | let username: string; |
| 112 | let password: string; |
| 113 | |
| 114 | try { |
| 115 | const credentials = Buffer.from(base64Credentials, 'base64').toString('utf-8'); |
| 116 | [username, password] = credentials.split(':'); |
| 117 | } catch { |
| 118 | throw new UnauthorizedException('Invalid Basic Auth format'); |
| 119 | } |
| 120 | |
| 121 | if (!username || !password) { |
| 122 | throw new UnauthorizedException('Invalid Basic Auth format'); |
| 123 | } |
| 124 | |
| 125 | // Validate credentials |
| 126 | if ( |
| 127 | username !== this.authCfg.local.adminUsername || |
| 128 | password !== this.authCfg.local.adminPassword |
| 129 | ) { |
| 130 | throw new UnauthorizedException('Invalid admin credentials'); |
| 131 | } |
| 132 | |
| 133 | // Create session token and set cookie |
| 134 | const sessionToken = createSessionToken(username); |
| 135 | |
| 136 | res.cookie(SESSION_COOKIE_NAME, sessionToken, { |
| 137 | httpOnly: true, |
| 138 | secure: process.env.NODE_ENV === 'production', |
| 139 | sameSite: 'lax', |
| 140 | maxAge: SESSION_COOKIE_MAX_AGE, |
| 141 | path: '/', |
| 142 | }); |
| 143 | |
| 144 | return { success: true, message: 'Logged in successfully' }; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Logout endpoint for local auth. |
nothing calls this directly
no test coverage detected