Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

1.8 KB
Raw Blame
"use client";

import useUser from "@/lib/swr/use-user";
import { Button } from "@dub/ui";
import { cn } from "@dub/utils";
import { useState } from "react";
import { toast } from "sonner";

// Displayed when the user doesn't have a password set for their account
export const RequestSetPassword = () => {
  const { user } = useUser();
  const [sending, setSending] = useState(false);

  // Send an email to the user with instructions to set their password
  const sendPasswordSetRequest = async () => {
    try {
      setSending(true);

      const response = await fetch("/api/user/set-password", {
        method: "POST",
      });

      if (response.ok) {
        toast.success(
          `We've sent you an email to ${user?.email} with instructions to set your password`,
        );
        return;
      }

      const { error } = await response.json();
      throw new Error(error.message);
    } catch (error) {
      toast.error(error.message);
    } finally {
      setSending(false);
    }
  };

  return (
    <div className="flex flex-col gap-2 rounded-xl border border-neutral-200 bg-white p-6">
      <h2 className="text-base font-semibold">Password</h2>
      <p className="pb-2 text-sm text-neutral-500">
        {user?.provider && (
          <>
            Your account is managed by{" "}
            <span
              className={cn(
                "font-medium capitalize text-neutral-700",
                user?.provider === "saml" && "uppercase",
              )}
            >
              {user?.provider}
            </span>
            .{" "}
          </>
        )}
        You can set a password to use with your Dub account.
      </p>
      <Button
        text="Create account password"
        onClick={sendPasswordSetRequest}
        loading={sending}
        disabled={sending}
        className="w-fit"
      />
    </div>
  );
};