Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates Login.jsx with showSignInToGetTokens #184

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions custom-login/config-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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`);
Expand Down
34 changes: 16 additions & 18 deletions custom-login/src/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
/**
Expand All @@ -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 (
Expand Down