Ulysses Alves Ulysses Alves - 6 months ago 174
Node.js Question

How to send the JWT token to the client with feathersjs-authentication?

I'm using local authentication in my feathersjs REST-API application, but after the user is authenticated, instead of sending me the authentication token, feathers is sending the following HTML as a response to the authentication request:

<body>
<img class="logo" src="alongbase64string" alt="Feathers Logo">
<main>
<h1 class="center-text">Success</h1>
<h3 class="center-text">You are now logged in. We've stored your JWT in a cookie with the name
<span class="italic">"feathers-jwt"</span> for you. It is:
</h3>
<pre id="token"></pre>
</main>
<script type="text/javascript">
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}

var token = getCookie('feathers-jwt');

var el = document.getElementById('token');
el.innerHTML = token;
</script>


which prints the following page:

enter image description here


I think this would work good enough if I was sending the request from a web page, but in my case I need to get the token, because the client is a mobile app, not a web browser, so cookies won't work for me.

Is it possible for me to make feathersjs send the token in the response? Something like:

{
token: 'açldkjfaçldkfjçasdkfjdçakfjd'
}


This way I could store the token in the mobile app, and use it to authenticate further requests to my feathersjs API server.

For now I won't put any more code here, because the application was made entirely with the console commands available by feathersjs, like
feathers generate
but if anyone needs to understand more about the code, just let me know, and I will edit the question adding more details.

Answer

You have to make sure to set the Accept header in your request to application/json otherwise it'll assume HTML and send the page you are seeing.