How to Use Supabase Apple OAuth in React Native

Supabase combined with NextJS or Expo makes spinning up a side project in a few hours possible.

Supabase recently added a tutorial for Expo and support for Apple OAuth authentication. However, Apple OAuth does not work out of the box with Expo and Sup…


This content originally appeared on DEV Community and was authored by Dan Curtis

Supabase combined with NextJS or Expo makes spinning up a side project in a few hours possible.

Supabase recently added a tutorial for Expo and support for Apple OAuth authentication. However, Apple OAuth does not work out of the box with Expo and Supabase. So I figured I'd write this article and create a GitHub template.

GIF of Sign in with Apple in template

Supabase and Expo

I followed Supabase's Expo quickstart to get basic authentication working in Expo. The quickstart does not mentioned AsyncStorage which is required in lib/supabase.js to get it working.

My final code:

import AsyncStorage from '@react-native-async-storage/async-storage';
import { createClient } from '@supabase/supabase-js';

// https://reactnative.dev/docs/security#storing-sensitive-info
import { supabaseUrl, supabaseAnonKey } from './supabase-keys';

export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
  localStorage: AsyncStorage,
  detectSessionInUrl: false
});

Supabase Apple OAuth with Expo

Next I followed Supabase's tutorial for Apple authentication. I tried to use Supabase's sign in method onClick in my React Native auth component, which doesn't work:

const { user, session, error } = await supabase.auth.signIn({
  provider: 'apple'
});

The user/session/error all will be null. I was a bit worried Apple OAuth on mobile wouldn't be supported by Supabase's Go True library, but I stumbled upon a PR which adds support Fix: Add id_token grant flow

Instead of using Apple as the provider I decided to use Expo's authentication library to get a token and then pass that to Supabase:

import { startAsync, makeRedirectUri } from 'expo-auth-session';

import { supabase } from '../lib/supabase';
import { supabaseUrl } from '../lib/supabase-keys';

const signInWithApple = async () => {
    const returnUrl = makeRedirectUri({ useProxy: false });
    const provider = 'apple';
    const authUrl = `${supabaseUrl}/auth/v1/authorize?provider=${provider}&redirect_to=${returnUrl}`;

    const response = await startAsync({ authUrl, returnUrl });

    if (!response || !response.params?.refresh_token) {
      return;
    }

    await supabase.auth.signIn({
      refreshToken: response.params.refresh_token
    });
};

The full code is available on GitHub. Apple OAuth with Supabase and support for React Native is relatively new. Feedback is always welcome if there's a better way of doing things.


This content originally appeared on DEV Community and was authored by Dan Curtis


Print Share Comment Cite Upload Translate Updates
APA

Dan Curtis | Sciencx (2022-03-20T15:14:20+00:00) How to Use Supabase Apple OAuth in React Native. Retrieved from https://www.scien.cx/2022/03/20/how-to-use-supabase-apple-oauth-in-react-native/

MLA
" » How to Use Supabase Apple OAuth in React Native." Dan Curtis | Sciencx - Sunday March 20, 2022, https://www.scien.cx/2022/03/20/how-to-use-supabase-apple-oauth-in-react-native/
HARVARD
Dan Curtis | Sciencx Sunday March 20, 2022 » How to Use Supabase Apple OAuth in React Native., viewed ,<https://www.scien.cx/2022/03/20/how-to-use-supabase-apple-oauth-in-react-native/>
VANCOUVER
Dan Curtis | Sciencx - » How to Use Supabase Apple OAuth in React Native. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/20/how-to-use-supabase-apple-oauth-in-react-native/
CHICAGO
" » How to Use Supabase Apple OAuth in React Native." Dan Curtis | Sciencx - Accessed . https://www.scien.cx/2022/03/20/how-to-use-supabase-apple-oauth-in-react-native/
IEEE
" » How to Use Supabase Apple OAuth in React Native." Dan Curtis | Sciencx [Online]. Available: https://www.scien.cx/2022/03/20/how-to-use-supabase-apple-oauth-in-react-native/. [Accessed: ]
rf:citation
» How to Use Supabase Apple OAuth in React Native | Dan Curtis | Sciencx | https://www.scien.cx/2022/03/20/how-to-use-supabase-apple-oauth-in-react-native/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.