Julian
Julian

Reputation: 99

Issues with Expo Auth Session implementation

I am having issues with the implementation of Expo Auth Session.

I am trying Google Oauth Login in Expo Auth Session as documented here: https://docs.expo.io/guides/authentication/#google

WebBrowser.maybeCompleteAuthSession();

const GoogleButton = () => {
  // Endpoint
  const discovery = useAutoDiscovery('https://accounts.google.com');

  // Request
  const [request, response, promptAsync] = useAuthRequest(
    {
      clientId: 'MYID',
      scopes: ['email', 'profile'],
      // For usage in managed apps using the proxy
      redirectUri: makeRedirectUri({
        // For usage in bare and standalone
        native: 'com.googleusercontent.apps.MYID://redirect',
        useProxy: true,
      }),
    },
    discovery,
  );

  console.log(request);
  console.log(response);

  return (
    <Button
      onPress={promptAsync}
      icon={GoogleIcon}
    />
  );
};

The browser opens, I can successfully log in with google, but when I get redirected to the app, response resolves to Object { "type": "dismiss", }

I also tried implementing oauth with a different oauth service:

WebBrowser.maybeCompleteAuthSession();

const HiveButton = () => {
  // Endpoint
  const discovery = {
    authorizationEndpoint:
      'https://hivesigner.com/login-request/my.app',
  };

  // Request
  const [request, response, promptAsync] = useAuthRequest(
    {
      scopes: ['posting'],
      // For usage in managed apps using the proxy
      redirectUri: makeRedirectUri({
        useProxy: true,
      }),
    },
    discovery,
  );

  console.log(request);
  console.log(response);

  return (
    <Button
      onPress={promptAsync}
      icon={HiveIcon}
    />
  );
};

The browser opens, I can log in successfully, but instead of being redirected to the app, I get "Something went wrong trying to finish signing in. Please close this screen to go back to the app." on auth.expo.io/@me/myapp, even though the parameter code has the correct login token that I want to pass to my app.

Upvotes: 8

Views: 19186

Answers (4)

Vasyl Nahuliak
Vasyl Nahuliak

Reputation: 2468

Use expo-auth-session/providers/google from the https://docs.expo.dev/guides/authentication/#google example

import * as React from 'react';
import * as WebBrowser from 'expo-web-browser';
import * as Google from 'expo-auth-session/providers/google';
import { Button } from 'react-native';

WebBrowser.maybeCompleteAuthSession();

export default function App() {
  const [request, response, promptAsync] = Google.useAuthRequest({
    expoClientId: 'GOOGLE_GUID.apps.googleusercontent.com',
    iosClientId: 'GOOGLE_GUID.apps.googleusercontent.com',
    androidClientId: 'GOOGLE_GUID.apps.googleusercontent.com',
    webClientId: 'GOOGLE_GUID.apps.googleusercontent.com',
  });

  React.useEffect(() => {
    if (response?.type === 'success') {
      const { authentication } = response;
      }
  }, [response]);

  return (
    <Button
      disabled={!request}
      title="Login"
      onPress={() => {
        promptAsync();
        }}
    />
  );
}

Upvotes: -1

paichato
paichato

Reputation: 188

Try this:

-Login to your expo account: on command line type:

expo login
##then your account credencials##

then restart your expo instance:

expo start

And everything should work fine

Upvotes: 3

Julian
Julian

Reputation: 99

I’ve now solved the second issue by using AuthSession.startAsync instead:

 const handleLogin = async () => {
    const redirectUrl = AuthSession.getRedirectUrl({ useProxy: true });
    const response = await AuthSession.startAsync({
      authUrl: `https://hivesigner.com/login-request/my.app?redirect_uri=${redirectUrl}&scope=posting`,
    });
console.log(response)
}

Instead of using AuthSession for Google login I’m using the expo-google-sign-in implementation

Upvotes: 0

Sean O&#39;Leary
Sean O&#39;Leary

Reputation: 618

I have the exact same issue. I've posted on the Expo forums too and tried to contact the devs about it but nobody's responding to me. I think it's been broken with a recent change. If you look at the redirect URL it's supposed to have two more query parameters one for the authentication URL and one for the return URL

Upvotes: 0

Related Questions