window is not defined in NextJS client component
CameronDickie opened this issue · comments
Hello, I am not sure if this is because of my lack of understanding of how the NextJS client components work or if it is a problem with using react-firebase-hooks in NextJS 14.
I am attempting to do firebase authentication from a client component. I am using the hook 'useSigninWithEmailAndPassword' which is taking in information from the state and is called on a form submit.
Authentication does indeed work as intended, but when I go to run 'npm run build' I am getting a 'window not defined' error related to all of my files which implement methods from react-firebase-hooks. I have attempted to wrap these components in a "typeof window !== "undefined" to see if this would fix my build issues but it does not.
I have spent more time than I would like trying to diagnose this issue. Is this to do with the library itself or how I have setup my project?
Here is the component, which lives at src/app/login/page.tsx
'use client'
import React, { useState, FormEvent } from 'react';
import { useSignInWithEmailAndPassword } from "react-firebase-hooks/auth"
import { auth } from "@/app/firebase/config";
import { useRouter } from 'next/navigation';
const LoginPage: React.FC = () => {
const [email, setEmail] = useState<string>('');
const [password, setPassword] = useState<string>('');
const [rememberMe, setRememberMe] = useState<boolean>(false);
const [signInWithEmailAndPassword] = useSignInWithEmailAndPassword(auth);
const router = useRouter();
const handleLogin = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
// Handle the login logic here, e.g., authenticating with a backend server
console.log('Logging in with Email:', email, 'Password:', password);
try {
const res = await signInWithEmailAndPassword(email, password);
console.log(res);
sessionStorage.setItem('user-session', 'true');
setEmail('')
setPassword('');
router.push('/admin')
} catch (e) {
console.error(e);
}
};
return (
<>
<div className="flex min-h-full bg-white flex-1 flex-col justify-center px-6 py-12 lg:px-8">
<div className="sm:mx-auto sm:w-full sm:max-w-sm">
<h2 className="mt-10 text-center text-2xl font-bold leading-9 tracking-tight text-gray-900">
Sign in to your account
</h2>
</div>
<div className="mt-10 sm:mx-auto sm:w-full sm:max-w-sm">
<form className="space-y-6" onSubmit={handleLogin} method="POST">
<div>
<label htmlFor="email" className="block text-sm font-medium leading-6 text-gray-900">
Email address
</label>
<div className="mt-2">
<input
id="email"
name="email"
type="email"
autoComplete="email"
onChange={(e) => setEmail(e.target.value)}
required
className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
/>
</div>
</div>
<div>
<div className="flex items-center justify-between">
<label htmlFor="password" className="block text-sm font-medium leading-6 text-gray-900">
Password
</label>
<div className="text-sm">
<a href="#" className="font-semibold text-indigo-600 hover:text-indigo-500">
Forgot password?
</a>
</div>
</div>
<div className="mt-2">
<input
id="password"
name="password"
type="password"
autoComplete="current-password"
onChange={(e) => setPassword(e.target.value)}
required
className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
/>
</div>
</div>
<div>
<button
type="submit"
className="flex w-full justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
>
Sign in
</button>
</div>
<div className="flex items-center justify-between">
<div className="flex items-center">
<input
id="remember-me"
name="remember-me"
type="checkbox"
checked={rememberMe}
onChange={(e) => setRememberMe(e.target.checked)}
className="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded"
/>
<label htmlFor="remember-me" className="ml-2 block text-sm text-gray-900">
Remember Me
</label>
</div>
</div>
</form>
</div>
</div>
</>
)
};
export default LoginPage;
Sorry, this was just because I was including this in a static page causing the error at build time