import { Response, Request } from "express"; import { Controller, Post } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; import Id360Service, { EnrollmentResponse } from "@Services/common/Id360Service/Id360Service"; import CustomersService from "@Services/customer/CustomersService/CustomersService"; import AuthService, { ICustomerJwtPayload } from "@Services/common/AuthService/AuthService"; import { Customer } from "le-coffre-resources/dist/SuperAdmin"; @Controller() @Service() export default class CustomerController extends ApiController { constructor(private id360Service: Id360Service, private customerService: CustomersService, private authService: AuthService) { super(); } @Post("/api/v1/id360/customers/login") protected async login(req: Request, response: Response) { try { const enrollment = await this.id360Service.createFranceConnectEnrollment(); this.httpSuccess(response, { enrollment }); } catch (error) { console.log(error); this.httpInternalError(response); return; } } @Post("/api/v1/id360/customers/login-callback/:callbackToken") protected async loginCallback(req: Request, response: Response) { const callbackToken = req.params["callbackToken"]; if (!callbackToken) { this.httpBadRequest(response, "callback Token is required"); return; } try { await new Promise((resolve) => setTimeout(resolve, 3000)); // wait 3 seconds to be sure that the enrollment is finilazed const res = await this.id360Service.getEnrollment(callbackToken); const enrollment = (await res.json()) as EnrollmentResponse; if (enrollment.status !== "OK") { this.httpUnauthorized(response, "Enrollment status is not OK"); return; } const customerData = await this.id360Service.getReport(enrollment.id); const customer = await this.customerService.get({ where: { contact: { last_name: { contains: customerData.external_methods.france_connect.results.france_connect_out_userinfo[0].family_name, mode: "insensitive", }, first_name: { contains: customerData.external_methods.france_connect.results.france_connect_out_userinfo[0].given_name.split( " ", )[0], mode: "insensitive", }, }, }, include: { contact: true, }, }); // const contact = await this.customerService.getByEmail( // customerData.external_methods.france_connect.results.france_connect_out_userinfo[0].email, // ); if (customer.length === 0) { this.httpNotFoundRequest(response, "Customer not found"); return; } const customersHydrated = Customer.hydrateArray(customer); const payload = await this.authService.getCustomerJwtPayload(customersHydrated); const accessToken = this.authService.generateAccessToken(payload); const refreshToken = this.authService.generateRefreshToken(payload); this.httpSuccess(response, { accessToken, refreshToken }); } catch (error) { console.log(error); this.httpInternalError(response); return; } } @Post("/api/v1/id360/token") protected async getToken(req: Request, response: Response) { try { const token = await this.id360Service.getId360Token(); this.httpSuccess(response, { token }); } catch (error) { console.log(error); this.httpInternalError(response); return; } } @Post("/api/v1/id360/customers/refresh-token") protected async refreshToken(req: Request, response: Response) { try { const authHeader = req.headers["authorization"]; const token = authHeader && authHeader.split(" ")[1]; if (!token) { this.httpBadRequest(response); return; } let accessToken; this.authService.verifyRefreshToken(token, (err, userPayload) => { if (err) { console.log(err); this.httpUnauthorized(response); return; } const user = userPayload as ICustomerJwtPayload; delete user.iat; delete user.exp; accessToken = this.authService.generateAccessToken(user); }); //success this.httpSuccess(response, { accessToken }); } catch (error) { console.log(error); this.httpInternalError(response); return; } } }