Skip to content

Instantly share code, notes, and snippets.

@revskill10
Forked from michaelkremenetsky/auth.ts
Created February 8, 2024 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save revskill10/7e8996498b943f8dc8e04efae6592a83 to your computer and use it in GitHub Desktop.
Save revskill10/7e8996498b943f8dc8e04efae6592a83 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