Next.js Discord

Discord Forum

I m trying to deploy a next.js project through vercel but my server side not being deployed and when

Unanswered
Northeast Congo Lion posted this in #help-forum
Open in Discord
Northeast Congo LionOP
i have tried multiple times but in deployment data can't be fetched from database

35 Replies

American Chinchilla
@Northeast Congo Lion
Did you check any logs
Or network http status
Northeast Congo LionOP
my client side working properly on deployed
but when try to login it says cant fetch data , but on local its working
American Chinchilla
Is there any error message?
Northeast Congo LionOP
no
American Chinchilla
Like what exactly does the error message say
Are you using anything like prisma
Or just fetching?
Northeast Congo LionOP
no error message only the data fetch failing
@American Chinchilla Are you using anything like prisma
Northeast Congo LionOP
no only next.js for both server side and client side
American Chinchilla
Can you a code snippet
Of how your fetching
Can you show *^
Northeast Congo LionOP
my client side working
l
American Chinchilla
I understand, could you please show a code snippet of your fetch
Sorry i dont download files
It be better if you post it here using backticks
Or you can wait for someone else to help too
Northeast Congo LionOP
export async function POST(req) {
  try {
    const body = await req.json();
    const { email, password } = body;

    //fetch
    const user = await User.findOne({ email });

    // Hash password
    const validPassword = await bcrypt.compare(password, user.password);

    if (!validPassword) {
      return NextResponse.json({ message: "Wrong Credientials.", success: false }, { status: 400 });
    }
    //create token data
    const tokenData = {
      id: user._id,
      username: user.username,
      email: user.email
    }

    // Option to prevent dots in the token
    const token = jwt.sign(tokenData, process.env.TOKEN, {
      expiresIn: '7d',
      // Ensure no dots in the resulting token
      jwtid: 'unique-id-without-dots'
    });


    const response = NextResponse.json({
      message: "Login successful",
      success: true,
    })
    response.cookies.set("token", token, {
      httpOnly: true,

    })
    return response;

  } catch (error) {
    console.log('jo');
    return NextResponse.json({ error: error.message }, { status: 500 })
  }
}
this is the api
gor login
and the client side -
American Chinchilla
And you say you dont get any error message from the http status your returning?
Just to confirm
Northeast Congo LionOP
  console.log(process.env.DOMAIN);
  const checkEmailExists = async (email) => {
    try {
      const response = await axios.get(`/api/user/signup/email?email=${email}`);
      return response.data.exists;
    } catch (error) {
      throw new Error('Error checking email');
    }
  };

  const matchUser = async (email, password) => {
    try {
      const response = await axios.post(`/api/user/login`, { email, password });
      return response;
    } catch (error) {
      throw new Error('Error checking email and password');
    }
  };

  const handleSubmit = (e) => {
    e.preventDefault();
    setLoading(true);
    setError('');

    if (!email || password.length === 0) {
      setLoading(false);
      setError('Please fill all the fields');
      return;
    }

    checkEmailExists(email)
      .then((emailExists) => {
        if (!emailExists) {
          setLoading(false);
          showAlert('Error', 'You have not signed up before, please Sign Up.');
          router.push('/signup');
          return Promise.reject('Email does not exist');
        }

        return matchUser(email, password);
      })
      .then((response) => {
        if (response.status === 200) {
          setLoading(false);
          setUser(response.data.data);
          fetchCurrentUser();
          showAlert('Success', 'You have logged in successfully!');
          setEmail('');
          setPassword('');
          router.push(nextPath);
        } else {
          setLoading(false);
          showAlert('Error', 'There was an error logging in. Please try again.');
        }
      })
      .catch((error) => {
        if (error.message === 'Email does not exist') {
          setError('Email does not exist');
        } else {
          setError('Invalid email or password');
        }
        setLoading(false);
      });
  };
@American Chinchilla Just to confirm
Northeast Congo LionOP
when i try to login which is working fine on local storage, it is saying - 779-fb386eda2bd2c8bb.js:6


GET https://sweet-share-latest.vercel.app/api/user/signup/email?email=arshad@gmail.com 400 (Bad Request)
American Chinchilla
This error is from production?
Or local
@American Chinchilla This error is from production?
Northeast Congo LionOP
production, in local its working
American Chinchilla
Hmm well it saying there 400
Im not too familiar with bycrpts
Northeast Congo LionOP