Jwt-auth: how to send the token by the header and not by the url

Created on 19 Feb 2018  路  5Comments  路  Source: tymondesigns/jwt-auth

My environment

| homestead | vagrant
| Bug? | no
| New Feature? | no
| Framework | Laravel
| Framework version | 5.5
| PHP version | 7.1

thanks in advance

Most helpful comment

I believe you could so something like this:

return response()
    ->header( Token in here! )
    ->json($response, 201);

Double check with the docs - but I think that should work!

But, in most situations I would just return the token in the response body.

All 5 comments

Use Laravel's response helper to return the token in the header.

return response(200)->header( Put token in here );

Is this what you are trying to achieve?

This is my code, how would it be?

what I want is that all the tokens that I use are sent by the header for security, none should be for the url

public function signin(Request $request)
{
$this->validate($request,[
'user_email' => 'required|email',
'password' => 'required|min:6',
]);

  $user_email = $request->input('user_email');
  $password = $request->input('password');

  if ($user = User::where('user_email', $user_email)->first()) {
    $credentials = [
      'user_email' => $user_email,
      'password' => $password,
    ];

    $token = null;

    try {
        if (!$token = JWTAuth::attempt($credentials)) {
          return response()->json([
            'error' => 'El correo 贸 la contrase帽a son incorrectos'
          ],404);
        }
    } catch (JWTAuthException $e) {
        return response()->json([
          'error' => 'failed_to_create_token',
        ],404);
    }

    $response = [
      'success' => 'Usuario Logueado',
      'Usuario' => $user,
      'token' => $token
    ];
    return response()->json($response, 201);
  }
   $response =[
     'error' => 'Ha ocurrido un error durante el logueo al sistema',
   ];
   return response()->json($response,404);
}

I believe you could so something like this:

return response()
    ->header( Token in here! )
    ->json($response, 201);

Double check with the docs - but I think that should work!

But, in most situations I would just return the token in the response body.

try placing the code as you say but this one says this
image
image
I'm using postman to prove everything, what am I doing wrong?
The only thing I saw about passing the token via http with the header is this, but I do not understand
image

I already knew how to send the token by the header in the following way:
image

image

but I get another question, how do I recover the token in another view so that I do not mark the token is required if something like this happens:

image

if I pass the token by url it works, but it's not what I need

image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SimonErich picture SimonErich  路  23Comments

johncloud200 picture johncloud200  路  32Comments

frenetic picture frenetic  路  26Comments

marijang picture marijang  路  53Comments

vsecades picture vsecades  路  26Comments