Skip to content

Added check to redirect if already signed-in #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions src/app/(auth)/sign-in/email/login-form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
"use client";

import { z } from "zod";

import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { pageTitleStyles } from "@/styles/common";
import { cn } from "@/lib/utils";
import { useServerAction } from "zsa-react";
import Link from "next/link";
import { useToast } from "@/components/ui/use-toast";
import { signInAction } from "./actions";
import { LoaderButton } from "@/components/loader-button";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Terminal } from "lucide-react";

const registrationSchema = z.object({
email: z.string().email(),
password: z.string().min(8),
});

export default function LoginForm() {
const { toast } = useToast();

const { execute, isPending, error, reset } = useServerAction(signInAction, {
onError({ err }) {
toast({
title: "Something went wrong",
description: err.message,
variant: "destructive",
});
},
onSuccess() {
toast({
title: "Let's Go!",
description: "Enjoy your session",
});
},
});

const form = useForm<z.infer<typeof registrationSchema>>({
resolver: zodResolver(registrationSchema),
defaultValues: {
email: "",
password: "",
},
});

function onSubmit(values: z.infer<typeof registrationSchema>) {
execute(values);
}

return (
<div className="mx-auto max-w-[400px] space-y-6 py-24">
<h1 className={cn(pageTitleStyles, "text-center")}>Sign In</h1>

<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input
{...field}
className="w-full"
placeholder="Enter your email"
type="email"
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>

<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel>Password</FormLabel>
<FormControl>
<Input
{...field}
className="w-full"
placeholder="Enter your password"
type="password"
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>

{error && (
<Alert variant="destructive">
<Terminal className="h-4 w-4" />
<AlertTitle>Uhoh, we couldn&apos;t log you in</AlertTitle>
<AlertDescription>{error.message}</AlertDescription>
</Alert>
)}

<LoaderButton isLoading={isPending} className="w-full" type="submit">
Sign In
</LoaderButton>
</form>
</Form>

<div className="flex justify-center">
<Button asChild variant="link">
<Link href="/sign-in/forgot-password">Forgot Password</Link>
</Button>
</div>

<div className="relative py-4">
<div className="absolute inset-0 flex items-center">
<span className="w-full border-t" />
</div>
<div className="relative flex justify-center text-xs uppercase">
<span className="bg-gray-100 px-2 text-gray-500 dark:bg-gray-950 dark:text-gray-400">
Or
</span>
</div>
</div>

<Button className="w-full" variant={"secondary"}>
<Link href="/sign-up">Create an account</Link>
</Button>
</div>
);
}
147 changes: 7 additions & 140 deletions src/app/(auth)/sign-in/email/page.tsx
Original file line number Diff line number Diff line change
@@ -1,143 +1,10 @@
"use client";
import { getCurrentUser } from "@/lib/session";
import { redirect } from "next/navigation";
import LoginForm from "./login-form";

import { z } from "zod";
export default async function SignInPage() {
const user = await getCurrentUser();
if (user) redirect("/dashboard");

import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { pageTitleStyles } from "@/styles/common";
import { cn } from "@/lib/utils";
import { useServerAction } from "zsa-react";
import Link from "next/link";
import { useToast } from "@/components/ui/use-toast";
import { signInAction } from "./actions";
import { LoaderButton } from "@/components/loader-button";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Terminal } from "lucide-react";

const registrationSchema = z.object({
email: z.string().email(),
password: z.string().min(8),
});

export default function SignInPage() {
const { toast } = useToast();

const { execute, isPending, error, reset } = useServerAction(signInAction, {
onError({ err }) {
toast({
title: "Something went wrong",
description: err.message,
variant: "destructive",
});
},
onSuccess() {
toast({
title: "Let's Go!",
description: "Enjoy your session",
});
},
});

const form = useForm<z.infer<typeof registrationSchema>>({
resolver: zodResolver(registrationSchema),
defaultValues: {
email: "",
password: "",
},
});

function onSubmit(values: z.infer<typeof registrationSchema>) {
execute(values);
}

return (
<div className="mx-auto max-w-[400px] space-y-6 py-24">
<h1 className={cn(pageTitleStyles, "text-center")}>Sign In</h1>

<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input
{...field}
className="w-full"
placeholder="Enter your email"
type="email"
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>

<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel>Password</FormLabel>
<FormControl>
<Input
{...field}
className="w-full"
placeholder="Enter your password"
type="password"
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>

{error && (
<Alert variant="destructive">
<Terminal className="h-4 w-4" />
<AlertTitle>Uhoh, we couldn&apos;t log you in</AlertTitle>
<AlertDescription>{error.message}</AlertDescription>
</Alert>
)}

<LoaderButton isLoading={isPending} className="w-full" type="submit">
Sign In
</LoaderButton>
</form>
</Form>

<div className="flex justify-center">
<Button asChild variant="link">
<Link href="/sign-in/forgot-password">Forgot Password</Link>
</Button>
</div>

<div className="relative py-4">
<div className="absolute inset-0 flex items-center">
<span className="w-full border-t" />
</div>
<div className="relative flex justify-center text-xs uppercase">
<span className="bg-gray-100 px-2 text-gray-500 dark:bg-gray-950 dark:text-gray-400">
Or
</span>
</div>
</div>

<Button className="w-full" variant={"secondary"}>
<Link href="/sign-up">Create an account</Link>
</Button>
</div>
);
return <LoginForm />;
}