diff --git a/custom-login/config-overrides.js b/custom-login/config-overrides.js index 32d1255f..acad52b1 100644 --- a/custom-login/config-overrides.js +++ b/custom-login/config-overrides.js @@ -16,6 +16,7 @@ if (fs.existsSync(TESTENV)) { } process.env.CLIENT_ID = process.env.CLIENT_ID || process.env.SPA_CLIENT_ID; process.env.OKTA_TESTING_DISABLEHTTPSCHECK = process.env.OKTA_TESTING_DISABLEHTTPSCHECK || false; +process.env.USE_INTERACTION_CODE_FLOW = process.env.USE_INTERACTION_CODE_FLOW || false; const webpack = require('webpack'); @@ -26,6 +27,7 @@ const env = {}; 'ISSUER', 'CLIENT_ID', 'OKTA_TESTING_DISABLEHTTPSCHECK', + 'USE_INTERACTION_CODE_FLOW', ].forEach((key) => { if (!process.env[key]) { throw new Error(`Environment variable ${key} must be set. See README.md`); diff --git a/custom-login/src/Login.jsx b/custom-login/src/Login.jsx index f792c91c..9a945031 100644 --- a/custom-login/src/Login.jsx +++ b/custom-login/src/Login.jsx @@ -9,22 +9,20 @@ * * See the License for the specific language governing permissions and limitations under the License. */ -import React, { useEffect, useRef } from 'react'; +import React, { useLayoutEffect, useRef } from 'react'; import { useOktaAuth } from '@okta/okta-react'; import * as OktaSignIn from '@okta/okta-signin-widget'; import '@okta/okta-signin-widget/dist/css/okta-sign-in.min.css'; import config from './config'; +const USE_INTERACTION_CODE_FLOW = process.env.USE_INTERACTION_CODE_FLOW || false; + const Login = () => { const { oktaAuth } = useOktaAuth(); const widgetRef = useRef(); - useEffect(() => { - if (!widgetRef.current) { - return false; - } - + useLayoutEffect(() => { const { issuer, clientId, redirectUri, scopes } = config.oidc; const widget = new OktaSignIn({ /** @@ -46,21 +44,21 @@ const Login = () => { issuer, scopes, }, - useInteractionCodeFlow: false, // Set to true, if your org is OIE enabled + useInteractionCodeFlow: USE_INTERACTION_CODE_FLOW, // Set to true if your org is OIE enabled }); - widget.renderEl( - { el: widgetRef.current }, - (res) => { - console.log(res); - oktaAuth.handleLoginRedirect(res.tokens); - }, - (err) => { - throw err; - }, - ); + widget.showSignInToGetTokens({ + el: widgetRef.current, + scopes, + }).then((tokens) => { + // Remove the widget + widget.remove(); - return () => widget.remove(); + // Add tokens to storage + oktaAuth.handleLoginRedirect(tokens); + }).catch((err) => { + throw err; + }); }, [oktaAuth]); return (