Search code examples
authenticationvuejs2couchdbtoken

Couchdb Vue.js authentication


I'm trying to move a node/express authentication application over to vue.js. I am able to successfully authenticate getting a 200 code. However, the response returned from couchdb does not contain the "set-cookie" header, which contains the much needed AuthSession token. The code that I am using in my Vue component is:

      var reqBody = "name="+user+"&password="+pass;
      var reqBodyLength = reqBody.length;
      console.log(reqBodyLength);
      this.$http.post('http://localhost:5984/_session/', reqBody, {headers: {'Content-Type' : 'application/x-www-form-urlencoded', 'Accept' : 'application/json'}}).then(response => {
        console.log("response: " + JSON.stringify(response));
        console.log("response.headers: " + JSON.stringify(response.headers));
        console.log("response.headers.set-cookie: " + JSON.stringify(response.headers["set-cookie"]));
      }, response => {  
        alert('you unauthorized, fool!')
      })

Has anyone ever had an issue getting the "set-cookie" header?

Thanks, Tyler


Solution

  • Couch's AuthSession is HttpOnly cookie and therefore can't be accessed through a client side script. But the cookie itself should be set to a browser by that _session query, so all the consequent requests will be authorized.

    BTW, /_session also accepts JSON payload, at least in CouchDB 1.6, so the query doesn't have to be in x-www-form-urlencoded form.