Skip to content
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
8 changes: 7 additions & 1 deletion task2/src/app/auth/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Suspense } from "react";

export default function AuthLayout({
children,
}: {
children: React.ReactNode;
}) {
return <div>{children}</div>;
return (
<div>
<Suspense>{children}</Suspense>
</div>
);
}
27 changes: 11 additions & 16 deletions task2/src/app/auth/signin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,29 @@

import type React from "react";

import { useEffect, useState } from "react";
import { signIn } from "next-auth/react";
import { useRouter } from "next/navigation";
import { motion } from "framer-motion";
import { Mail, Lock, Loader2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { useSession } from "next-auth/react";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { toast } from "@/components/ui/use-toast";
import { motion } from "framer-motion";
import { Loader2, Lock, Mail } from "lucide-react";
import { signIn } from "next-auth/react";
import { useRouter, useSearchParams } from "next/navigation";
import { useState } from "react";

export default function SignIn() {
const searchParams = useSearchParams();
const callbackUrl = searchParams.get("callbackUrl") ?? "/blog";
const router = useRouter();
const [error, setError] = useState("");
const [isLoading, setIsLoading] = useState(false);
const { data: session } = useSession();

useEffect(() => {
if (session) {
router.push("/blog");
}
}, [session, router]);

const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
Expand All @@ -43,14 +37,15 @@ export default function SignIn() {
email: formData.get("email"),
password: formData.get("password"),
redirect: false,
callbackUrl,
});

if (response?.error) {
console.error("sign in ", response?.error);
setError("Invalid credentials");
setIsLoading(false);
} else {
router.push("/blog");
router.push(callbackUrl);
router.refresh();
}
} catch (error) {
Expand Down
40 changes: 39 additions & 1 deletion task2/src/app/blog/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,45 @@ import { CommentSection } from "@/components/CommentSection";
import { DeleteBlogButton } from "@/components/DeleteBlogButton";
import { supabase } from "@/lib/supabase";
import { Blog } from "@/supbase";
import { capitalize } from "@/utils/string";
import { formatDistanceToNow } from "date-fns";
import { Calendar, Tag, User } from "lucide-react";
import { Metadata } from "next";
import { getServerSession } from "next-auth/next";
import { notFound } from "next/navigation";
import ReactMarkdown from "react-markdown";
import "react-markdown-editor-lite/lib/index.css";

type User = {
id: string;
email: string;
name: string;
};

type Props = {
params: Promise<{ id: string }>;
};

export async function generateMetadata({ params }: Props): Promise<Metadata> {
const { id } = await params;

const { data: blog } = await supabase
.from("blogs")
.select("*")
.eq("id", id)
.single<Blog>();

return {
title: blog?.title,
openGraph: {
description: blog?.excerpt,
publishedTime: `${new Date(`${blog?.created_at}`).getTime()}`,
tags: blog?.category,
writers: blog?.author,
},
};
}

export default async function BlogPost({
params,
}: {
Expand All @@ -23,6 +55,12 @@ export default async function BlogPost({
.single<Blog>();
const session = await getServerSession(authOptions);

const { data: blogAuthor } = await supabase
.from("users")
.select("*")
.eq("id", blog?.author)
.single<User>();

if (!blog) {
notFound();
}
Expand All @@ -38,7 +76,7 @@ export default async function BlogPost({
</h1>
<div className="flex flex-wrap items-center text-muted-foreground mb-2">
<User className="mr-2 h-4 w-4" />
<span className="mr-4">{blog.author}</span>
<span className="mr-4">{capitalize(blogAuthor?.name)}</span>
<Calendar className="mr-2 h-4 w-4" />
<span className="mr-4">
{formatDistanceToNow(blog.created_at)} ago
Expand Down
2 changes: 1 addition & 1 deletion task2/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function RootLayout({
children: React.ReactNode;
}) {
return (
<html lang="en">
<html lang="en" suppressHydrationWarning>
<body className={` antialiased`}>
<ThemeProvider
attribute="class"
Expand Down
2 changes: 1 addition & 1 deletion task2/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function Home() {
</Link>
</Button>
<Button asChild size="lg" variant="outline">
<Link href="/auth/signin">Start Writing</Link>
<Link href="/blog/create">Start Writing</Link>
</Button>
</div>
</motion.div>
Expand Down
4 changes: 4 additions & 0 deletions task2/src/utils/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function capitalize(text: string | undefined) {
if (!text) return "";
return text.charAt(0).toUpperCase() + text.slice(1);
}