A social knowledge tool for researchers built on ATProto
1import { Controller } from '../../../../../shared/infrastructure/http/Controller';
2import { Request, Response } from 'express';
3import { CompleteOAuthSignInUseCase } from '../../../application/use-cases/CompleteOAuthSignInUseCase';
4import { configService } from 'src/shared/infrastructure/config';
5
6export class CompleteOAuthSignInController extends Controller {
7 constructor(private completeOAuthSignInUseCase: CompleteOAuthSignInUseCase) {
8 super();
9 }
10
11 async executeImpl(req: Request, res: Response): Promise<any> {
12 const appUrl = configService.getAppConfig().appUrl;
13 try {
14 const { code, state, iss } = req.query;
15
16 if (!code || !state || !iss) {
17 return this.badRequest(res, 'Missing required parameters');
18 }
19
20 const result = await this.completeOAuthSignInUseCase.execute({
21 code: code as string,
22 state: state as string,
23 iss: iss as string,
24 });
25
26 if (result.isErr()) {
27 // Instead of returning JSON, redirect with error
28 return res.redirect(
29 `${process.env.FRONTEND_URL}/login?error=${encodeURIComponent(result.error.message)}`,
30 );
31 }
32
33 // Redirect back to frontend with tokens in URL parameters
34 return res.redirect(
35 `${appUrl}/auth/complete?accessToken=${encodeURIComponent(result.value.accessToken)}&refreshToken=${encodeURIComponent(result.value.refreshToken)}`,
36 );
37 } catch (error: any) {
38 return res.redirect(
39 `${appUrl}/login?error=${encodeURIComponent(error.message || 'Unknown error')}`,
40 );
41 }
42 }
43}