lecoffre-back/src/app/api/franceConnect/CustomerController.ts
2023-09-26 12:01:04 +02:00

68 lines
2.0 KiB
TypeScript

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, { ICustomerJwtPayload } 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);
// if (!payload) {
// this.httpNotFoundRequest(response);
// return;
// }
// 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} as ICustomerJwtPayload);
});
//success
this.httpSuccess(response, {accessToken});
} catch (error) {
this.httpInternalError(response);
return;
}
}
}