2 years ago

#56061

test-img

abg

expo auth response is always null

I've searched on here already but it seems all the answers are very outdated or they are questions that haven't been answered.

I've got an expo app SDK 43 and I'm using their auth library to authorize a reddit login. I've followed the example here https://docs.expo.dev/guides/authentication/#reddit to produce this code

import React from 'react';
import * as WebBrowser from 'expo-web-browser';
import { makeRedirectUri, ResponseType, useAuthRequest } from 'expo-auth-session';
import { Button } from 'react-native-paper';
import { useAppDispatch } from '../../common/hooks/redux';
import { setAuthCode } from '../../common/state/authSlice';

WebBrowser.maybeCompleteAuthSession();

// Endpoint
const discovery = {
    authorizationEndpoint: 'https://www.reddit.com/api/v1/authorize.compact',
    tokenEndpoint: 'https://www.reddit.com/api/v1/access_token',
};

const LoginScreen = () => {
    const dispatch = useAppDispatch();
    const [request, response, promptAsync] = useAuthRequest(
        {
            responseType: ResponseType.Token,
            clientId: 'MY_CLIENT_ID',
            scopes: ['identity'],
            redirectUri: makeRedirectUri({
                scheme: undefined,
            }),
        },
        discovery
    );

    React.useEffect(() => {
        console.log(`response is ${response}`);
        if (response?.type === 'success') {
            const { access_token } = response.params;
            dispatch(setAuthCode(access_token));
            console.log(access_token);
        } else {
            console.log(response);
        }
    }, [response]);

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

export default LoginScreen;

But despite the fact that the login button correctly takes me to the login screen, I successfully log in and allow the app (and if I go onto the web separately I can see in my account that the app is there under the authorized apps.)

At this point on my device one of two things happens: 1. something causes the app to disconnect from metro and it hangs on a loading wheel belonging to the greater expo stuff, or 2. It successfully gets back to the app but it redownloads the bundle and the response is null.

What is screwing up here?

javascript

react-native

oauth-2.0

expo

reddit

0 Answers

Your Answer

Accepted video resources