Skip to content
Closed
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
17 changes: 10 additions & 7 deletions custom-login/src/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* See the License for the specific language governing permissions and limitations under the License.
*/
import React, { useEffect } from 'react';
import React, { useEffect, 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';
Expand All @@ -18,8 +18,13 @@ import config from './config';

const Login = () => {
const { oktaAuth } = useOktaAuth();
const widgetRef = useRef();

useEffect(() => {
if (!widgetRef.current) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want to use useLayoutEffect here and then you don't need this check.

https://reactjs.org/docs/hooks-reference.html#uselayouteffect

return false;
}

const { issuer, clientId, redirectUri, scopes } = config.oidc;
const widget = new OktaSignIn({
/**
Expand All @@ -44,23 +49,21 @@ const Login = () => {
});

widget.showSignInToGetTokens({
el: '#sign-in-widget',
el: widgetRef.current,
scopes,
}).then((tokens) => {

// Remove the widget
widget.remove();

// Add tokens to storage
oktaAuth.handleLoginRedirect(tokens);
}).catch((err) => {
throw err;
});

return () => widget.remove();
}, [oktaAuth]);

return (
<div>
<div id="sign-in-widget" />
<div ref={widgetRef} />
</div>
);
};
Expand Down