Getting error: Reference Error: window object is not defined
Answered
Maltese posted this in #help-forum
MalteseOP
Context: I want to achieve client side authentication my next.js 13.4 application with endpoint URL/cart
As child component is wrapped within SessionProvider it should get window object, but still getting error. Help me to achieve client side authentication! Thanks in Advance
I have written
And Cart component is defined as follows:
Also NextAuth handler is defined as follows:
As child component is wrapped within SessionProvider it should get window object, but still getting error. Help me to achieve client side authentication! Thanks in Advance
I have written
cart/page.jsx as follows:"use client";
import React from "react";
import { SessionProvider } from "next-auth/react";
import Cart from "@/components/Cart";
export default function CartUI() {
return (
<SessionProvider>
<Cart />
</SessionProvider>
);
}And Cart component is defined as follows:
"use client"
import React from 'react'
import { useSession, signIn, signOut } from 'next-auth/react';
export default function Cart() {
const { data: session } = useSession();
// Check if we're on the client side before accessing the window object
const handleAuthClick = () => {
if (typeof window !== 'undefined') {
if (session) {
signOut();
} else {
signIn();
}
}
};
console.log("session", session);
return (
<div>
{/* <button onClick={handleAuthClick}>{ session ? "Log out" : "Log In"}</button> */}
</div>
)
}Also NextAuth handler is defined as follows:
import NextAuth from 'next-auth';
import GoogleProvider from 'next-auth/providers/google';
const handler = NextAuth({
providers: [
GoogleProvider({
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
clientSecret: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_SECRET,
}),
],
callbacks: {
async signIn({ account, profile, user, credentials }) {
try {
console.log("user in signIn callback!", JSON.stringify(user, null, 2));
} catch (error) {
console.log("Error checking if user exists: ", error.message);
return false
}
},
},
session: {
strategy: "jwt"
},
secret: process.env.NEXTAUTH_SECRET
});
export { handler as GET, handler as POST };Answered by DirtyCajunRice | AppDir
1. dont make pages client. do it in the components.
2. if it needs window you need to wait to access window until it has mounted, preferably in a useEffect
2. if it needs window you need to wait to access window until it has mounted, preferably in a useEffect
8 Replies
1. dont make pages client. do it in the components.
2. if it needs window you need to wait to access window until it has mounted, preferably in a useEffect
2. if it needs window you need to wait to access window until it has mounted, preferably in a useEffect
Answer
MalteseOP
regarding 1st point if I dont make pages client then i wont be able to use SessionProvider there right?
@Maltese regarding 1st point if I dont make pages client then i wont be able to use SessionProvider there right?
European sprat
SessionProvider should be a client component
The page does not need to be a client component in order to use another client component
MalteseOP
But if remove then i get the error useSession must be wrapped within SessionProvider!
Also i have update NextAuth code with callback, strategy and secret and my error is gone but it's not redirecting me to /api/auth/google by default previously it was!!
Also i have update NextAuth code with callback, strategy and secret and my error is gone but it's not redirecting me to /api/auth/google by default previously it was!!
European sprat
if SessionProvider is a thid party component that isn't updated for server components you may need to create your own wrapper component for it where you specify use client
MalteseOP
Now when i am visiting cart route, logs are as follows:
it doesnt redirect me back to default sign in page given by NextAuth.js
Thanks I will work on making on wrapper and optimize!
- wait compiling /api/auth/[...nextauth]/route (client and server)...
- event compiled successfully in 879 ms (528 modules)
session in cart component undefinedit doesnt redirect me back to default sign in page given by NextAuth.js
Thanks I will work on making on wrapper and optimize!
MalteseOP
Defining maxAge and refershAge solved issues too!