@saas-ui/supabase
Version:
Saas UI Supabase Auth integration
200 lines (198 loc) • 5.86 kB
JavaScript
'use client'
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
createAuthService: () => createAuthService
});
module.exports = __toCommonJS(src_exports);
// 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);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createAuthService
});
//# sourceMappingURL=index.js.map