简体   繁体   中英

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:

在此处输入图片说明

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.

您必须确保在请求中将Accept标头设置为application/json否则它将采用HTML并发送您正在查看的页面。

Use the local middleware instead of the socket scheme referenced in the question.

POST'ing login data to /auth/local will yield

{
  "token": {JWT},
  "data": {
    "email": "admin@feathersjs.com",
    "id": 0
  }
}

Your client can pull values from that JSON response and handle them appropriately.

Most anything beyond that will require modifying the stock Feathers demo.

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