Sherlock / components / authHelpers.tsx
authHelpers.tsx
Raw
import { supabase } from "./supabase";
import { makeRedirectUri } from "expo-auth-session";
import * as QueryParams from "expo-auth-session/build/QueryParams";
import * as Linking from "expo-linking";
import global from "../constants/global";
import { createUser, deviceLogin } from "./backend";
import { useAuth } from "./authContext";

type SignUpParameters = {
    username:string,
    email: string;
    password: string;
    nickname: string;
};

// export async function signUp({email,password}: SignUpParameters) {
//     const { data, error } = await supabase.auth.signUp({
//         email,
//         password,
//     })

//     if (error) {
//         console.log("Error signing up: ", error);
//         throw error;
//     }else if(!data.session){
//         console.log("Signed up, Session is null");
//         global.waitingForSession = true;
//     }else {
//         console.log("User signed up: ", data.session);

//         createUser().then((user) => {
//             console.log("User created in Sherlock: ", user);
//         }).catch((error) => {
//             console.log("Error creating user in Sherlock: ", error);
//             throw error;
//         })

//         return data.session;
//     }
// }

export async function signInOtp(email: string) {
    const { data, error} = await supabase.auth.signInWithOtp({
        email: email,
        options:{
            emailRedirectTo: '../app/(tabs)/(home)/home',
            shouldCreateUser: false
        }
    })

    if (error) {
        console.log("Error signing in: ", error);
        throw error;
    }

    if (data.user) {
        console.log("User signed in: ", data.user);
    } else {
        console.log("User not found");
        throw new Error("User not found");
    }
}

export async function verifyOtp(hash:string, type:string){
    const { data, error} = await supabase.auth.verifyOtp({
        token_hash: hash,
        type: type as any,
    })

    if (error) {
        console.log("Error verifying OTP: ", error);
        throw error;
    }
    if (data.user){
        console.log("User verified: ", data.user.email);
        return data;
    } else {
        console.log("User not found");
        throw new Error("User not found");
    }
}

export async function createSessionFromUrl(url: string) {
    const {params, errorCode} = QueryParams.getQueryParams(url)

    console.log("Params: ", params);

    if(errorCode) throw new Error(errorCode);
    const { access_token, refresh_token} = params;

    const { data, error } = await supabase.auth.setSession({
        access_token,
        refresh_token,
    });
    if(error) throw error;

    createUser().then((user) => {
        console.log("User created in Sherlock: ", user);
    }).catch((error) => {
        console.log("Error creating user in Sherlock: ", error);
        throw error;
    })

    return data.session;
}

export async function updateEmail(email:string){
    const { data, error } = await supabase.auth.updateUser({
        email: email,
    });

    if (error) {
        console.log("Error updating email: ", error);
        throw error;
    }

    if (data.user) {
        console.log("Email updated to : ", data.user.email);
    } else {
        console.log("User not found");
        throw new Error("User not found");
    }
}

export async function sendRecovery(email:string){
    const { data, error } = await supabase.auth.resetPasswordForEmail(email, {
        redirectTo: 'sherlock://(auth)/updatePassword',
    })

    if (error) {
        console.log("Error sending recovery email: ", error);
        throw error;
    }
    if (data) {
        console.log("Recovery email sent.");
    }
}

export async function updatePassword(password:string){
    const { data, error } = await supabase.auth.updateUser({
        password: password,
    });

    if (error) {
        console.log("Error updating password: ", error);
        throw error;
    }

    if (data.user) {
        console.log("Password updated for user: ", data.user.email);
    } else {
        console.log("User not found");
        throw new Error("User not found");
    }
}

/**
 * Resends confirmation email
 * @param email email to send confirmation to
 * @param type 1 for sign up confirmation, 2 for email change confirmation
 */
export async function resendConfirmation(email:string, type:number){
    if(type !== 1 && type !== 2){
        console.log("Invalid type for resend confirmation");
        throw new Error("Invalid type for resend confirmation");
    }
    let t:any = type == 1 ? 'signup' : 'email_change';
    const {error} = await supabase.auth.resend({
        type: t,
        email: email,
    })

    if (error) {
        console.log("Error resending confirmation email: ", error);
        throw error;
    }
}