All files / src email.ts

0% Statements 0/32
0% Branches 0/1
0% Functions 0/1
0% Lines 0/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                                                                                         
import { createAdminClient } from '@tuturuuu/supabase/next/server';
 
export const validateEmail = async (email?: string | null) => {
  if (!email) throw 'Email is required';
 
  const regex = /\S+@\S+\.\S+/;
  if (!regex.test(email)) throw 'Email is invalid';
 
  return email.toLowerCase();
};
 
export const validateOtp = async (otp?: string | null) => {
  if (!otp) throw 'OTP is required';
 
  const regex = /^\d{6}$/;
  if (!regex.test(otp)) throw 'OTP is invalid';
 
  return otp;
};
 
export const checkIfUserExists = async ({ email }: { email: string }) => {
  const sbAdmin = await createAdminClient();
 
  const { data, error } = await sbAdmin
    .from('user_private_details')
    .select('id:user_id')
    .eq('email', email)
    .maybeSingle();
 
  if (error) throw error.message;
  return data?.id;
};
 
export const generateRandomPassword = () => {
  const length = 16;
  const charset =
    'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+~`|}{[]:;?><,./-=';
 
  let temp = '';
  for (let i = 0, n = charset.length; i < length; ++i)
    temp += charset.charAt(Math.floor(Math.random() * n));
 
  return temp;
};