How to Have Simultaneous Sessions?

Asked

Viewed 38 times

-1

Hello I created a basic CRUD Node server using Heroku and Mysql Postgres, the problem is that it only allows you to create a session, ie if you log into a devices all entering the site are logged in as well. How to solve this and if you refer me to a library could explain to me how it works?

    const express = require('express');
const session = require('cookie-session');
const bodyParser = require('body-parser');
const app = express();
const Sequelize = require('sequelize')


app.use(express.json())
app.use(session({
      secret:'wtsrtsgt1234@3#22',

})
       )

app.use(bodyParser.json());      
app.use(bodyParser.urlencoded({extended: true}));


const sequelize = new Sequelize(process.env.DATABASE_URL,{
    dialect: 'postgres',
    protocol: 'postgres',
    port:5432
})


/*
const sequelize = new Sequelize('player','root','',{
    host:'localhost',
    dialect:'mysql'
})
*/

const Usuarios = sequelize.define('usuarios',{
    email: {
        type: Sequelize.STRING
    },
    senha:{
        type: Sequelize.STRING
    },
    nivel:{
        type: Sequelize.STRING
    }
})
let sess;



app.get('/', (req,res)=>{
    Usuarios.sync({force:true})
    if(sess == undefined){
        res.sendfile(__dirname+"/public/login.html")
    }
    else{
        res.send("YOU ARE A COMMUM USER")
    }

})

app.post('/login', async (req,res)=>{
    let email = req.body.email;
    let senha = req.body.senha;
    if(email == "empty" || senha == "empty"){
        res.json({"MENSAGEM":"Algum campo está vazio"})
    }
    else{
        const retorno = await Usuarios.findOne({ where: { email: email, senha:senha } })
            if(retorno == null){
                res.json({"MENSAGEM":"Não existe essa conta"})
             }
            else{
                res.json({"MENSAGEM":"Logado"})
                sess = req.session
                sess.nivel = retorno.nivel
             }

    }




})
app.get('/a', (req,res)=>{
    res.send('pock')
})
app.post('/registre', async (req,res)=>{
    let email = req.body.email
    let senha = req.body.senha
    let confirm = req.body.confirm

    if(senha != confirm && senha != "empty"){
        res.json({"MENSAGEM":"Senhas não batem"})
    }
    else if(senha == "empty" || confirm == "empty" || email == "empty"){
        console.log('ERRO:falta preencher um campo')
        res.json({"MENSAGEM":"Falta preencher um campo"})
    }
    else{
        const retorno = await Usuarios.findOne({ where: { email: email } })
        if(retorno == null){
            Usuarios.create({
                email: email,
                senha: senha,
                nivel: 'comum'
        })
        res.json({"MENSAGEM":"Conta criada"})
    }
        else{
            res.json({
                "MENSAGEM":"Esse email já existe"
            })
        }

    }

})

app.listen(process.env.PORT || 3000)
  • Take a look and see if you’re looking for it: https://github.com/expressjs/session.

1 answer

0


Try to put the application login inside a "middleware" (a function that is executed at each request on your server), because maybe what is logging in is your entire application and not only the request made at that time.

Node.js works with an infinite loop that receives the request and generates a response, use the request and response process to log in.

  • It is basically this same kkk after a long time breaking the head I realized that when using a global session I create a single "room" and then everyone who enters access this same "room". Thank you for the reply

Browser other questions tagged

You are not signed in. Login or sign up in order to post.