Skip to content

Commit

Permalink
Merge pull request #701 from FleetAdmiralJakob/improved-sign-in-page
Browse files Browse the repository at this point in the history
  • Loading branch information
FleetAdmiralJakob authored Nov 15, 2024
2 parents 6531749 + 563aa4a commit e581ead
Showing 1 changed file with 54 additions and 13 deletions.
67 changes: 54 additions & 13 deletions src/app/(auth)/sign-in/signin-form.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";

import { useSignIn } from "@clerk/nextjs";
import { useAuth, useSignIn } from "@clerk/nextjs";
import { isClerkAPIResponseError } from "@clerk/nextjs/errors";
import { zodResolver } from "@hookform/resolvers/zod";
import { Button } from "~/components/ui/button";
import {
Expand All @@ -15,8 +16,9 @@ import { Input } from "~/components/ui/input";
import { cn } from "~/lib/utils";
import { useConvexAuth } from "convex/react";
import { LoaderCircle } from "lucide-react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import React, { useEffect, useState } from "react";
import React, { ReactNode, useEffect, useState } from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";

Expand Down Expand Up @@ -55,14 +57,17 @@ export const formSchema = z.object({
});

export function SignInForm() {
const [, setFormIsLoading] = useState(false);
const [formIsLoading, setFormIsLoading] = useState(false);
const [signInComplete, setSignInComplete] = React.useState(false);

const { isLoaded, signIn, setActive } = useSignIn();
const { signOut } = useAuth();

const { isLoading, isAuthenticated } = useConvexAuth();
const { isAuthenticated } = useConvexAuth();

const [wholeFormError, setWholeFormError] = useState<null | string>(null);
const [wholeFormError, setWholeFormError] = useState<
null | string | ReactNode
>(null);
const router = useRouter();

const form = useForm<z.infer<typeof formSchema>>({
Expand Down Expand Up @@ -99,10 +104,42 @@ export function SignInForm() {
await setActive({ session: result.createdSessionId });
setSignInComplete(true);
}
} catch {
setWholeFormError(
"The Username + ID or Password you entered is incorrect. Please try again.",
);
} catch (err) {
if (isClerkAPIResponseError(err)) {
if (err.errors.some((err) => err.code === "session_exists")) {
setWholeFormError(
// "You are already signed in. Please sign out before signing in again.",
<div>
You are already signed in. Please{" "}
<button className="underline" onMouseDown={() => signOut()}>
sign out
</button>{" "}
before signing in again. Alternatively you can go back to the{" "}
<Link className="underline" href="/chats">
home page
</Link>
.
</div>,
);
} else if (
err.errors.some((err) => err.code === "form_identifier_not_found")
) {
setWholeFormError(
"The username + id you entered does not exist. Please try again.",
);
} else if (
err.errors.some((err) => err.code === "form_password_incorrect")
) {
form.setError("password", {
type: "manual",
message: "The password you entered is incorrect. Please try again.",
});
} else {
setWholeFormError("Something went wrong. Please try again.");
}
} else {
setWholeFormError("Something went wrong. Please try again.");
}
}

setFormIsLoading(false);
Expand Down Expand Up @@ -195,8 +232,12 @@ export function SignInForm() {
</FormItem>
)}
/>
<Button disabled={isLoading} type="submit" aria-disabled={isLoading}>
{isLoading ? (
<Button
disabled={formIsLoading}
type="submit"
aria-disabled={formIsLoading}
>
{formIsLoading ? (
<>
<LoaderCircle className="mr-1.5 animate-spin p-0.5" />{" "}
Processing...
Expand All @@ -208,9 +249,9 @@ export function SignInForm() {
{wholeFormError && (
<>
<br />
<span className="text-sm font-medium text-destructive">
<div className="text-sm font-medium text-destructive">
{wholeFormError}
</span>
</div>
</>
)}
</form>
Expand Down

0 comments on commit e581ead

Please sign in to comment.