Skip to content

Commit c42b747

Browse files
authored
Merge pull request #23 from mscode07/dev
Done with POST
2 parents b3e0fd2 + b6368a2 commit c42b747

File tree

6 files changed

+207
-19
lines changed

6 files changed

+207
-19
lines changed

apps/X/app/api/post/route.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { PrismaClient } from "@prisma/client";
2+
import { authOptions } from "app/lib/auth";
3+
import { getServerSession } from "next-auth";
4+
//? https://github.com/code100x/cms/blob/main/src/app/api/admin/content/route.ts
5+
import { NextRequest, NextResponse } from "next/server";
6+
7+
const prisma = new PrismaClient();
8+
9+
export async function GET() {
10+
const session = await getServerSession(authOptions);
11+
12+
if (!session) {
13+
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
14+
}
15+
16+
return NextResponse.json({ userId: session.user.id });
17+
}
18+
19+
export const POST = async (req: NextRequest) => {
20+
try {
21+
const session = await getServerSession(authOptions);
22+
console.log("Reaching in Post");
23+
console.log(session, "This is the user");
24+
console.log(session?.user.id, "This is the userID");
25+
console.log(session?.user, "This is the userID");
26+
27+
if (!session?.user?.id) {
28+
return NextResponse.json(
29+
{ error: "Unauthorized - User not authenticated" },
30+
{ status: 401 }
31+
);
32+
} else {
33+
console.log("reaching");
34+
}
35+
36+
const body = await req.json();
37+
console.log("Request Body:", body);
38+
39+
if (!body || !body.content) {
40+
return NextResponse.json(
41+
{ error: "content is requried" },
42+
{ status: 400 }
43+
);
44+
}
45+
const userId = parseInt(session.user.id);
46+
if (isNaN(userId)) {
47+
return NextResponse.json({ error: "Invalid user ID" }, { status: 400 });
48+
}
49+
const { content } = body;
50+
const tweet = await prisma.tweet.create({
51+
data: { content, userID: userId },
52+
});
53+
console.log("This is the response", tweet);
54+
return NextResponse.json(tweet, { status: 201 });
55+
} catch (error) {
56+
console.log("Getting error in Creating", error);
57+
}
58+
};

apps/X/app/lib/auth.ts

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import db from "@repo/db/client";
22
import bcrypt from "bcrypt";
3-
import { JWT } from "next-auth/jwt";
43
import CredentialsProvider from "next-auth/providers/credentials";
54
import GithubProvider from "next-auth/providers/github";
65
import GoogleProvider from "next-auth/providers/google";
7-
import { signIn, signOut } from "next-auth/react";
6+
import { Session } from "next-auth";
87
import z from "zod";
98

9+
declare module "next-auth" {
10+
interface Session {
11+
user: {
12+
id: string;
13+
name?: string | null;
14+
email?: string | null;
15+
image?: string | null;
16+
};
17+
}
18+
}
19+
1020
interface credentialsTypes {
1121
username: string;
1222
password: string;
@@ -97,7 +107,7 @@ export const authOptions = {
97107
console.log("This is userEmail", existingUser.email);
98108
console.log("This is username", existingUser.username);
99109
return {
100-
id: existingUser?.id.toString(),
110+
id: existingUser.id.toString(),
101111
usernname: existingUser.username,
102112
email: existingUser.email,
103113
name: existingUser.name,
@@ -127,6 +137,7 @@ export const authOptions = {
127137
id: user.id.toString(),
128138
name: user.name,
129139
username: user.username,
140+
email: user.email,
130141
};
131142
} catch (error) {
132143
console.log(error, "Not able to Create new user");
@@ -142,6 +153,8 @@ export const authOptions = {
142153
async jwt({ token, user, account }: any) {
143154
console.log("JWT Callback - User:", user);
144155
console.log("JWT Callback - Account:", account);
156+
console.log("OOOOOOOOOOO", token.id);
157+
145158
if (user) {
146159
token.id = user.id;
147160
token.username = user.username;
@@ -150,15 +163,16 @@ export const authOptions = {
150163
return token;
151164
},
152165
async session({ session, token }: any) {
153-
// const user = await db.user.findUnique({
154-
// where: { id: token.sub },
155-
// });
166+
console.log("Session Callback - Token:", token);
167+
console.log("Session Callback - Initial Session:", session);
156168

157169
if (token && session.user) {
158-
session.user.id = token.id || null;
170+
session.user.id = token.id as string;
159171
session.user.username = token.username || null;
160172
session.user.email = token.email || null;
161173
}
174+
175+
console.log("Session Callback - Updated Session:", session);
162176
return session;
163177
},
164178

@@ -171,9 +185,8 @@ export const authOptions = {
171185
});
172186

173187
if (!existingUser) {
174-
await db.user.create({
188+
const newUser = await db.user.create({
175189
data: {
176-
// Make sure this matches your DB schema
177190
username:
178191
user.username ||
179192
(account.provider === "github"
@@ -185,13 +198,14 @@ export const authOptions = {
185198
// image: user.image,
186199
},
187200
});
188-
console.log("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
189-
190-
console.log({
191-
id: user.id.toString(),
192-
name: user.name,
193-
username: user.username,
194-
});
201+
user.id = newUser.id.toString();
202+
// console.log({
203+
// id: user.id.toString(),
204+
// name: user.name,
205+
// username: user.username,
206+
// });
207+
} else {
208+
user.id = existingUser.id.toString();
195209
}
196210
return true;
197211
} catch (error) {

apps/X/app/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { authOptions } from "./lib/auth";
33
import { redirect } from "next/navigation";
44

55
const Page = async () => {
6-
console.log(">>>>>>>>>>>>>>>");
76
try {
87
const session = await getServerSession(authOptions);
98
console.log("Full session object:", JSON.stringify(session, null, 2));
9+
console.log("UseriD test>>>>>>>>>>>>>>", session?.user?.id);
1010

1111
if (!session?.user?.id) {
1212
console.log("No valid session, redirecting to signin");

apps/X/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@radix-ui/react-dialog": "^1.1.4",
1616
"@radix-ui/react-slot": "^1.1.1",
1717
"@repo/ui": "*",
18+
"axios": "^1.7.9",
1819
"bcrypt": "^5.1.1",
1920
"class-variance-authority": "^0.7.1",
2021
"clsx": "^2.1.1",

apps/X/src/components/ui/Post/TopPost.tsx

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
11
"use client";
2+
import axios from "axios";
3+
import { useSession } from "next-auth/react";
24
import { useState } from "react";
5+
import { AiOutlineItalic } from "react-icons/ai";
6+
import { BsTypeBold } from "react-icons/bs";
7+
import { GrEmoji } from "react-icons/gr";
8+
import { HiMiniListBullet } from "react-icons/hi2";
9+
import { IoLocationOutline } from "react-icons/io5";
10+
import { MdOutlineGifBox } from "react-icons/md";
11+
import { RiCalendarScheduleLine } from "react-icons/ri";
12+
import { TbPhotoSquareRounded } from "react-icons/tb";
313
import { Button } from "../button";
414
import { Input } from "../input";
515
import { UserAvatar } from "../usrAvatar";
16+
import X_Icon from "../X_Icon";
17+
interface TweetInput {
18+
content: string;
19+
userId: number;
20+
}
621

722
export const TopPost = () => {
823
const [postInput, setPostInput] = useState("");
24+
const { data: session } = useSession();
925

1026
const handelChanges = (e: React.ChangeEvent<HTMLInputElement>) => {
1127
setPostInput(e.target.value);
1228
};
13-
const handelClick = () => {
29+
const handelClick = async () => {
1430
console.log(postInput);
31+
if (!session?.user?.email) {
32+
console.error("No User session found");
33+
return;
34+
}
35+
try {
36+
const res = await axios.post("/api/post", { content: postInput });
37+
console.log("Post saved successfully:", res.data);
38+
setPostInput("");
39+
} catch (error) {
40+
console.log("Errro while Posting", error);
41+
}
1542
};
1643
return (
1744
<div>
@@ -30,7 +57,43 @@ export const TopPost = () => {
3057
/>
3158
</div>
3259
</div>
33-
<Button onClick={handelClick}>Post</Button>
60+
<div className="flex items-center ml-12">
61+
<div className="flex gap-4 text-blue-500 text-lg cursor-pointer">
62+
<div>
63+
<TbPhotoSquareRounded />
64+
</div>
65+
<div>
66+
<MdOutlineGifBox />
67+
</div>
68+
<div>
69+
<X_Icon />
70+
</div>
71+
<div>
72+
<HiMiniListBullet />
73+
</div>
74+
<div>
75+
<GrEmoji />
76+
</div>
77+
<div>
78+
<RiCalendarScheduleLine />
79+
</div>
80+
<div>
81+
<IoLocationOutline />
82+
</div>
83+
<div>
84+
<BsTypeBold />
85+
</div>
86+
<div>
87+
<AiOutlineItalic />
88+
</div>
89+
</div>
90+
<Button
91+
onClick={handelClick}
92+
className="ml-28 rounded-2xl font-semibold"
93+
>
94+
Post
95+
</Button>
96+
</div>
3497
</div>
3598
</div>
3699
);

yarn.lock

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,11 @@ ast-types@^0.13.4:
11551155
dependencies:
11561156
tslib "^2.0.1"
11571157

1158+
asynckit@^0.4.0:
1159+
version "0.4.0"
1160+
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
1161+
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==
1162+
11581163
autoprefixer@^10.4.20:
11591164
version "10.4.20"
11601165
resolved "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.20.tgz"
@@ -1174,6 +1179,15 @@ available-typed-arrays@^1.0.7:
11741179
dependencies:
11751180
possible-typed-array-names "^1.0.0"
11761181

1182+
axios@^1.7.9:
1183+
version "1.7.9"
1184+
resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.9.tgz#d7d071380c132a24accda1b2cfc1535b79ec650a"
1185+
integrity sha512-LhLcE7Hbiryz8oMDdDptSrWowmB4Bl6RCt6sIJKpRB4XtVf0iEgewX3au/pJqm+Py1kCASkb/FFKjxQaLtxJvw==
1186+
dependencies:
1187+
follow-redirects "^1.15.6"
1188+
form-data "^4.0.0"
1189+
proxy-from-env "^1.1.0"
1190+
11771191
balanced-match@^1.0.0:
11781192
version "1.0.2"
11791193
resolved "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz"
@@ -1470,6 +1484,13 @@ color@^4.2.3:
14701484
color-convert "^2.0.1"
14711485
color-string "^1.9.0"
14721486

1487+
combined-stream@^1.0.8:
1488+
version "1.0.8"
1489+
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
1490+
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
1491+
dependencies:
1492+
delayed-stream "~1.0.0"
1493+
14731494
commander@^10.0.0:
14741495
version "10.0.1"
14751496
resolved "https://registry.npmjs.org/commander/-/commander-10.0.1.tgz"
@@ -1629,6 +1650,11 @@ del@^5.1.0:
16291650
rimraf "^3.0.0"
16301651
slash "^3.0.0"
16311652

1653+
delayed-stream@~1.0.0:
1654+
version "1.0.0"
1655+
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
1656+
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==
1657+
16321658
delegates@^1.0.0:
16331659
version "1.0.0"
16341660
resolved "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz"
@@ -2139,6 +2165,11 @@ flatted@^3.2.9:
21392165
resolved "https://registry.npmjs.org/flatted/-/flatted-3.3.2.tgz"
21402166
integrity sha512-AiwGJM8YcNOaobumgtng+6NHuOqC3A7MixFeDafM3X9cIUM+xUXoS5Vfgf+OihAYe20fxqNM9yPBXJzRtZ/4eA==
21412167

2168+
follow-redirects@^1.15.6:
2169+
version "1.15.9"
2170+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.9.tgz#a604fa10e443bf98ca94228d9eebcc2e8a2c8ee1"
2171+
integrity sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==
2172+
21422173
for-each@^0.3.3:
21432174
version "0.3.3"
21442175
resolved "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz"
@@ -2154,6 +2185,15 @@ foreground-child@^3.1.0:
21542185
cross-spawn "^7.0.0"
21552186
signal-exit "^4.0.1"
21562187

2188+
form-data@^4.0.0:
2189+
version "4.0.1"
2190+
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.1.tgz#ba1076daaaa5bfd7e99c1a6cb02aa0a5cff90d48"
2191+
integrity sha512-tzN8e4TX8+kkxGPK8D5u0FNmjPUjw3lwC9lSLxxoB/+GtsJG91CO8bSWy73APlgAZzZbXEYZJuxjkHH2w+Ezhw==
2192+
dependencies:
2193+
asynckit "^0.4.0"
2194+
combined-stream "^1.0.8"
2195+
mime-types "^2.1.12"
2196+
21572197
fraction.js@^4.3.7:
21582198
version "4.3.7"
21592199
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:
30433083
braces "^3.0.3"
30443084
picomatch "^2.3.1"
30453085

3086+
3087+
version "1.52.0"
3088+
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70"
3089+
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==
3090+
3091+
mime-types@^2.1.12:
3092+
version "2.1.35"
3093+
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a"
3094+
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==
3095+
dependencies:
3096+
mime-db "1.52.0"
3097+
30463098
mimic-fn@^2.1.0:
30473099
version "2.1.0"
30483100
resolved "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz"

0 commit comments

Comments
 (0)