Using JWT with Laravel

Asked

Viewed 816 times

0

Guys, I’m using JWT to generate access token to my system... So far it’s ok, I’m managing to call my API and the same return me a Token.

The problem is in the following scenario. - My intention is to work with API in my web application and would reuse that same API in Mobile. - Using JWT I am generating a TOKEN but in my WEB application I need to redirect the user who does not have this Token to the LOGIN screen so it will generate the TOKEN and be redirected to the INDEX page of the WEB. (Already in the application the treatment is different and does not come the case here.. )

My problem is that I cannot redirect my WEB user to my system’s INDEX page...

Doubt is my Token I need to set somewhere? Create a session and save that token in it? or what I do?

My Route is as follows...

Route::get('/', 'HomeController@getIndex');

Route::group(['prefix' => 'api'], function () {

    Route::get('/', function () {
      return response()->json(['message' => 'Jobs API', 'status' => 'Connected']);;
    });

    Route::post('/auth/login', 'UsuarioController@login');

    Route::group(['middleware' => ['jwt.auth', 'jwt.refresh']], function() {
        Route::post('logout', 'AuthenticateController@logout');

        Route::get('/teste', function() {
            return response()->json(['foo' => 'bar']);
        });
    });    
});

There in the Homecontroller class I put in the Construct

$this->middleware('jwt.auth');   

But every time I enter the main page of my site already displays the message:

{"error":"token_not_provided"}

My Login function is below:

public function login(AuthenticateRequest $request) {        
      // Get only email and password from request
      $credentials = $request->only('usuario', 'senha');

      // Get user by email
      $user = User::where('usuario', $credentials['usuario'])->first();
      // $company = DB::table('empresas_funcionario')->where('usuario', $credentials['usuario'])->first();

      // Validate Company
      if(!$user) {
        return response()->json([
          'result' => false,
          'error' => Lang::get('messages.userInvalid')
        ], 401);
      }

      // Validate Password
      if (md5($credentials['senha']) != $user->senha ) {
          return response()->json([
            'result' => false,
            'error' => Lang::get('messages.passInvalid')
          ], 401);
      }

      // Generate Token
      $token = JWTAuth::fromUser($user);

      // Get expiration time
      $objectToken = JWTAuth::setToken($token);
      // $expiration = JWTAuth::decode($objectToken->getToken())->get('exp');

      return response()->json([
        'com' => $user,
        'access_token' => $token,
        'token_type' => 'gestor'
      ]);
    }

I wonder if someone has been through this and how can I do so after the person logs through the Web is redirected to the Index page and/or how do I identify if the token was created so I play it to the Login screen or if I don’t play for the Index....

I appreciate the help...

1 answer

-2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.