examples icon indicating copy to clipboard operation
examples copied to clipboard

Logout Example using Auth0 Expo

Open myhendry opened this issue 4 years ago • 4 comments

The example with Auth0 does not show users how to logout from the expo app. I am able to login successfully using the Auth0 example but unable to implement logout and there is scant information on this subject on the web

myhendry avatar Mar 22 '20 13:03 myhendry

This is working for me on iOS

  function handleRedirect() {
    WebBrowser.dismissBrowser();
    navigation.navigate("SignIn");
  }

...
<Button
        onPress={async () => {
          // Adapted from this example for Linking
          // https://github.com/expo/examples/blob/master/with-webbrowser-redirect/app/App.js
          // TODO: Test Android. May run into https://github.com/expo/expo/issues/5555
          Linking.addEventListener("url", handleRedirect);
          const redirectUrl = Linking.makeUrl("/");
          // Adapted from this example for logging out
          // https://github.com/expo/auth0-example/issues/25#issuecomment-468533295
          await WebBrowser.openBrowserAsync(
            `https://${process.env.AUTH0_DOMAIN}/v2/logout?client_id=${process.env.AUTH0_CLIENT_ID}&returnTo=${redirectUrl}`
          );
          Linking.removeEventListener("url", handleRedirect);
        }}
...
/>

Set the allowed logout urls on Auth0 native client to "exp://192.168.0.21:19000/--/"

tiagob avatar Aug 19 '20 16:08 tiagob

Any update on this? I'm having similar problems finding a solution

michaelmang avatar Dec 15 '20 17:12 michaelmang

any update on this?

alokrajiv avatar Jan 02 '21 01:01 alokrajiv

It seems that if you are using the web prompt to "login" to your mobile app, you can't really "logout" as the session is stored in the browser cookie & you can't really clear it on iOS.

I used prompt: 'login' config, which is part of AuthRequestConfig, so that the app's login function will ALWAYS prompt a proper login web screen instead of doing a silent authentication with existing session, this seems to be the recommended approach according to this doc

The code would be something like this:

const [loginResponse, setLoginResponse] = useState()
const [authRequest, setAuthRequest] = useState()

const clientId = 'xxx'
const redirectUri = makeRedirectUri({ ... })
const discoveryDocs = { ... }

const authRequestConfig = {
  clientId,
  redirectUri,
  scopes: [...],
  prompt: 'login', // this is the kicker
  extraParams: {  audience: 'your_auth_audience' },
}

useEffect(() => {
  if (!authRequest) {
    setAuthRequest(new AuthRequest(authRequestConfig) // import AuthRequest from 'expo-auth-session'
  }
}, [authRequest])

useEffect(() => {
  if (loginResponse) {
    if (loginResponse.type === 'success') {
      // login stuff
    } else {
      // other stuff & error handling
    }
  }
}, [loginResponse])

const onPressLoginButton = async () => {
  if (authRequest) {
    const response = await authRequest.promptAsync(discoveryDocs)
    setLoginResponse(response)
  }
}   

shan-du avatar Jul 14 '23 17:07 shan-du