A social knowledge tool for researchers built on ATProto
1import { Controller } from '../../../../../shared/infrastructure/http/Controller';
2import { Request, Response } from 'express';
3import { LoginWithAppPasswordUseCase } from '../../../application/use-cases/LoginWithAppPasswordUseCase';
4import { CookieService } from '../../../../../shared/infrastructure/http/services/CookieService';
5
6export class LoginWithAppPasswordController extends Controller {
7 constructor(
8 private loginWithAppPasswordUseCase: LoginWithAppPasswordUseCase,
9 private cookieService: CookieService,
10 ) {
11 super();
12 }
13
14 async executeImpl(req: Request, res: Response): Promise<any> {
15 try {
16 const { identifier, appPassword } = req.body;
17
18 if (!identifier || !appPassword) {
19 return this.badRequest(res, 'Missing identifier or app password');
20 }
21
22 const result = await this.loginWithAppPasswordUseCase.execute({
23 identifier,
24 appPassword,
25 });
26
27 if (result.isErr()) {
28 return this.badRequest(res, result.error.message);
29 }
30
31 // Set tokens in httpOnly cookies
32 this.cookieService.setTokens(res, {
33 accessToken: result.value.accessToken,
34 refreshToken: result.value.refreshToken,
35 });
36
37 return this.ok(res, {
38 success: true,
39 message: 'Logged in successfully',
40 });
41 } catch (error: any) {
42 return this.fail(res, error.message || 'Unknown error');
43 }
44 }
45}