Skip to content

Commit

Permalink
Upd: Allow login with IDP and normal login simultaneously
Browse files Browse the repository at this point in the history
Signed-off-by: George J Padayatti <[email protected]>
  • Loading branch information
georgepadayatti committed Nov 16, 2023
1 parent 380972d commit 4219cf8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
4 changes: 2 additions & 2 deletions public/config/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
40 changes: 29 additions & 11 deletions src/Components/Login/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -228,6 +240,12 @@ class Login extends Component {
</div>
</Form.Item>
<Divider className="login-divider" />
{store.idpConfig !== undefined || store.idpConfig !== null ? (
<div className="login-actions">
<a onClick={this.handleOidcLogin}>Login with {data.name}</a>
</div>
) : null}

<div className="login-actions">
<Link to={`/forgot-password`}>{t("forgot")}</Link>
</div>
Expand Down
4 changes: 4 additions & 0 deletions src/Provider/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Services from "../services";
import moment from "moment";

class Store {

@observable user = {
name: "",
email: ""
Expand Down Expand Up @@ -47,6 +48,9 @@ class Store {
isFetching: true
};




@action fetchLogs = () => {
// Fetch logs
const request = services.fetchLogs();
Expand Down
13 changes: 8 additions & 5 deletions src/authorization/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "axios";
import { setSession } from "./utils";
import { getSession, setSession } from "./utils";


import { history } from "../history";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -297,6 +299,7 @@ class Auth {
localStorage.removeItem("refreshToken");
localStorage.removeItem("expiresIn");
localStorage.removeItem("userId");
localStorage.removeItem("isIdpLogin");
};

isLoginValid = () => {
Expand Down
1 change: 1 addition & 0 deletions src/authorization/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

0 comments on commit 4219cf8

Please sign in to comment.