Skip to content

Instantly share code, notes, and snippets.

@michaelkremenetsky
Last active February 8, 2024 08:23
Show Gist options
  • Save michaelkremenetsky/bb177f997106141a4399fcf3f03cda05 to your computer and use it in GitHub Desktop.
Save michaelkremenetsky/bb177f997106141a4399fcf3f03cda05 to your computer and use it in GitHub Desktop.
Supabase Auth Hook
import { Alert } from "react-native";
import * as WebBrowser from "expo-web-browser";
import { useSupabaseClient } from "@supabase/auth-helpers-react";
import { initiateAppleSignIn } from "../utils/auth";
export const useAuth = () => {
const Supabase = useSupabaseClient();
const signInWithPassword = async (
email: string,
password: string,
type: "Login" | "Sign Up",
) => {
const { error } =
type == "Sign Up"
? await Supabase.auth.signUp({
email,
password,
})
: await Supabase.auth.signInWithPassword({
email,
password,
});
if (!error && type == "Sign Up") {
Alert.alert("Error", "Check Your Email!");
}
if (error) return Alert.alert("Error", error.message);
};
const signInWithApple = async () => {
const { token, nonce } = await initiateAppleSignIn();
const { error } = await Supabase.auth.signInWithIdToken({
provider: "apple",
token,
nonce,
});
if (error) return Alert.alert("Error", error.message);
};
const signInWithAppleOnAndroid = async () => {
try {
// whatever route you want to deeplink to; make sure to configure in dashboard
const redirectUri = "refeed://login";
const provider = "apple";
const response = await WebBrowser.openAuthSessionAsync(
`${process.env.SUPABASE_URL}/auth/v1/authorize?provider=${provider}&redirect_to=${redirectUri}`,
redirectUri,
{},
);
if (response.type === "success") {
const url = response.url;
const params = url.split("#")[1];
const accessToken = params?.split("&")[0]?.split("=")[1];
const refreshToken = params?.split("&")[2]?.split("=")[1];
const { error } = await Supabase.auth.setSession({
// You can export data from this if you need it.
access_token: accessToken!,
refresh_token: refreshToken!,
});
if (error) {
Alert.alert("Error", error.message);
}
}
} catch (error) {
Alert.alert("Error", "Issue with Google Auth try again");
} finally {
WebBrowser.maybeCompleteAuthSession();
}
WebBrowser.maybeCompleteAuthSession();
};
const signInWithGoogle = async () => {
// Remember in order for this work you have to configure the Redirect URLs in the Supabase Dashboard
try {
// whatever route you want to deeplink to; make sure to configure in dashboard
const redirectUri = "refeed://login";
const provider = "google";
const response = await WebBrowser.openAuthSessionAsync(
`${process.env.EXPO_PUBLIC_SUPABASE_URL}/auth/v1/authorize?provider=${provider}&redirect_to=${redirectUri}`,
redirectUri,
);
if (response.type === "success") {
const url = response.url;
const params = url.split("#")[1];
const accessToken = params?.split("&")[0]?.split("=")[1];
const refreshToken = params?.split("&")[2]?.split("=")[1];
const { error } = await Supabase.auth.setSession({
// You can export data from this if you need it.
access_token: accessToken!,
refresh_token: refreshToken!,
});
if (error) {
Alert.alert("Error", error.message);
}
}
} catch (error) {
Alert.alert("Error", "Issue with Google Auth try again");
} finally {
WebBrowser.maybeCompleteAuthSession();
}
WebBrowser.maybeCompleteAuthSession();
};
return {
signInWithPassword,
signInWithGoogle,
signInWithApple,
signInWithAppleOnAndroid,
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment