Next.js Discord

Discord Forum

Prerender error during build

Unanswered
Schneider’s Smooth-fronted Caima… posted this in #help-forum
Open in Discord
Schneider’s Smooth-fronted CaimanOP
Error occurred prerendering page "/login". Read more: https://nextjs.org/docs/messages/prerender-error

ReferenceError: localStorage is not defined

21 Replies

American Chinchilla
@Schneider’s Smooth-fronted Caiman did you mark it as a client comp?
Schneider’s Smooth-fronted CaimanOP
yep
do i even mark my hook as the clinet component
American Chinchilla
Can you show how your marking it as a client comp?
Schneider’s Smooth-fronted CaimanOP
"use client";
import useLogin from "@/hooks/useLogin";
import { useRouter } from "next/navigation";
import React, { useState } from "react";

const Page = () => {
  const router = useRouter();
  const { loading, login } = useLogin();
  const [inputs, setInputs] = useState({
    email: "",
    password: "",
  });
  const handleLogin = (e: any) => {
    e.preventDefault();
    login(inputs);
  };

  return (
    <div className="flex justify-center items-center h-screen bg-[url('/bg.jpg')] bg-fixed bg-no-repeat bg-cover">
      body
    </div>
  );
};

export default Page;
import { useState } from "react";
import toast from "react-hot-toast";
import { useRouter } from "next/navigation";
import { useAuthContext } from "@/context/AuthContext";

interface LoginProps {
  email: string;
  password: string;
}

const useLogin = () => {
  const router = useRouter();
  const [loading, setLoading] = useState(false);
  // @ts-ignore
  const { setAuthUser } = useAuthContext();
  const login = async ({ email, password }: LoginProps) => {
    try {
      setLoading(true);
      const res = await fetch("/api/auth/login", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ email, password }),
      });

      const data = await res.json();

      if (data.error) {
        throw new Error(data.error);
      }

      await localStorage.setItem("Auth", JSON.stringify(data));

      await setAuthUser(data.details);

      toast.success("Logged in");
      router.push("/");
    } catch (err: any) {
      toast.error(err.message);
      console.log(err);
    } finally {
      setLoading(false);
    }
  };
  return { loading, login };
};

export default useLogin;
American Chinchilla
In uselogin
You have to mark it as a client too
@American Chinchilla You have to mark it as a client too
Schneider’s Smooth-fronted CaimanOP
still not working
ill die 😭 i have to presend this
istg i hate next js
imma never use it again
American Chinchilla
@Schneider’s Smooth-fronted Caiman it should work
Any component using state or hook or local storage should be marked as client
Schneider’s Smooth-fronted CaimanOP
i just wrapped my authcontextprovdider with a forced client component now it works thankfully
American Chinchilla
Also i forgot to say
Sometimes these errors are from server imports inside a client comp
@American Chinchilla Sometimes these errors are from server imports inside a client comp
Schneider’s Smooth-fronted CaimanOP
😭 i wish u said this earlier
American Chinchilla
Yeah so if your using next auth just make sure like if it using server side your not importing inside a client comp
As it wont work
Schneider’s Smooth-fronted CaimanOP
yea it works now thanks so much