Home > Software design >  Redirects to wrong page when logging in
Redirects to wrong page when logging in

Time:12-27

it redirects to an empty js file when logging in, not to the js file I wrote. Should I redirect in my database code? ////////////////

const client = require('../../db')
const express = require('express');
const app = express();
const cors = require("cors");

app.use(cors());
app.use(express.json()); //req.body

app.listen(2136, ()=>{
    console.log("Sever is now listening at port 5000");
})

client.connect();



app.post("/login", async (req, res) => {
  try {
    const { email, password } = req.body;

    const user = await client.query(
      `SELECT * FROM users WHERE email=$1 AND password=$2`,
      [email, password]
    );
    
    if (user.rows.length === 0) {
      res.status(404).send("Kullanıcı adı veya şifre yanlış");
    } else {
      res.send("Kullanıcı adı veya şifre doğru");
   
    }
    
  }catch (err) {
    response
   .status(500)
   .json({ message: "Error in invocation of API: /login" })
  }
});

this is my database code. ///////////////////////

import {
  BrowserRouter as Router,
  Route
} from "react-router-dom";
import React, { useState } from 'react'

import Navbar from '../../common/Navbar/Navbar';
import AdminPage from './AdminPage';

const User = () => {
 
    const [email, setEmail] = useState("");
    const [password, setPassword] = useState("");
  const [user, setUser] = useState([]);
  const [error, setError] = useState('');

  const onSubmitForm = async e => {
    e.preventDefault();
    try {
      const response = await fetch(`http://localhost:2136/login`,{
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ email, password }),
      });
      console.log(email,password)
      console.log(response);
      
      if ((response.status)===404) {
       
        setError('Invalid email or password');
      } else 
      {
        window.location.replace(`/AdminPage`);
      }

      
    } catch (err) {
        console.error(error);
        setError('An error occurred. Please try again later.');
    }
  };


  return (
    <>
    <Navbar/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3 Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"></link>
    <div className="container text-center">
        <h1 className="my-5">Search for the Dormitory You Want</h1>
        <form className="d-flex" onSubmit={onSubmitForm}>
          <input
            type="text"
            name="name"
            placeholder="Enter email ..."
            className="form-control"
            value={email}
            onChange={e => setEmail(e.target.value)}
          />
          <input
            type="text"
            name="name"
            placeholder="Enter password ..."
            className="form-control"
            value={password}
            onChange={e => setPassword(e.target.value)}
          />
          
          <button className="btn btn-success">Submit
          
          </button>
        </form>
        
        
      </div>
    </>
  )
}

export default User

this is my login page. the page it redirects to is a blank page. i don't understand why. ///////////////

import React, { useState } from 'react'
import AdminNavbar from '../../common/Navbar/AdminNavbar';


const AdminPage = () => {
    
  return (
   <>
   <AdminNavbar/>
   </>
  );
  
}

export default AdminPage

and this the page I want to redirect AdminPage.So what can i do? ///////////////////

CodePudding user response:

The difference between assign() and replace():

replace() removes the current URL from the document history.

With replace() it is not possible to use "back" to navigate back to the original document.

You can use assign method instead of location method

  • Related