Home > Software design >  i'm having problem making a login system in node js
i'm having problem making a login system in node js

Time:10-11

When someone tries to log in and their id and password are wrong, they should be redirected or go back to the login page.

I tried window.location.replace but I don't know why it's not working.

And when someone enters the right id pass they should be sent to the dashboard.

GitHub: https://github.com/GoD-ATHEN/nodedash

const express = require('express');
const app = express();
const mysql = require('mysql');
const router = express.Router();
const path = require('path');
var bodyParser = require('body-parser')
const ifl = path.join(__dirname, '/views/assets/')

var urlencodedParser = bodyParser.urlencoded({ extended: false });

app.set('view engine', 'ejs');
app.use('/assets',express.static(ifl));

//CONNECTION CONFIGURATION
var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "anoe"
   });

//========== Login =============
app.get("/login", (req, res)=>{
    res.render("login");
});


//==============================
app.get("/loginn", (req, res)=>{

/////////////////////////PROBLEM AREA=========================

    let query = `SELECT * FROM users WHERE email = "${req.query.email}" AND pass = "${req.query.pass}"`;
    
    con.query(query, (err, result) => {
      if(err) throw err, window.location.replace("http://phenomit.com");

        if (result[0].email === `${req.query.email}` && result[0].pass === `${req.query.pass}`){
          var status = 1;
        }
        else{
          status = 0;
        }

    });

/////////////////////////PROBLEM AREA END=========================

    res.render("dashboard");

});
//========== Login END ==========


app.listen(3000);

CodePudding user response:

window is not known in your backend. You need to use res.redirect([status,] path) to redirect a request.

Also if(err) throw err, window.location.replace("http://phenomit.com"); doesn't make that much sense. Inside your callback try something like:

if (err) return res.redirect(...);
...
return res.render('dashboard');

CodePudding user response:

You are in the NodeJS context. The window object does not refer to your client window. You could handle redirection directly from the client application or just use res.redirect from express

CodePudding user response:

instead of

//==============================
app.get("/loginn", (req, res)=>{...})

why not write as

app.post("/login",(req,res)=>{...})
i think it's because you mispell app.post("login",...) to app.get("login",...), it make app.get("login",...) have 2 function

hope this helps solve the problem

  • Related