简体   繁体   中英

how can I get status (stored in json) from another page at ejs file after login success

I am building a login system. if user login successfully then it will show status:'ok' in json. what i want is to get that status in ejs file, status is in nodejs file. so that every time ejs notices that status, on the homepage instead of showing the signin and signup buttons like this header before login , it will show the user's photo like this header after login . i am working on nodejs (where login authentication is installed) and ejs. If anyone sees that the repository understands the code I was talking about, this is my code

login.js

const jwt = require('jsonwebtoken')
    const bcrypt = require('bcryptjs')
    const User = require('../model/user') // ../model/user is where user data is stored
    const dotenv = require('dotenv')
    
    const login =  (req, res) => {
    
        const { username, password: plainTextPassword} = req.body
        //no value entered
        if (!plainTextPassword || !username) {
          return res.json({status: 'error', error: 'Invalid username/ password'})
        }
        else {  
          User.query('SELECT * FROM users WHERE username = ?', [username], async (error, result) => {
            if (error) throw error
            const validPassword = await bcrypt.compare(plainTextPassword, result[0].password)
            console.log(validPassword)
            //enter wrong username or password 
            if ( !result[0] || !validPassword) {
              return res.json({status: 'error', error: 'Incorrect username or password'}) }
            //In case the username, password is entered correctly
            else {
              const token = jwt.sign({
                id: result[0].id, 
                username: result[0].username
              },
              process.env.JWT_SECRET,{
              expiresIn: process.env.JWT_EXPIRES
              })
              const cookie_option = {
                expiresIn: new Date(Date.now() + process.env.COOKIE_EXPIRES *24 *60 *60 *1000),
                httpOnly: true,
                secure: false
              }
              res.cookie('userRegister', token, cookie_option)
              return res.json({status: 'ok', success: 'Logged in successfully', data: token})
            }
          })
        }
      }
      module.exports = login

Hi you can send req in Ejs with Helper like this

module.exports = class Helper {
    req: any;

    constructor(req: any) {
        autoBind(this);
        this.req = req;
    }

    getObject() {
        return {
            req: this.req,    
        };
    }

};

and call your helper in your server file like this:

  app.use((req: any, res: any, next: any) => {
            app.locals = new Helper(req, res).getObject();
            next();
        });

after that you can set any variable in your req like:

req.status = true;

and you can use this in your ejs view like:

<%= req.status %>

good luck;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM