UNPKG

@saas-ui/supabase

Version:

Saas UI Supabase Auth integration

174 lines (173 loc) 4.83 kB
'use client' // src/supabase.ts var createAuthService = (supabase, serviceOptions) => { const onLogin = async (params, authOptions) => { const options = { ...serviceOptions == null ? void 0 : serviceOptions.loginOptions, ...authOptions, emailRedirectTo: authOptions == null ? void 0 : authOptions.redirectTo }; function authenticate() { const { email, password, provider, phone } = params; if (email && password) { return supabase.auth.signInWithPassword({ email, password, options }); } else if (email) { return supabase.auth.signInWithOtp({ email, options }); } else if (provider) { return supabase.auth.signInWithOAuth({ provider, options }); } else if (phone && password) { return supabase.auth.signInWithPassword({ phone, password, options }); } else if (phone) { return supabase.auth.signInWithOtp({ phone, options }); } throw new Error("Could not find correct authentication method"); } const resp = await authenticate(); if (resp.error) { throw resp.error; } if (isOauthResponse(resp)) { return; } return resp.data.user; }; const onSignup = async (params, authOptions) => { async function signup() { const { email, phone, password } = params; const options = { ...serviceOptions == null ? void 0 : serviceOptions.signupOptions, ...authOptions, emailRedirectTo: authOptions == null ? void 0 : authOptions.redirectTo }; if (email && password) { return await supabase.auth.signUp({ email, password, options }); } else if (phone && password) { return await supabase.auth.signUp({ phone, password, options }); } else if (email) { return supabase.auth.signInWithOtp({ email, options }); } else if (phone) { return supabase.auth.signInWithOtp({ phone, options }); } } const resp = await signup(); if (resp == null ? void 0 : resp.error) { throw resp.error; } return resp == null ? void 0 : resp.data.user; }; const onVerifyOtp = async (params, options) => { const { email, phone, otp, type } = params; if (email) { const verify = { email, token: otp, type: type || "signup", options: { ...serviceOptions == null ? void 0 : serviceOptions.verifyOptions, ...options } }; const resp = await supabase.auth.verifyOtp(verify); if (resp.error) { throw resp.error; } return Boolean(resp.data.session); } if (phone) { const verify = { phone, token: otp, type: type || "sms", options: { ...serviceOptions == null ? void 0 : serviceOptions.verifyOptions, ...options } }; const resp = await supabase.auth.verifyOtp(verify); if (resp.error) { throw resp.error; } return Boolean(resp.data.session); } throw new Error("You need to provide either email or phone"); }; const onLogout = async (options) => { return await supabase.auth.signOut(options); }; const onAuthStateChange = (callback) => { const { data } = supabase.auth.onAuthStateChange( (event, session) => { callback(session == null ? void 0 : session.user); } ); return () => data == null ? void 0 : data.subscription.unsubscribe(); }; const onLoadUser = async () => { const { data, error } = await supabase.auth.getUser(); if (error) { throw error; } return data.user; }; const onGetToken = async () => { var _a; const { data, error } = await supabase.auth.getSession(); if (error) { throw error; } return ((_a = data.session) == null ? void 0 : _a.access_token) || null; }; const onResetPassword = async ({ email }, options) => { const { error } = await supabase.auth.resetPasswordForEmail(email, { ...serviceOptions == null ? void 0 : serviceOptions.resetPasswordOptions, ...options }); if (error) { throw error; } }; const onUpdatePassword = async ({ password }) => { const { error } = await supabase.auth.updateUser({ password }); if (error) { throw error; } }; return { onLogin, onSignup, onVerifyOtp, onLogout, onAuthStateChange, onLoadUser, onGetToken, onResetPassword, onUpdatePassword }; }; function isOauthResponse(response) { var _a; return Boolean((_a = response.data) == null ? void 0 : _a.provider); } export { createAuthService }; //# sourceMappingURL=index.mjs.map