Auto log out when session expires (fixes #320)

This commit is contained in:
An Phan 2016-05-06 13:04:59 +08:00
parent 68baf5001c
commit 3d46e73e8b
3 changed files with 9 additions and 8 deletions

View file

@ -7,6 +7,7 @@ use App\Http\Requests\API\UserLoginRequest;
use App\Http\Requests\API\UserStoreRequest;
use App\Http\Requests\API\UserUpdateRequest;
use App\Models\User;
use Exception;
use Hash;
use JWTAuth;
use Log;
@ -43,12 +44,12 @@ class UserController extends Controller
*/
public function logout()
{
try {
JWTAuth::invalidate(JWTAuth::getToken());
} catch (JWTException $e) {
Log:error($e);
return response()->json(['error' => 'could_not_invalidate_token'], 500);
if ($token = JWTAuth::getToken()) {
try {
JWTAuth::invalidate($token);
} catch (Exception $e) {
Log::error($e);
}
}
return response()->json();

View file

@ -12,6 +12,7 @@ Route::get('/♫', function () {
Route::group(['prefix' => 'api', 'namespace' => 'API'], function () {
Route::post('me', 'UserController@login');
Route::delete('me', 'UserController@logout');
Route::group(['middleware' => 'jwt.auth'], function () {
Route::get('/', function () {
@ -40,7 +41,6 @@ Route::group(['prefix' => 'api', 'namespace' => 'API'], function () {
Route::resource('user', 'UserController', ['only' => ['store', 'update', 'destroy']]);
Route::put('me', 'UserController@updateProfile');
Route::delete('me', 'UserController@logout');
Route::get('lastfm/connect', 'LastfmController@connect');
Route::post('lastfm/session-key', 'LastfmController@setSessionKey');

View file

@ -22,7 +22,7 @@ Vue.http.interceptors.push({
NProgress.done();
if (r.status === 400 || r.status === 401) {
if (r.request.method !== 'POST' && r.request.url !== 'me') {
if (!(r.request.method === 'POST' && r.request.url === 'me')) {
// This is not a failed login. Log out then.
app.logout();
}