From 4219cf89b8b6d895e586a8d65fb3f536fca07d6f Mon Sep 17 00:00:00 2001 From: George J Padayatti Date: Fri, 17 Nov 2023 02:30:09 +0530 Subject: [PATCH] Upd: Allow login with IDP and normal login simultaneously Signed-off-by: George J Padayatti --- public/config/config.json | 4 ++-- src/Components/Login/Login.jsx | 40 ++++++++++++++++++++++++---------- src/Provider/store.js | 4 ++++ src/authorization/auth.js | 13 ++++++----- src/authorization/utils.js | 1 + 5 files changed, 44 insertions(+), 18 deletions(-) diff --git a/public/config/config.json b/public/config/config.json index 9a5ec661..7943c774 100644 --- a/public/config/config.json +++ b/public/config/config.json @@ -1,5 +1,5 @@ { - "baseUrl": "https://api.igrant.dev/v2", - "redirectUrl": "https://staging-consent-bb-privacy-dashboard.igrant.io/login", + "baseUrl": "https://staging-consent-bb-api.igrant.io/v2", + "redirectUrl": "http://localhost:3000/login", "consentBbClientId": "igrant-ios-app" } \ No newline at end of file diff --git a/src/Components/Login/Login.jsx b/src/Components/Login/Login.jsx index 78fd42a0..dcb1386b 100644 --- a/src/Components/Login/Login.jsx +++ b/src/Components/Login/Login.jsx @@ -12,7 +12,6 @@ import { LanguageSelector } from "./LanguageSelector"; import { Logo } from "./Logo"; import "./login.css"; import loginIcon from "assets/icons/arrow.svg"; -import defaultLogo from "assets/icons/igrant.io_200X200.jpg"; import qs from "qs"; @observer @@ -22,6 +21,7 @@ class Login extends Component { // Don't show login for OpenID connect subscription method this.state = { showLogin: false, openIdLoaderText: "" }; + this.handleOidcLogin = this.handleOidcLogin.bind(this); } componentWillMount() { @@ -85,17 +85,10 @@ class Login extends Component { }); } } else { - const authUrl = readIdpRes.data.idp.authorisationUrl; - const clientId = readIdpRes.data.idp.clientId; - const redirectUri = store.config.redirectUrl; - - // Updating the loader text to be shown while waiting for the redirection towards org login page. + // Show login screen this.setState({ - openIdLoaderText: "Redirecting to organization login page...", + showLogin: true, }); - - // Redirecting user to configured external identity provider login - window.location.href = `${authUrl}?client_id=${clientId}&response_type=code&redirect_uri=${redirectUri}`; } } else { // Show login screen @@ -124,6 +117,25 @@ class Login extends Component { store.fetchOrganisation(); } + handleOidcLogin() { + const authUrl = store.idpConfig.authorisationUrl; + const clientId = store.idpConfig.clientId; + const redirectUri = store.config.redirectUrl; + + // Hide login + this.setState({ + showLogin: false, + }); + + // Updating the loader text to be shown while waiting for the redirection towards org login page. + this.setState({ + openIdLoaderText: "Redirecting to organization login page...", + }); + + // Redirecting user to configured external identity provider login + window.location.href = `${authUrl}?client_id=${clientId}&response_type=code&redirect_uri=${redirectUri}`; + } + handleKeydown(e) { if (e.keyCode === 13 && e.shiftKey === false) { this.handleLogin(); @@ -155,7 +167,7 @@ class Login extends Component { const { getFieldDecorator } = this.props.form; const loading = store.authStore.isLoading; - const { logoImageURI } = store.organizationStore; + const { logoImageURI, data } = store.organizationStore; if (this.state.showLogin) { return ( @@ -228,6 +240,12 @@ class Login extends Component { + {store.idpConfig !== undefined || store.idpConfig !== null ? ( +
+ Login with {data.name} +
+ ) : null} +
{t("forgot")}
diff --git a/src/Provider/store.js b/src/Provider/store.js index e88214ee..8208042d 100644 --- a/src/Provider/store.js +++ b/src/Provider/store.js @@ -5,6 +5,7 @@ import Services from "../services"; import moment from "moment"; class Store { + @observable user = { name: "", email: "" @@ -47,6 +48,9 @@ class Store { isFetching: true }; + + + @action fetchLogs = () => { // Fetch logs const request = services.fetchLogs(); diff --git a/src/authorization/auth.js b/src/authorization/auth.js index a45ed1ba..230f1197 100644 --- a/src/authorization/auth.js +++ b/src/authorization/auth.js @@ -1,5 +1,5 @@ import axios from "axios"; -import { setSession } from "./utils"; +import { getSession, setSession } from "./utils"; import { history } from "../history"; @@ -66,7 +66,7 @@ class Auth { // Check if access token is expired if (accessTokenExpiresIn - currentDateInUTC < 10) { - if (this.store.idpConfig !== undefined || this.store.idpConfig !== null) { + if (getSession("isIdpLogin") === "true") { // Perform local logout by clearing local storage this.clearSession(); this.isAuthenticated = false; @@ -170,7 +170,7 @@ class Auth { logout = () => { try { // Perform online logout for IDP if available - if (this.store.idpConfig !== undefined || this.store.idpConfig !== null) { + if (getSession("isIdpLogin") === "true") { const data = { client_id: this.store.idpConfig.clientId, refresh_token: this.refreshToken @@ -214,7 +214,8 @@ class Auth { ...loginRes.token, userId: loginRes.userInfo.subject, username: loginRes.userInfo.email, - email: loginRes.userInfo.email + email: loginRes.userInfo.email, + isIdpLogin: "true" }); } // Access token @@ -261,7 +262,8 @@ class Auth { ...loginRes.token, userId: loginRes.individual.id, username: loginRes.individual.name, - email: loginRes.individual.email + email: loginRes.individual.email, + isIdpLogin: "false" }); } // Access token @@ -297,6 +299,7 @@ class Auth { localStorage.removeItem("refreshToken"); localStorage.removeItem("expiresIn"); localStorage.removeItem("userId"); + localStorage.removeItem("isIdpLogin"); }; isLoginValid = () => { diff --git a/src/authorization/utils.js b/src/authorization/utils.js index c975b0c6..ac2568a5 100644 --- a/src/authorization/utils.js +++ b/src/authorization/utils.js @@ -34,4 +34,5 @@ export const setSession = authResult => { localStorage.setItem("userId", authResult.userId); localStorage.setItem("username", authResult.username); localStorage.setItem("email", authResult.email); + localStorage.setItem("isIdpLogin", authResult.isIdpLogin); };