简体   繁体   中英

can't get hapi reply headers to work

I'm trying to get JWT for hapi to work, I'm getting a difficulty with sending the token back to the client. I'm trying to send it in the header like the example:

in example from: jwt

res.writeHead(200, {
'content-type': 'text/html',
'authorization': token});

I'm trying something like :

reply('Here is token').header({
    'content-type': 'text/html',
    'authorization': token}).code(200);

But I get an error

TypeError: Uncaught error: key.toLowerCase is not a function

Somehow I can't find an example of how to do this. I'm suppose to send the Token back to the 'client app' in the header, but can't find a way of doing it with hapi. Anyone has any clue?

The syntax for chaining is like this

return reply('Here is token')
    .type('text/html')
    .header('X-authorization', token)
    .code(200);

Usually people send the token in the body of the response back to the client along with some user information for front end convenience, so your reply would probably just look more like this

const authenticatedUser = {
  id: 'testuser',
  firstName: 'Simon',
  lastName: 'Prince',
  token: 'bearer supersecretjwttokenhere'
};
return reply(authenticatedUser);

Your client application will get that JSON response, grab the token, store it in local storage and update the UI with the user information from the response.

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