Skip to content

Instantly share code, notes, and snippets.

@bjollans
Last active April 29, 2024 05:53
Show Gist options
  • Save bjollans/b1008e70022e52ed50fce4684bf27359 to your computer and use it in GitHub Desktop.
Save bjollans/b1008e70022e52ed50fce4684bf27359 to your computer and use it in GitHub Desktop.
Minimal version to get Google Sign in working with Supabase on IOS in React Native with Expo. If you are not using Expo, follow the setup guide of `react-native-app-auth` instead of using `rn-app-auth-plugin.js` and `app.json`.
{
...
"plugins": [
[
"./rn-app-auth-plugin"
]
],
...
}
import { Session } from '@supabase/supabase-js';
import supabase from './util/supabase';
import { useEffect, useState } from 'react';
import AuthForm from './components/AuthForm';
export default function App() {
const [session, setSession] = useState<Session | null>(null)
useEffect(() => {
supabase.auth.getSession().then(({ data: { session } }) => {
setSession(session)
})
supabase.auth.onAuthStateChange((_event, session) => {
setSession(session)
})
}, []);
return session && session.user ? <INSERT_YOUR_CONTENT_HERE> : <AuthForm />;
}
import React from 'react';
import { Alert, StyleSheet, View } from 'react-native';
import { Button } from 'react-native-elements';
import supabase from '../util/supabase';
import { authorize } from 'react-native-app-auth';
export default function AuthForm() {
async function authGoogle() {
const config = {
issuer: 'https://accounts.google.com',
clientId: 'GOOGLE_GUID.apps.googleusercontent.com',
redirectUrl: 'com.googleusercontent.apps.GOOGLE_GUID:/oauth2redirect/google',
scopes: ['email'],
useNonce: false,
};
try {
const authState = await authorize(config);
const { data, error } = await supabase.auth.signInWithIdToken({
provider: 'google',
token: authState.idToken,
})
} catch (error) {
Alert.alert('Something went wrong, please try again later');
}
}
return (
<Button title="[G] Sign in with Google" onPress={() => authGoogle()} />
)
}
const {
withAppDelegate,
} = require('@expo/config-plugins');
function withRnAppAuth(config) {
return withAppDelegate(
config,
async (
config
) => {
const xcodeProject = config.modResults;
xcodeProject.contents = xcodeProject.contents.replace('#import "AppDelegate.h"', `#import <RCTAppDelegate.h>
#import <UIKit/UIKit.h>
#import <Expo/Expo.h>
#import <React/RCTLinkingManager.h>
#import "RNAppAuthAuthorizationFlowManager.h"
@interface AppDelegate : EXAppDelegateWrapper <RNAppAuthAuthorizationFlowManager>
@property(nonatomic, weak) id<RNAppAuthAuthorizationFlowManagerDelegate> authorizationFlowManagerDelegate;
@end`);
return config;
}
);
};
module.exports = withRnAppAuth;
import AsyncStorage from '@react-native-async-storage/async-storage';
import { createClient } from "@supabase/supabase-js";
import { AppState } from "react-native";
const supabase = createClient(
<INSERT_YOUR_SUPABASE_URL>,
<INSERT_YOUR_SUPABASE_PUBLIC_KEY>,
{
auth: {
storage: AsyncStorage,
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: false,
}
}
);
AppState.addEventListener('change', (state) => {
if (state === 'active') {
supabase.auth.startAutoRefresh()
} else {
supabase.auth.stopAutoRefresh()
}
});
export default supabase;
@bjollans
Copy link
Author

Minimal version to get Google Sign in working with Supabase on IOS in React Native with Expo. If you are not using Expo, follow the setup guide of react-native-app-auth instead of using rn-app-auth-plugin.js and app.json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment