cookies saving not working on electron app

Viewed 39

I have a simple login code that uses jwt to verify token and save credentials to cookies. It works perfectly on web browsers like chrome but when I load the site in an electron app even though the login verification process passes the cookies are not being stored. Does electron require more steps in order to save to cookies? How can I fix my code in order to save to cookies on electron. Thanks in advance.

const jwt = require('jsonwebtoken')
const db = require('path_to_database')
const bcrypt = require('bcryptjs')

const login = async(req, res) => {
  const {
    username,
    password
  } = req.body;
  if (!username || !password) return res.json({
    status: 'error',
    error: 'Please enter your username and password'
  })
  else {
    db.query('SELECT * FROM users WHERE username = ?', [username], async(Err, result) => {
      if (Err) throw Err;
      if (!result.length || !await bcrypt.compare(password, result[0].password)) return res.json({
        status: 'error',
        error: 'Incorrect Username or password'
      })
      else {
        const token = jwt.sign({
          id: result[0].id
        }, process.env.JWT_SECRET, {
          expiresIn: process.env.JWT_EXPIRES
        })
        const cookieOption = {
          expiresIn: new Date(Date.now() + process.env.COOKIE_EXPIRES * 24 * 60 * 60 * 1000),
          httpOnly: true
        }
        //registers on chrome but not on electron app
        res.cookie('userRegistered', token, cookieOption)
        return res.json({
          status: 'success',
          success: 'User has been logged In'
        })
      }
    })
  }
}

1 Answers

Chrome doesn't support cookies for local files(file://) by default.

You could save token to local storage.

Related