diff --git a/src/actions/auth.ts b/src/actions/auth.ts index 4db317c..4f9c9c6 100644 --- a/src/actions/auth.ts +++ b/src/actions/auth.ts @@ -9,7 +9,7 @@ import { CredentialsSignUpFormValues } from "@/components/credentials-sign-up-fo const saltRounds = 10; -export async function signInWithCredentials(formData: CredentialsSignInFormValues) { +export async function signInWithCredentials(formData: CredentialsSignInFormValues, redirectTo?: string) { try { // Parse credentials using authSchema for validation const { email, password } = await authSchema.parseAsync(formData); @@ -33,7 +33,7 @@ export async function signInWithCredentials(formData: CredentialsSignInFormValue throw new Error("Incorrect password."); } - await signIn("credentials", { ...formData, redirect: false }); + await signIn("credentials", { ...formData, redirectTo, redirect: !!redirectTo }); return { success: true }; } catch (error) { return { error: error instanceof Error ? error.message : "Failed to sign in. Please try again." }; @@ -67,3 +67,7 @@ export async function signUpWithCredentials(formData: CredentialsSignUpFormValue return { error: error instanceof Error ? error.message : "Registration failed. Please try again." }; } } + +export async function signInWithGithub(redirectTo?: string) { + await signIn("github", { redirectTo, redirect: !!redirectTo }); +} diff --git a/src/app/(auth)/layout.tsx b/src/app/(auth)/layout.tsx index 9ce6ca7..f62b2a5 100644 --- a/src/app/(auth)/layout.tsx +++ b/src/app/(auth)/layout.tsx @@ -1,8 +1,6 @@ import Link from "next/link"; import Image from "next/image"; -import { auth } from "@/lib/auth"; -import { Code } from "lucide-react"; -import { redirect } from "next/navigation"; +import { CodeIcon } from "lucide-react"; interface AuthLayoutProps { children: React.ReactNode; @@ -11,16 +9,13 @@ interface AuthLayoutProps { export default async function AuthLayout({ children }: AuthLayoutProps) { - const session = await auth(); - if (session) redirect("/"); - return (
- +
Judge4c diff --git a/src/components/avatar-button.tsx b/src/components/avatar-button.tsx index eaf0aec..51bd2c7 100644 --- a/src/components/avatar-button.tsx +++ b/src/components/avatar-button.tsx @@ -1,11 +1,4 @@ -import { - LogIn, - LogOut, -} from "lucide-react"; -import { - Avatar, - AvatarImage, -} from "@/components/ui/avatar"; +import { LogOut } from "lucide-react"; import { DropdownMenu, DropdownMenuContent, @@ -16,8 +9,9 @@ import { DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { auth, signOut } from "@/lib/auth"; -import { redirect } from "next/navigation"; import { Skeleton } from "@/components/ui/skeleton"; +import LogInButton from "@/components/log-in-button"; +import { Avatar, AvatarImage } from "@/components/ui/avatar"; import { SettingsButton } from "@/components/settings-button"; const UserAvatar = ({ image, name }: { image: string; name: string }) => ( @@ -27,12 +21,7 @@ const UserAvatar = ({ image, name }: { image: string; name: string }) => ( ); -async function handleSignIn() { - "use server"; - redirect("/sign-in"); -} - -async function handleSignOut() { +async function handleLogOut() { "use server"; await signOut(); } @@ -57,10 +46,7 @@ export async function AvatarButton() { {!isLoggedIn ? ( - - - Log In - + ) : ( <> @@ -76,7 +62,7 @@ export async function AvatarButton() { - + Log out diff --git a/src/components/credentials-sign-in-form.tsx b/src/components/credentials-sign-in-form.tsx index 47e6da0..4984e0b 100644 --- a/src/components/credentials-sign-in-form.tsx +++ b/src/components/credentials-sign-in-form.tsx @@ -12,18 +12,20 @@ import { import { toast } from "sonner"; import { authSchema } from "@/lib/zod"; import { useForm } from "react-hook-form"; -import { useRouter } from "next/navigation"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { useState, useTransition } from "react"; import { zodResolver } from "@hookform/resolvers/zod"; import { signInWithCredentials } from "@/actions/auth"; import { EyeIcon, EyeOffIcon, MailIcon } from "lucide-react"; +import { useRouter, useSearchParams } from "next/navigation"; export type CredentialsSignInFormValues = z.infer; export function CredentialsSignInForm() { const router = useRouter(); + const searchParams = useSearchParams(); + const redirectTo = searchParams.get("redirectTo"); const [isPending, startTransition] = useTransition(); const [isVisible, setIsVisible] = useState(false); @@ -47,7 +49,7 @@ export function CredentialsSignInForm() { }); } else { toast.success("Signed In Successfully"); - router.push("/"); + router.push(redirectTo || "/"); } }); }; diff --git a/src/components/credentials-sign-up-form.tsx b/src/components/credentials-sign-up-form.tsx index 32b7e29..3d7b34f 100644 --- a/src/components/credentials-sign-up-form.tsx +++ b/src/components/credentials-sign-up-form.tsx @@ -12,18 +12,20 @@ import { import { toast } from "sonner"; import { authSchema } from "@/lib/zod"; import { useForm } from "react-hook-form"; -import { useRouter } from "next/navigation"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { useState, useTransition } from "react"; import { zodResolver } from "@hookform/resolvers/zod"; import { signUpWithCredentials } from "@/actions/auth"; import { EyeIcon, EyeOffIcon, MailIcon } from "lucide-react"; +import { useRouter, useSearchParams } from "next/navigation"; export type CredentialsSignUpFormValues = z.infer; export function CredentialsSignUpForm() { const router = useRouter(); + const searchParams = useSearchParams(); + const redirectTo = searchParams.get("redirectTo"); const [isPending, startTransition] = useTransition(); const [isVisible, setIsVisible] = useState(false); @@ -48,11 +50,8 @@ export function CredentialsSignUpForm() { } else { toast.success("Account Created", { description: "You can now sign in with your credentials", - action: { - label: "Go to Sign In", - onClick: () => router.push("/sign-in"), - }, }); + router.push(`/sign-in?${redirectTo}`) } }); }; diff --git a/src/components/github-sign-in-form.tsx b/src/components/github-sign-in-form.tsx index 6f8ca0e..984a855 100644 --- a/src/components/github-sign-in-form.tsx +++ b/src/components/github-sign-in-form.tsx @@ -1,15 +1,17 @@ -import { signIn } from "@/lib/auth"; +"use client"; + import { Button } from "@/components/ui/button"; +import { signInWithGithub } from "@/actions/auth"; +import { useSearchParams } from "next/navigation"; export function GithubSignInForm() { + const searchParams = useSearchParams(); + const redirectTo = searchParams.get("redirectTo"); + const signInAction = signInWithGithub.bind(null, redirectTo || "/"); + return ( -
{ - "use server"; - await signIn("github"); - }} - > -
); diff --git a/src/components/sign-up-form.tsx b/src/components/sign-up-form.tsx index 265e3a7..ac44a2f 100644 --- a/src/components/sign-up-form.tsx +++ b/src/components/sign-up-form.tsx @@ -1,7 +1,18 @@ +"use client"; + +import { useRouter, useSearchParams } from "next/navigation"; import { GithubSignInForm } from "@/components/github-sign-in-form"; import { CredentialsSignUpForm } from "@/components/credentials-sign-up-form"; export function SignUpForm() { + const router = useRouter(); + const searchParams = useSearchParams(); + + const handleSignIn = () => { + const params = new URLSearchParams(searchParams.toString()); + router.push(`/sign-in?${params.toString()}`); + }; + return (
@@ -19,9 +30,12 @@ export function SignUpForm() {
Already have an account?{" "} - +
);