Skip to content

Instantly share code, notes, and snippets.

@revskill10
Forked from michaelkremenetsky/auth.ts
Created February 8, 2024 08:23
Show Gist options
  • Save revskill10/7e8996498b943f8dc8e04efae6592a83 to your computer and use it in GitHub Desktop.
Save revskill10/7e8996498b943f8dc8e04efae6592a83 to your computer and use it in GitHub Desktop.

Revisions

  1. @michaelkremenetsky michaelkremenetsky revised this gist Oct 7, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion auth.ts
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ export const useAuth = () => {
    try {
    // whatever route you want to deeplink to; make sure to configure in dashboard
    const redirectUri = "refeed://login";
    const provider = "aplpe";
    const provider = "apple";
    const response = await WebBrowser.openAuthSessionAsync(
    `${process.env.SUPABASE_URL}/auth/v1/authorize?provider=${provider}&redirect_to=${redirectUri}`,
    redirectUri,
  2. @michaelkremenetsky michaelkremenetsky revised this gist Oct 5, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion auth.ts
    Original file line number Diff line number Diff line change
    @@ -41,7 +41,7 @@ export const useAuth = () => {

    const signInWithAppleOnAndroid = async () => {
    try {
    // whatever route you ant to deeplink to; make sure to configure in dashboard
    // whatever route you want to deeplink to; make sure to configure in dashboard
    const redirectUri = "refeed://login";
    const provider = "aplpe";
    const response = await WebBrowser.openAuthSessionAsync(
  3. @michaelkremenetsky michaelkremenetsky created this gist Oct 5, 2023.
    117 changes: 117 additions & 0 deletions auth.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,117 @@
    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 ant to deeplink to; make sure to configure in dashboard
    const redirectUri = "refeed://login";
    const provider = "aplpe";
    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,
    };
    };