Guest User

Untitled

a guest
Apr 27th, 2023
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.10 KB | Software | 0 0
  1. import Layout from "../components/Layout";
  2. import { zodResolver } from "@hookform/resolvers/zod";
  3. import { z } from "zod";
  4. import {  useSupabaseClient } from "@supabase/auth-helpers-react";
  5.  
  6. import { SubmitHandler, useForm } from "react-hook-form";
  7. const formSchema = z.object({
  8.   password1: z.string().min(4, { message: "Passord må være over 4 tegn" }),
  9.   password2: z.string().min(4, { message: "Passord må være over 4 tegn" }),
  10. });
  11. type FormValues = z.infer<typeof formSchema>;
  12.  
  13. const ResetPassword = () => {
  14.  
  15.   const supabase = useSupabaseClient();
  16.   const supaResett: SubmitHandler<FormValues> = async ({
  17.     password1,
  18.     password2}, e?: React.BaseSyntheticEvent ) => {
  19.       e?.preventDefault();
  20.     if (password1 !== password2) {
  21.       console.log("Passwords are not alike");
  22.       return;
  23.     }
  24.     const { data, error } = await supabase.auth.updateUser({
  25.       password: password1,
  26.     });
  27.  
  28.     if (data) console.log("Update successfull, data is  ", data);
  29.     if (error) console.log("There was en error ", error);
  30.   };
  31.  
  32.   const {
  33.     register,
  34.     handleSubmit,
  35.     formState: { errors },
  36.   } = useForm<FormValues>({
  37.     resolver: zodResolver(formSchema),
  38.   });
  39.   return (
  40.     <Layout>
  41.       <p>Test</p>
  42.       <form onSubmit={void handleSubmit(supaResett)}>
  43.         <label htmlFor="password1">Password</label>
  44.         <div aria-live="assertive">
  45.           {errors?.password1?.message && <p>Feilmelding her </p>}
  46.         </div>
  47.         <input
  48.           type="text"
  49.           autoComplete="false"
  50.           id="password1"
  51.           {...register("password1")}
  52.         />
  53.         <label htmlFor="password2">Password</label>
  54.         <div aria-live="assertive">
  55.           {errors?.password1?.message && <p>Feilmelding her </p>}
  56.         </div>
  57.         <input
  58.           type="text"
  59.           autoComplete="false"
  60.           id="password2"
  61.           {...register("password2")}
  62.         />
  63.         <button type="submit" className="rounded bg-purple-500 px-4 py-2">
  64.           Resett passord
  65.         </button>
  66.       </form>
  67.     </Layout>
  68.   );
  69. };
  70. export default ResetPassword;
  71.  
Advertisement
Add Comment
Please, Sign In to add comment