Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retain cookies in node-webkit

I wish to create an application using node-webkit which does the simple job to open a remote web application. The web application has some cookie based authentication. Considering that the user has signed-in successfully, how can the required cookies be retained, so that the next time the application runs, the user will be authenticated?

like image 813
Dimitris Zorbas Avatar asked May 04 '15 22:05

Dimitris Zorbas


2 Answers

  1. You can get auth cookies using this instructions: https://github.com/nwjs/nw.js/wiki/window#windowcookies

  2. Save it with prefered method: https://github.com/nwjs/nw.js/wiki/Save-persistent-data-in-app

  3. On next start just check that record exists, and set cookies with method from 1.

Should be something like this:

var gui = require('nw.gui');
var win = gui.Window.get();

function login() {
   var opts = {};
   if(localStorage.auth) {
      opts.cookies: [
           'Auth': localStorage.auth
      ];
   }

    someRequest.get(opts, function(result) {
        if(result)
          localStorage.auth = win.cookies.get('auth');
    });
}
like image 96
Nazar Sakharenko Avatar answered Nov 03 '22 08:11

Nazar Sakharenko


As of 2018, NW.js retains cookies and other persistent browser data by default. These are stored in %LOCALAPPDATA%/name-in-manifest/ in Windows or equivalent depending on OS. Nothing is required from the application itself, you do not have to persist cookie data manually in local storage.

like image 31
Mahn Avatar answered Nov 03 '22 10:11

Mahn