authts / react-oidc-context

Lightweight auth library based on oidc-client-ts for React single page applications (SPA). Support for hooks and higher-order components (HOC).
MIT License
618 stars 60 forks source link

Possibility to access signinCallback function? #1216

Open ns-ovc opened 2 months ago

ns-ovc commented 2 months ago

I have a website that is also converted into an app via Capacitor. The login on the website works without any problems. But not for the native app. There is a hook for Capacitor when the redirect URL is called from the auth provider.

    CapApp.addListener("appUrlOpen", async ({ url }) => {
        // TODO: handle URL
    });

A URL is returned that contains the code & session_state. I would need to call a function there to handle the URL (not window.location.href). Is there a way to do this?

In auth0-react there is a function called "handleRedirectCallback(url)" that does just that.

pamapa commented 2 months ago

Have you tried onSigninCallback? See https://github.com/authts/react-oidc-context/blob/main/src/AuthProvider.tsx#L42

ns-ovc commented 2 months ago

Have you tried onSigninCallback? See https://github.com/authts/react-oidc-context/blob/main/src/AuthProvider.tsx#L42

Thank you for the fast response! How should I use this event? The user is not yet authorized at this time.

The process is:

ns-ovc commented 2 months ago

If I think correctly, I need access to this method via useAuth hook:

https://github.com/authts/react-oidc-context/blob/39bad747a847d8c82881f4f7e5e737255c89a871/src/AuthProvider.tsx#L237

https://github.com/authts/oidc-client-ts/blob/8a386f559cbb4d605660f8a3040a4ec2b1cf11c4/src/UserManager.ts#L368

Here I can specify my custom URL to log in the user.

pamapa commented 2 months ago

If I think correctly, I need access to this method via useAuth hook:

You can register your own function of onSigninCallback via setttings into the auth context <AuthProvider onSigninCallback={(user) => console.log(user)} ...>....