Skip to content

Commit

Permalink
new
Browse files Browse the repository at this point in the history
  • Loading branch information
sayandedotcom committed Oct 12, 2024
1 parent fec5c17 commit dc359dc
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 95 deletions.
53 changes: 10 additions & 43 deletions apps/web/app/[locale]/(auth)/auth/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use client";

import React, { useState } from "react";
import React from "react";

import { useRouter, useSearchParams } from "next/navigation";
import { useSearchParams } from "next/navigation";

import { useLoading } from "@/hooks";
import { Link } from "@/navigation";
Expand Down Expand Up @@ -43,20 +43,17 @@ const loginSchema = z.object({
export default function Login() {
const locale = useLocale();
const t = useTranslations("Index");
const router = useRouter();
const { loadingValue, setLoadingValue } = useLoading();
const [isVerificationEmail, setVerificationEmail] = useLocalStorage("verification-email", "");

const [error, setError] = useState("");
const searchParams = useSearchParams();
let errorCallback = searchParams.get("error");
let callbackUrl = searchParams.get("callbackUrl") || "/";
if (callbackUrl.endsWith("sign-up")) {
callbackUrl = "/";
} else {
callbackUrl = callbackUrl || "/";
}
// console.log("errorCallback 😊😊😊😊😊😊😊😊😊😊", errorCallback);
let callbackUrl = searchParams.get("callbackUrl") || `/${locale}/home`;
// if (callbackUrl.endsWith("sign-up")) {
// callbackUrl = "/";
// } else {
// callbackUrl = callbackUrl || "/";
// }

const form = useForm<z.infer<typeof loginSchema>>({
resolver: zodResolver(loginSchema),
defaultValues: {
Expand All @@ -66,7 +63,7 @@ export default function Login() {

const signInProviders = (auth: "google") => {
setLoadingValue(auth);
signIn(auth, { redirect: true, callbackUrl: `/${locale}` });
signIn(auth, { callbackUrl });
};

const onSubmit = async (values: z.infer<typeof loginSchema>) => {
Expand All @@ -78,7 +75,6 @@ export default function Login() {
redirect: true,
callbackUrl: `${locale}/auth/verify-request`,
});
console.log("result", result);
// if (!result?.error) {
// sonerToast({
// severity: "success",
Expand All @@ -99,35 +95,6 @@ export default function Login() {
} finally {
setLoadingValue("");
}
// try {
// setLoadingValue("logIn");
// const result = await signIn("credentials", {
// email: values.email,
// password: values.password,
// redirect: false,
// callbackUrl,
// });
// console.log("result", result);
// if (!result?.error) {
// sonerToast({
// severity: "success",
// title: "Woohhh...",
// message: "You have Sucessfully Logged In !!!",
// });
// router.push(callbackUrl);
// } else {
// sonerToast({
// severity: "error",
// title: "Oopss.....",
// message: "Your email / username or password is incorrect",
// });
// setError("Invalid email or password");
// }
// } catch (e) {
// console.log("signup", e);
// } finally {
// setLoadingValue("");
// }
};
return (
<div
Expand Down
14 changes: 13 additions & 1 deletion apps/web/app/[locale]/client/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"use client";

import { useQuery } from "@tanstack/react-query";
import { useSession } from "next-auth/react";

import { request } from "@/lib/axios";

function Client() {
const { data: session } = useSession();
const { data, error, isLoading } = useQuery({
queryKey: ["test"],
queryFn: () => {
Expand All @@ -21,7 +23,17 @@ function Client() {

console.log("datadatadatadatadata", data, error);

return <div>{data?.data?.Hi}</div>;
if (isLoading) {
return <h1>Loading</h1>;
}

return (
<div>
Client
{data?.data?.message}
<h4>{JSON.stringify(session?.user)}</h4>
</div>
);
}

export default Client;
15 changes: 10 additions & 5 deletions apps/web/app/[locale]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Suspense } from "react";

import { useTranslations } from "next-intl";
import { unstable_setRequestLocale } from "next-intl/server";
import { redirect } from "@/navigation";
import { getServerSession } from "next-auth";
import { getTranslations, unstable_setRequestLocale } from "next-intl/server";
import Starfield from "react-starfield";

import {
Expand All @@ -23,11 +24,15 @@ import {

import Loading from "./loading";

export default function Page({ params: { locale } }) {
export default async function Page({ params: { locale } }) {
const session = await getServerSession();

if (session?.user) {
redirect("/home");
}
unstable_setRequestLocale(locale);

// const { user } = await getAuthSession();
const t = useTranslations("Index");
const t = await getTranslations("Index");

const user = null;

Expand Down
56 changes: 24 additions & 32 deletions apps/web/app/[locale]/server/page.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
// export const dynamic = "force-dynamic";
// export const fetchCache = "force-no-store";
// import { requests } from "@/lib/axios";
import { cookies } from "next/headers";

import { getServerAuthSession } from "@/lib/auth";
import { request } from "@/lib/axios";

async function getTest() {
const response = await request.get("/test");
async function getTest(hasCookie: string) {
const response = await request.get("/test", {
withCredentials: true,

headers: { withCredentials: true, Authorization: `Bearer ${hasCookie}` },
});

return response.data;
}

export default async function Server() {
const response = (await request.get("/test")).data;
const cookieStore = cookies();
const hasCookie = cookieStore.get("next-auth.session-token");
const response = await getTest(hasCookie?.value);
// console.log("😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊datadatadatadatadata", response);

// console.log(
// "😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊hasCookiehasCookiehasCookiehasCookiehasCookiehasCookiehasCookiehasCookie",
// hasCookie.value
// );

const session = await getServerAuthSession();
// const response = await fetch("http://localhost:3000/api/v1/test", {
// method: "GET",
// }).then((ans) => ans.json());
Expand All @@ -21,34 +35,12 @@ export default async function Server() {
// })
// .then((ans) => ans.data);

// console.log("😊datadatadatadatadata", response);
// console.log("😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊datadatadatadatadata", response);
return (
<div>
hi
{response?.Hi}
Server
{response?.message}
<h4>{JSON.stringify(session?.user)}</h4>{" "}
</div>
);
}

// const queryClient = new QueryClient();
// await queryClient.prefetchQuery({
// queryKey: ["test"],
// queryFn: () => {
// return axios.get("http://localhost:3000/api/v1/test", {
// headers: {
// name: "Sayan De from Client Component",
// },
// });
// },
// });
// const { data, error, isLoading } = useQuery({
// queryKey: ["test"],
// queryFn: () => {
// return axios.get("http://localhost:3000/api/v1/test", {
// headers: {
// name: "Sayan De from Client Component",
// },
// });
// },
// });
// console.log("😊datadatadatadatadata",data);
73 changes: 60 additions & 13 deletions apps/web/app/api/v1/test/route.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,71 @@
import { headers } from "next/headers";
import { cookies } from "next/headers";
import { type NextRequest, NextResponse } from "next/server";

export async function GET(request: NextRequest) {
import { getToken } from "next-auth/jwt";
import { getServerSession } from "next-auth/next";

import { authOptions, getServerAuthSession } from "@/lib/auth";

export async function GET(request: NextRequest, { params }: { params: { postId: string } }) {
const searchParams = request.nextUrl.searchParams;
const query = searchParams.get("key");
const headersList = headers();
const session2 = await getServerSession(authOptions);
const cookieStore = cookies();
const hasCookie = cookieStore.get("next-auth.session-token");

const referer = headersList.get("cookie");
// console.log("🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨", request.headers);
// console.log("🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨", referer);
console.log("🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨", hasCookie);

console.log(
"🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨headersList.getheadersList.get",
headersList.get("Authorization")?.split(" ")[1]
);

console.log(request);

const token = await getToken({ req: request });
console.log(
"tokentokentokentokentokenttokentokentokentokentokenoken🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨",
token
);

const session = await getServerAuthSession();
// console.log(
// "🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 const session = await getServerAuthSession();",
// session?.user
// );

// console.log(
// "🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 const session2 = await getServerAuthSession();",
// session2?.user
// );

const referer = headersList.get("commonRequest");
// console.log("😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊", headersList);
console.log("😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊", query);
try {
return NextResponse.json(
{ Hi: "Hello, Next.js!" },
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
if (token) {
return NextResponse.json(
{ message: "You are authenticated !!" },
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
} else {
return NextResponse.json(
{ message: "You are NOT authenticated !!" },
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
}
} catch (error) {
return NextResponse.json(`Error from our side! ${error.message}`, {
status: 500,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/components/layout/navbar-components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const NavButtonsUnAuthenticated = () => {
<li className="mt-4 lg:mt-0">
<TooltipDemo text="Log In">
<Button
className="font-heading h-[3.2rem] w-32 rounded-full text-xl"
className="font-heading h-[3.2rem] w-32 rounded-full text-xl hover:rounded-xl"
onClick={() => handleCLick("login", "logInRedirect")}
// className={buttonVariants()}
disabled={siteConfig.waitlist}
Expand Down

0 comments on commit dc359dc

Please sign in to comment.