Files
formbricks-formbricks/apps/web/lib/users.ts
Matti Nannt 5c378bc8ce Feature/monorepo #95 (#105)
Move repository into a monorepo with turborepo and pnpm.
This is a big change in the way the code is organized, used and deployed.
2022-10-13 09:46:43 +02:00

84 lines
2.1 KiB
TypeScript

import { hashPassword } from "./auth";
export const createUser = async (firstname, lastname, email, password) => {
const hashedPassword = await hashPassword(password);
try {
const res = await fetch(`/api/public/users`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
firstname,
lastname,
email,
password: hashedPassword,
}),
});
if (res.status !== 200) {
const json = await res.json();
throw Error(json.error);
}
return await res.json();
} catch (error) {
throw Error(`${error.message}`);
}
};
export const resendVerificationEmail = async (email) => {
try {
const res = await fetch(`/api/public/users/verification-email`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email,
}),
});
if (res.status !== 200) {
const json = await res.json();
throw Error(json.error);
}
return await res.json();
} catch (error) {
throw Error(`${error.message}`);
}
};
export const forgotPassword = async (email: string) => {
try {
const res = await fetch(`/api/public/users/forgot-password`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email,
}),
});
if (res.status !== 200) {
const json = await res.json();
throw Error(json.error);
}
return await res.json();
} catch (error) {
throw Error(`${error.message}`);
}
};
export const resetPassword = async (token, password) => {
const hashedPassword = await hashPassword(password);
try {
const res = await fetch(`/api/public/users/reset-password`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
token,
hashedPassword,
}),
});
if (res.status !== 200) {
const json = await res.json();
throw Error(json.error);
}
return await res.json();
} catch (error) {
throw Error(`${error.message}`);
}
};