From e84701867ac67186b1b3a1121465ea9245837ae7 Mon Sep 17 00:00:00 2001 From: Hugo Lextrait Date: Wed, 12 Apr 2023 18:40:39 +0200 Subject: [PATCH] wip connexion with idnote --- next.config.js | 2 + src/front/Api/Auth/IdNot/index.ts | 39 ++++++++++++++++ src/front/Api/BaseApiService.ts | 5 ++- src/front/Assets/Icons/loader.svg | 27 +++++++++++ .../DesignSystem/Loader/classes.module.scss | 2 +- .../Components/DesignSystem/Loader/index.tsx | 2 +- src/front/Components/Layouts/Login/index.tsx | 31 +++++-------- .../Layouts/LoginCallback/classes.module.scss | 16 +++++++ .../Layouts/LoginCallback/index.tsx | 45 +++++++++++++++++++ src/front/Config/VariablesFront.ts | 4 ++ src/pages/_app.tsx | 8 +++- src/pages/authorized-client.tsx | 5 +++ 12 files changed, 160 insertions(+), 26 deletions(-) create mode 100644 src/front/Api/Auth/IdNot/index.ts create mode 100644 src/front/Assets/Icons/loader.svg create mode 100644 src/front/Components/Layouts/LoginCallback/classes.module.scss create mode 100644 src/front/Components/Layouts/LoginCallback/index.tsx create mode 100644 src/pages/authorized-client.tsx diff --git a/next.config.js b/next.config.js index 5b4172f7..b11b161e 100644 --- a/next.config.js +++ b/next.config.js @@ -9,6 +9,8 @@ const nextConfig = { BACK_API_PORT: process.env.BACK_API_PORT, BACK_API_ROOT_URL: process.env.BACK_API_ROOT_URL, BACK_API_VERSION: process.env.BACK_API_VERSION, + FRONT_APP_HOST: process.env.FRONT_APP_HOST, + FRONT_APP_PORT: process.env.FRONT_APP_PORT, }, }; diff --git a/src/front/Api/Auth/IdNot/index.ts b/src/front/Api/Auth/IdNot/index.ts new file mode 100644 index 00000000..1255ce5a --- /dev/null +++ b/src/front/Api/Auth/IdNot/index.ts @@ -0,0 +1,39 @@ +import { Service } from "typedi"; +import BaseApiService from "@Front/Api/BaseApiService"; + +@Service() +export default class Auth extends BaseApiService { + private static instance: Auth; + + private constructor() { + super(); + } + + public static getInstance(): Auth { + return (this.instance = this.instance ?? new Auth()); + } + + public async userSignin() { + const url = new URL( + "https://qual-connexion.idnot.fr/IdPOAuth2/authorize/idnot_idp_v1?client_id=4501646203F3EF67&redirect_uri=http://0.0.0.0:3000/authorized-client&scope=openid,profile,offline_access&response_type=code" + ); + try { + return await this.getRequest(url); + } catch (err) { + this.onError(err); + return Promise.reject(err); + } + } + + public async getIdnotJwt(autorizationCode: string | string[]) { + const url = new URL( + `http://127.0.0.1:3001/api/v1/idnot-user/${autorizationCode}` + ); + try { + await this.postRequest(url); + } catch (err) { + this.onError(err); + //return Promise.reject(err); + } + } +} diff --git a/src/front/Api/BaseApiService.ts b/src/front/Api/BaseApiService.ts index 8fdaebe7..9b7ff91c 100644 --- a/src/front/Api/BaseApiService.ts +++ b/src/front/Api/BaseApiService.ts @@ -12,8 +12,9 @@ export default abstract class BaseApiService { BaseApiService.baseUrl ??= FrontendVariables.getInstance().BACK_API_PROTOCOL + FrontendVariables.getInstance().BACK_API_HOST + - ":" + - FrontendVariables.getInstance().BACK_API_PORT ?? "" + + (FrontendVariables.getInstance().BACK_API_PORT + ? ":" + FrontendVariables.getInstance().BACK_API_PORT + : "") + FrontendVariables.getInstance().BACK_API_ROOT_URL + FrontendVariables.getInstance().BACK_API_VERSION; } diff --git a/src/front/Assets/Icons/loader.svg b/src/front/Assets/Icons/loader.svg new file mode 100644 index 00000000..c59ed103 --- /dev/null +++ b/src/front/Assets/Icons/loader.svg @@ -0,0 +1,27 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/front/Components/DesignSystem/Loader/classes.module.scss b/src/front/Components/DesignSystem/Loader/classes.module.scss index a7f47cba..37403d86 100644 --- a/src/front/Components/DesignSystem/Loader/classes.module.scss +++ b/src/front/Components/DesignSystem/Loader/classes.module.scss @@ -1,4 +1,4 @@ -@import "Themes/constants.scss"; +@import "@Themes/constants.scss"; .loader { animation: spin 2s linear infinite; diff --git a/src/front/Components/DesignSystem/Loader/index.tsx b/src/front/Components/DesignSystem/Loader/index.tsx index f6a1ab85..09b63940 100644 --- a/src/front/Components/DesignSystem/Loader/index.tsx +++ b/src/front/Components/DesignSystem/Loader/index.tsx @@ -1,5 +1,5 @@ import React from "react"; -import LoaderIcon from "assets/images/Icons/loader.svg"; +import LoaderIcon from "@Assets/Icons/loader.svg"; import classes from "./classes.module.scss"; import Image from "next/image"; diff --git a/src/front/Components/Layouts/Login/index.tsx b/src/front/Components/Layouts/Login/index.tsx index 12d7c082..d0e3b3d9 100644 --- a/src/front/Components/Layouts/Login/index.tsx +++ b/src/front/Components/Layouts/Login/index.tsx @@ -6,13 +6,9 @@ import CoffreIcon from "@Assets/Icons/coffre.svg"; import LandingImage from "./landing-connect.png" import Image from "next/image"; import DefaultDoubleSidePage from "@Front/Components/LayoutTemplates/DefaultDoubleSidePage"; +import { FrontendVariables } from "@Front/Config/VariablesFront"; -export default class Login extends BasePage { - //constructor - public constructor(props: any) { - super(props); - // this.getAuthorizationCode = this.getAuthorizationCode.bind(this); - } +export default class LoginClass extends BasePage { public override render(): JSX.Element { return ( @@ -20,11 +16,6 @@ export default class Login extends BasePage { coffre
Connexion espace professionnel
- CONNECT
Vous n’arrivez pas à vous connecter ?
@@ -33,15 +24,13 @@ export default class Login extends BasePage { } private redirectUserOnConnection() { - const url = `https://qual-connexion.idnot.fr/IdPOAuth2/authorize/idnot_idp_v1? - client_id=4501646203F3EF67 - &redirect_uri=https://app.stg.lecoffre.smart-chain.fr/ - &scope=openid,profile,offline_access - &response_type=code`; + // check variable + const baseFronturl = FrontendVariables.getInstance().BACK_API_PROTOCOL + + FrontendVariables.getInstance().FRONT_APP_HOST + + (FrontendVariables.getInstance().FRONT_APP_PORT ? ":" + FrontendVariables.getInstance().FRONT_APP_PORT : ""); + + const url = `https://qual-connexion.idnot.fr/IdPOAuth2/authorize/idnot_idp_v1?client_id=4501646203F3EF67&redirect_uri=${baseFronturl}/authorized-client&scope=openid,profile,offline_access&response_type=code` + window.location.assign(url) } - - // private getAuthorizationCode() { - // Users.getInstance().getAuthorizationCode() - // } -} +} \ No newline at end of file diff --git a/src/front/Components/Layouts/LoginCallback/classes.module.scss b/src/front/Components/Layouts/LoginCallback/classes.module.scss new file mode 100644 index 00000000..53e48bc0 --- /dev/null +++ b/src/front/Components/Layouts/LoginCallback/classes.module.scss @@ -0,0 +1,16 @@ +.root { + display: flex; + align-items: center; + justify-content: center; + flex-direction: column; + + .title { + margin: 32px 0; + text-align: center; + } + + .forget-password { + margin-top: 32px; + margin-bottom: 8px; + } +} \ No newline at end of file diff --git a/src/front/Components/Layouts/LoginCallback/index.tsx b/src/front/Components/Layouts/LoginCallback/index.tsx new file mode 100644 index 00000000..1f1714da --- /dev/null +++ b/src/front/Components/Layouts/LoginCallback/index.tsx @@ -0,0 +1,45 @@ +import classes from "./classes.module.scss"; +import LandingImage from "../Login/landing-connect.png"; +import DefaultDoubleSidePage from "@Front/Components/LayoutTemplates/DefaultDoubleSidePage"; +import Router, { useRouter } from "next/router"; + +import React from "react"; +import Loader from "@Front/Components/DesignSystem/Loader"; +import Auth from "@Front/Api/Auth/IdNot"; + +type IPropsClass = {}; + +type IStateClass = {}; + +class LoginCallBackClass extends React.Component { + public override render(): JSX.Element { + return ( + +
+ +
+
+ ); + } +} + +// TODO: Refacto with functionnal component container of classcomponent +export default function LoginCallBack(props: IPropsClass) { + const router = useRouter(); + const { code } = router.query; + if (code) { + const getIdNotJwt = async () => { + try { + const authService = Auth.getInstance(); + const user = await authService.getIdnotJwt(code); + console.log(user); + Router.push('/dossier'); + } catch (error) { + console.error(error); + Router.push('/login'); + } + }; + getIdNotJwt(); + } + return ; +} \ No newline at end of file diff --git a/src/front/Config/VariablesFront.ts b/src/front/Config/VariablesFront.ts index 12cc48e4..33ddcfda 100644 --- a/src/front/Config/VariablesFront.ts +++ b/src/front/Config/VariablesFront.ts @@ -11,6 +11,10 @@ export class FrontendVariables { public BACK_API_VERSION!: string; + public FRONT_APP_HOST!: string; + + public FRONT_APP_PORT!: string; + private constructor() {} public static getInstance(): FrontendVariables { diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index 4bf451a8..41dd8b44 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -17,9 +17,11 @@ type AppPropsWithLayout = AppProps & { backApiPort: string, backApiRootUrl: string, backApiVersion: string, + frontAppHost: string, + frontAppPort: string, }; -const MyApp = (({ Component, pageProps, backApiProtocol, backApiHost, backApiPort, backApiRootUrl, backApiVersion }: AppPropsWithLayout) => { +const MyApp = (({ Component, pageProps, backApiProtocol, backApiHost, backApiPort, backApiRootUrl, backApiVersion, frontAppHost, frontAppPort }: AppPropsWithLayout) => { const getLayout = Component.getLayout ?? ((page) => ); FrontendVariables.getInstance().BACK_API_PROTOCOL = backApiProtocol; @@ -27,6 +29,8 @@ const MyApp = (({ Component, pageProps, backApiProtocol, backApiHost, backApiPor FrontendVariables.getInstance().BACK_API_PORT = backApiPort; FrontendVariables.getInstance().BACK_API_ROOT_URL = backApiRootUrl; FrontendVariables.getInstance().BACK_API_VERSION = backApiVersion; + FrontendVariables.getInstance().FRONT_APP_HOST = frontAppHost; + FrontendVariables.getInstance().FRONT_APP_PORT = frontAppPort; return getLayout(); }) as AppType; @@ -38,6 +42,8 @@ MyApp.getInitialProps = async () => { backApiPort: process.env["BACK_API_PORT"], backApiRootUrl: process.env["BACK_API_ROOT_URL"], backApiVersion: process.env["BACK_API_VERSION"], + frontAppHost: process.env["FRONT_APP_HOST"], + frontAppPort: process.env["FRONT_APP_PORT"], }; } diff --git a/src/pages/authorized-client.tsx b/src/pages/authorized-client.tsx new file mode 100644 index 00000000..6aa4ed39 --- /dev/null +++ b/src/pages/authorized-client.tsx @@ -0,0 +1,5 @@ +import LoginCallBack from "@Front/Components/Layouts/LoginCallback"; + +export default function Route() { + return ; +} \ No newline at end of file