import { Response, Request } from "express"; import { Controller, Post } from "@ControllerPattern/index"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Service } from "typedi"; import AuthService from "@Services/common/AuthService/AuthService"; import { JwtPayload } from "jsonwebtoken"; @Controller() @Service() export default class CustomerController extends ApiController { constructor(private authService: AuthService) { super(); } @Post("/api/v1/france-connect/customer/login/:email") protected async login(req: Request, response: Response) { try { const email = req.params["email"]; if (!email) throw new Error("email is required"); const payload = await this.authService.getCustomerJwtPayload(email); const accessToken = this.authService.generateAccessToken(payload); const refreshToken = this.authService.generateRefreshToken(payload); //success this.httpSuccess(response, { accessToken, refreshToken }); } catch (error) { this.httpInternalError(response); return; } } @Post("/api/v1/france-connect/customer/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, customerPayload) => { if (err) { this.httpUnauthorized(response); return; } const customer = customerPayload as JwtPayload; delete customer.iat; delete customer!.exp; accessToken = this.authService.generateAccessToken(customer); }); //success this.httpSuccess(response, accessToken); } catch (error) { this.httpInternalError(response); return; } } }