0
I’m creating a Rest api here, for some reason I get this message
"The page has expired due to inactivity.
Please refresh and try again."
Segue Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use JWTAuth;
use App\User;
use JWTAuthException;
class UserController extends Controller
{
private $user;
public function __construct(User $user){
$this->user = $user;
}
public function register(Request $request){
$user = $this->user->create([
'name' => $request->get('name'),
'email' => $request->get('email'),
'password' => bcrypt($request->get('password'))
]);
return response()->json(['status'=>true,'message'=>'User created successfully','data'=>$user]);
}
public function login(Request $request){
$credentials = $request->only('email', 'password');
$token = null;
try {
if (!$token = JWTAuth::attempt($credentials)) {
return response()->json(['invalid_email_or_password'], 422);
}
} catch (JWTAuthException $e) {
return response()->json(['failed_to_create_token'], 500);
}
return response()->json(compact('token'));
}
public function getAuthUser(Request $request){
$user = JWTAuth::toUser($request->token);
return response()->json(['result' => $user]);
}
}
The routes are like this
Route::prefix('api/v1/')->group(function () {
Route::post('register', 'UserController@register');
Route::group(['middleware' => 'jwt.auth'], function () {
Route::get('user', 'UserController@getAuthUser');
});
});
And the mistake I get is like this
DETAIL: It is being created a file in Storage/framework/Session to each connection even if I do, example, if I connect via Postman it creates one, if I connect via Chrome another and so will!
How to solve?
The concept ai is another should be created a token and not a Session
– novic
The File it creates within Session is this : a:2:{s:6:"_token";s:40:"zHZgdWW1brddvkcTgFlQvlkCatzJrwqzCYe8xK3k";s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}
– Diego Ananias
Read on: https://laravel.com/docs/5.4/passport#Introduction
– novic
Also: https://scotch.io/tutorials/token-based-authentication-for-angularjs-and-laravel-apps
– novic
Detail, just so you know, I have another route... When opening this route, it works perfectly. Maybe the problem is in the request, no?
– Diego Ananias
It can be!!! too!
– novic
@Virgilionovic I solved here crazy, I needed to comment the line of the Crsftoken if not he would expect the Token in the molds of the crsf_token and would not work ever... But vlw boy.
– Diego Ananias