diff --git a/apps/X/app/api/post/route.ts b/apps/X/app/api/post/route.ts new file mode 100644 index 0000000..6549f4f --- /dev/null +++ b/apps/X/app/api/post/route.ts @@ -0,0 +1,58 @@ +import { PrismaClient } from "@prisma/client"; +import { authOptions } from "app/lib/auth"; +import { getServerSession } from "next-auth"; +//? https://github.com/code100x/cms/blob/main/src/app/api/admin/content/route.ts +import { NextRequest, NextResponse } from "next/server"; + +const prisma = new PrismaClient(); + +export async function GET() { + const session = await getServerSession(authOptions); + + if (!session) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + + return NextResponse.json({ userId: session.user.id }); +} + +export const POST = async (req: NextRequest) => { + try { + const session = await getServerSession(authOptions); + console.log("Reaching in Post"); + console.log(session, "This is the user"); + console.log(session?.user.id, "This is the userID"); + console.log(session?.user, "This is the userID"); + + if (!session?.user?.id) { + return NextResponse.json( + { error: "Unauthorized - User not authenticated" }, + { status: 401 } + ); + } else { + console.log("reaching"); + } + + const body = await req.json(); + console.log("Request Body:", body); + + if (!body || !body.content) { + return NextResponse.json( + { error: "content is requried" }, + { status: 400 } + ); + } + const userId = parseInt(session.user.id); + if (isNaN(userId)) { + return NextResponse.json({ error: "Invalid user ID" }, { status: 400 }); + } + const { content } = body; + const tweet = await prisma.tweet.create({ + data: { content, userID: userId }, + }); + console.log("This is the response", tweet); + return NextResponse.json(tweet, { status: 201 }); + } catch (error) { + console.log("Getting error in Creating", error); + } +}; diff --git a/apps/X/app/lib/auth.ts b/apps/X/app/lib/auth.ts index 9cfc0e8..017188a 100644 --- a/apps/X/app/lib/auth.ts +++ b/apps/X/app/lib/auth.ts @@ -1,12 +1,22 @@ import db from "@repo/db/client"; import bcrypt from "bcrypt"; -import { JWT } from "next-auth/jwt"; import CredentialsProvider from "next-auth/providers/credentials"; import GithubProvider from "next-auth/providers/github"; import GoogleProvider from "next-auth/providers/google"; -import { signIn, signOut } from "next-auth/react"; +import { Session } from "next-auth"; import z from "zod"; +declare module "next-auth" { + interface Session { + user: { + id: string; + name?: string | null; + email?: string | null; + image?: string | null; + }; + } +} + interface credentialsTypes { username: string; password: string; @@ -97,7 +107,7 @@ export const authOptions = { console.log("This is userEmail", existingUser.email); console.log("This is username", existingUser.username); return { - id: existingUser?.id.toString(), + id: existingUser.id.toString(), usernname: existingUser.username, email: existingUser.email, name: existingUser.name, @@ -127,6 +137,7 @@ export const authOptions = { id: user.id.toString(), name: user.name, username: user.username, + email: user.email, }; } catch (error) { console.log(error, "Not able to Create new user"); @@ -142,6 +153,8 @@ export const authOptions = { async jwt({ token, user, account }: any) { console.log("JWT Callback - User:", user); console.log("JWT Callback - Account:", account); + console.log("OOOOOOOOOOO", token.id); + if (user) { token.id = user.id; token.username = user.username; @@ -150,15 +163,16 @@ export const authOptions = { return token; }, async session({ session, token }: any) { - // const user = await db.user.findUnique({ - // where: { id: token.sub }, - // }); + console.log("Session Callback - Token:", token); + console.log("Session Callback - Initial Session:", session); if (token && session.user) { - session.user.id = token.id || null; + session.user.id = token.id as string; session.user.username = token.username || null; session.user.email = token.email || null; } + + console.log("Session Callback - Updated Session:", session); return session; }, @@ -171,9 +185,8 @@ export const authOptions = { }); if (!existingUser) { - await db.user.create({ + const newUser = await db.user.create({ data: { - // Make sure this matches your DB schema username: user.username || (account.provider === "github" @@ -185,13 +198,14 @@ export const authOptions = { // image: user.image, }, }); - console.log("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); - - console.log({ - id: user.id.toString(), - name: user.name, - username: user.username, - }); + user.id = newUser.id.toString(); + // console.log({ + // id: user.id.toString(), + // name: user.name, + // username: user.username, + // }); + } else { + user.id = existingUser.id.toString(); } return true; } catch (error) { diff --git a/apps/X/app/page.tsx b/apps/X/app/page.tsx index 826b48a..35d8c22 100644 --- a/apps/X/app/page.tsx +++ b/apps/X/app/page.tsx @@ -3,10 +3,10 @@ import { authOptions } from "./lib/auth"; import { redirect } from "next/navigation"; const Page = async () => { - console.log(">>>>>>>>>>>>>>>"); try { const session = await getServerSession(authOptions); console.log("Full session object:", JSON.stringify(session, null, 2)); + console.log("UseriD test>>>>>>>>>>>>>>", session?.user?.id); if (!session?.user?.id) { console.log("No valid session, redirecting to signin"); diff --git a/apps/X/package.json b/apps/X/package.json index d0860f3..4ba256b 100644 --- a/apps/X/package.json +++ b/apps/X/package.json @@ -15,6 +15,7 @@ "@radix-ui/react-dialog": "^1.1.4", "@radix-ui/react-slot": "^1.1.1", "@repo/ui": "*", + "axios": "^1.7.9", "bcrypt": "^5.1.1", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", diff --git a/apps/X/src/components/ui/Post/TopPost.tsx b/apps/X/src/components/ui/Post/TopPost.tsx index 7125d0e..3a086a6 100644 --- a/apps/X/src/components/ui/Post/TopPost.tsx +++ b/apps/X/src/components/ui/Post/TopPost.tsx @@ -1,17 +1,44 @@ "use client"; +import axios from "axios"; +import { useSession } from "next-auth/react"; import { useState } from "react"; +import { AiOutlineItalic } from "react-icons/ai"; +import { BsTypeBold } from "react-icons/bs"; +import { GrEmoji } from "react-icons/gr"; +import { HiMiniListBullet } from "react-icons/hi2"; +import { IoLocationOutline } from "react-icons/io5"; +import { MdOutlineGifBox } from "react-icons/md"; +import { RiCalendarScheduleLine } from "react-icons/ri"; +import { TbPhotoSquareRounded } from "react-icons/tb"; import { Button } from "../button"; import { Input } from "../input"; import { UserAvatar } from "../usrAvatar"; +import X_Icon from "../X_Icon"; +interface TweetInput { + content: string; + userId: number; +} export const TopPost = () => { const [postInput, setPostInput] = useState(""); + const { data: session } = useSession(); const handelChanges = (e: React.ChangeEvent) => { setPostInput(e.target.value); }; - const handelClick = () => { + const handelClick = async () => { console.log(postInput); + if (!session?.user?.email) { + console.error("No User session found"); + return; + } + try { + const res = await axios.post("/api/post", { content: postInput }); + console.log("Post saved successfully:", res.data); + setPostInput(""); + } catch (error) { + console.log("Errro while Posting", error); + } }; return (
@@ -30,7 +57,43 @@ export const TopPost = () => { />
- +
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
); diff --git a/yarn.lock b/yarn.lock index c205e42..5893cc1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1155,6 +1155,11 @@ ast-types@^0.13.4: dependencies: tslib "^2.0.1" +asynckit@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" + integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== + autoprefixer@^10.4.20: version "10.4.20" resolved "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.20.tgz" @@ -1174,6 +1179,15 @@ available-typed-arrays@^1.0.7: dependencies: possible-typed-array-names "^1.0.0" +axios@^1.7.9: + version "1.7.9" + resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.9.tgz#d7d071380c132a24accda1b2cfc1535b79ec650a" + integrity sha512-LhLcE7Hbiryz8oMDdDptSrWowmB4Bl6RCt6sIJKpRB4XtVf0iEgewX3au/pJqm+Py1kCASkb/FFKjxQaLtxJvw== + dependencies: + follow-redirects "^1.15.6" + form-data "^4.0.0" + proxy-from-env "^1.1.0" + balanced-match@^1.0.0: version "1.0.2" resolved "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz" @@ -1470,6 +1484,13 @@ color@^4.2.3: color-convert "^2.0.1" color-string "^1.9.0" +combined-stream@^1.0.8: + version "1.0.8" + resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" + integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg== + dependencies: + delayed-stream "~1.0.0" + commander@^10.0.0: version "10.0.1" resolved "https://registry.npmjs.org/commander/-/commander-10.0.1.tgz" @@ -1629,6 +1650,11 @@ del@^5.1.0: rimraf "^3.0.0" slash "^3.0.0" +delayed-stream@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" + integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ== + delegates@^1.0.0: version "1.0.0" resolved "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz" @@ -2139,6 +2165,11 @@ flatted@^3.2.9: resolved "https://registry.npmjs.org/flatted/-/flatted-3.3.2.tgz" integrity sha512-AiwGJM8YcNOaobumgtng+6NHuOqC3A7MixFeDafM3X9cIUM+xUXoS5Vfgf+OihAYe20fxqNM9yPBXJzRtZ/4eA== +follow-redirects@^1.15.6: + version "1.15.9" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.9.tgz#a604fa10e443bf98ca94228d9eebcc2e8a2c8ee1" + integrity sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ== + for-each@^0.3.3: version "0.3.3" resolved "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz" @@ -2154,6 +2185,15 @@ foreground-child@^3.1.0: cross-spawn "^7.0.0" signal-exit "^4.0.1" +form-data@^4.0.0: + version "4.0.1" + resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.1.tgz#ba1076daaaa5bfd7e99c1a6cb02aa0a5cff90d48" + integrity sha512-tzN8e4TX8+kkxGPK8D5u0FNmjPUjw3lwC9lSLxxoB/+GtsJG91CO8bSWy73APlgAZzZbXEYZJuxjkHH2w+Ezhw== + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.8" + mime-types "^2.1.12" + fraction.js@^4.3.7: version "4.3.7" resolved "https://registry.npmjs.org/fraction.js/-/fraction.js-4.3.7.tgz" @@ -3043,6 +3083,18 @@ micromatch@^4.0.4, micromatch@^4.0.8: braces "^3.0.3" picomatch "^2.3.1" +mime-db@1.52.0: + version "1.52.0" + resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70" + integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg== + +mime-types@^2.1.12: + version "2.1.35" + resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a" + integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw== + dependencies: + mime-db "1.52.0" + mimic-fn@^2.1.0: version "2.1.0" resolved "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz"