Okta登录重定向不会更新authState。

huangapple go评论50阅读模式
英文:

Okta login redirection does not update authState

问题

以下是您要翻译的内容:

"Trying to integrate Okta with ReactJs web. Web app successfully route user to Okta login page, user managed to login successful too. Upon login successful, Okta redirected back to ReactJs web without authState update (authState is NULL while trying to read).

With my implemented code, im aways getting redirected back to Home.js with the Loading... text. What seems to be missing here?

Sample code impelmentation as below
App.js

const oktaAuth = new OktaAuth({
    issuer: 'https://{{ourDomain}}.com/oauth2/default',
    clientId: '{{ourValue}}',
    redirectUri: window.location.origin + '/login/callback',
    scopes: ["openid", "profile", "email"],
    pkce: true,
});

<Security oktaAuth={oktaAuth} restoreOriginalUri={restoreOriginalUri}>
        <Switch>
          <Route path="/login/callback" component={LoginCallback} />
          <Route path="/" component={Home} />
          <SecureRoute path="/profile" component={Profile} />
        </Switch>
      </Security>

Home.js

const { oktaAuth, authState } = useOktaAuth();
const login = async () => await oktaAuth.signInWithRedirect();
const logout = async () => oktaAuth.signOut();

const renderLogin = () => {
        if(!authState) {
          return <div>Loading...</div>;
        }
    
        if(!authState.isAuthenticated) {
            return (
            <div>
                <p>Not Logged in yet</p>
                <button onClick={login}>Login</button>
            </div>
            );
        }
    
        return (
            <div style={{backgroundColor:'blue'}}>
                <p>Logged in!</p>
                <button onClick={logout}>Logout</button>
            </div>
        );
    }    

    return (
        <div style={{height:'100%',justifyContent:'center', alignItems:'center',display:'flex'}}>
            {renderLogin()}
        </div>
    );

FYI

"@okta/okta-auth-js": "^7.2.0",
"@okta/okta-react": "^6.7.0",

Update:
Here's what i'm seeing with oktaAuth after redirected back from Okta Hosted Login.

Okta登录重定向不会更新authState。

Issue resolved:
Root cause identified, it was due to the use of different router from react-router-dom

import { BrowserRouter as Router } from "react-router-dom"; --- use this
// import { HashRouter as Router } from "react-router-dom"; --- this give issue, due to the extra # return in url
英文:

Trying to integrate Okta with ReactJs web. Web app successfully route user to Okta login page, user managed to login successful too. Upon login successful, Okta redirected back to ReactJs web without authState update (authState is NULL while trying to read).

With my implemented code, im aways getting redirected back to Home.js with the Loading... text. What seems to be missing here?

Sample code impelmentation as below

App.js

const oktaAuth = new OktaAuth({
    issuer: &#39;https://{{ourDomain}}.com/oauth2/default&#39;,
    clientId: &#39;{{ourValue}}&#39;,
    redirectUri: window.location.origin + &#39;/login/callback&#39;,
    scopes: [&quot;openid&quot;, &quot;profile&quot;, &quot;email&quot;],
    pkce: true,
  });

&lt;Security oktaAuth={oktaAuth} restoreOriginalUri={restoreOriginalUri}&gt;
        &lt;Switch&gt;
          &lt;Route path=&quot;/login/callback&quot; component={LoginCallback} /&gt;
          &lt;Route path=&quot;/&quot; component={Home} /&gt;
          &lt;SecureRoute path=&quot;/profile&quot; component={Profile} /&gt;
        &lt;/Switch&gt;
      &lt;/Security&gt;

Home.js

const { oktaAuth, authState } = useOktaAuth();
const login = async () =&gt; await oktaAuth.signInWithRedirect();
const logout = async () =&gt; oktaAuth.signOut();

const renderLogin = () =&gt; {
        if(!authState) {
          return &lt;div&gt;Loading...&lt;/div&gt;;
        }
    
        if(!authState.isAuthenticated) {
            return (
            &lt;div&gt;
                &lt;p&gt;Not Logged in yet&lt;/p&gt;
                &lt;button onClick={login}&gt;Login&lt;/button&gt;
            &lt;/div&gt;
            );
        }
    
        return (
            &lt;div style={{backgroundColor:&#39;blue&#39;}}&gt;
                &lt;p&gt;Logged in!&lt;/p&gt;
                &lt;button onClick={logout}&gt;Logout&lt;/button&gt;
            &lt;/div&gt;
        );
    }    

    return (
        &lt;div style={{height:&#39;100%&#39;,justifyContent:&#39;center&#39;, alignItems:&#39;center&#39;,display:&#39;flex&#39;}}&gt;
            {renderLogin()}
        &lt;/div&gt;
    );

FYI

&quot;@okta/okta-auth-js&quot;: &quot;^7.2.0&quot;,
&quot;@okta/okta-react&quot;: &quot;^6.7.0&quot;,

Update:

Here's what i'm seeing with oktaAuth after redirected back from Okta Hosted Login.

Okta登录重定向不会更新authState。

Issue resolved:

Root cause identified, it was due to the use of different router from react-router-dom

import { BrowserRouter as Router } from &quot;react-router-dom&quot;; --- use this
// import { HashRouter as Router } from &quot;react-router-dom&quot;; --- this give issue, due to the extra # return in url

答案1

得分: 1

问题已解决:

已经找到了根本原因,这是因为使用了来自 react-router-dom 的不同路由器。

import { BrowserRouter as Router } from "react-router-dom"; --- 使用这个
// import { HashRouter as Router } from "react-router-dom"; --- 这个会导致问题,因为URL中有额外的 #

另外,在初始化 OktaAuth 时,我多添加了一个键值对。之前它不在我的声明中。

const oktaAuth = new OktaAuth({
    clientId: CLIENT_ID,
    issuer: ISSUER,
    redirectUri: REDIRECT_URI,
    scopes: ['openid', 'profile', 'email'],
    pkce: true,
    disableHttpsCheck: OKTA_TESTING_DISABLEHTTPSCHECK,
  });
英文:

Issue resolved:

Root cause identified, it was due to the use of different router from react-router-dom

import { BrowserRouter as Router } from &quot;react-router-dom&quot;; --- use this
// import { HashRouter as Router } from &quot;react-router-dom&quot;; --- this give issue, due to the extra # in url

Also I have added 1 extra key:value while initializing OktaAuth. Previously it was not part of my declaration.

const oktaAuth = new OktaAuth({
    clientId: CLIENT_ID,
    issuer: ISSUER,
    redirectUri: REDIRECT_URI,
    scopes: [&#39;openid&#39;, &#39;profile&#39;, &#39;email&#39;],
    pkce: true,
    disableHttpsCheck: OKTA_TESTING_DISABLEHTTPSCHECK,
  });

huangapple
  • 本文由 发表于 2023年2月6日 16:17:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定