Invalidate token before logging out

This commit is contained in:
An Phan 2016-01-25 18:37:14 +08:00
parent bbce9d9adf
commit 190c2c117f
4 changed files with 43 additions and 7 deletions

View file

@ -9,6 +9,7 @@ use App\Http\Requests\API\UserUpdateRequest;
use App\Models\User;
use Hash;
use JWTAuth;
use Log;
use Tymon\JWTAuth\Exceptions\JWTException;
class UserController extends Controller
@ -25,12 +26,30 @@ class UserController extends Controller
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
Log:error($e);
return response()->json(['error' => 'could_not_create_token'], 500);
}
return response()->json(compact('token'));
}
/**
* Log the current user out.
*
* @return \Illuminate\Http\JsonResponse
*/
public function logout()
{
try {
JWTAuth::invalidate(JWTAuth::getToken());
} catch (JWTException $e) {
Log:error($e);
return response()->json(['error' => 'could_not_invalidate_token'], 500);
}
return response()->json();
}
/**
* Create a new user.
*

View file

@ -33,11 +33,12 @@ Route::group(['prefix' => 'api', 'namespace' => 'API'], function () {
Route::post('interaction/batch/like', 'InteractionController@batchLike');
Route::post('interaction/batch/unlike', 'InteractionController@batchUnlike');
Route::resource('playlist', 'PlaylistController', ['only' => ['store', 'update', 'destroy']]);
Route::resource('playlist', 'PlaylistController');
Route::put('playlist/{playlist}/sync', 'PlaylistController@sync')->where(['playlist' => '\d+']);
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::get('lastfm/callback', [

View file

@ -30,6 +30,7 @@
import sharedStore from './stores/shared';
import queueStore from './stores/queue';
import userStore from './stores/user';
import preferenceStore from './stores/preference';
import playback from './services/playback';
import focusDirective from './directives/focus';
@ -243,12 +244,14 @@
* Log the current user out and reset the application state.
*/
logout() {
ls.remove('jwt-token');
this.authenticated = false;
playback.stop();
queueStore.clear();
this.loadMainView('queue');
this.$broadcast('koel:teardown');
userStore.logout(() => {
ls.remove('jwt-token');
this.authenticated = false;
playback.stop();
queueStore.clear();
this.loadMainView('queue');
this.$broadcast('koel:teardown');
});
},
},

View file

@ -90,6 +90,19 @@ export default {
http.post('me', { email, password }, successCb, errorCb);
},
/**
* Log the current user out.
*
* @param {Function} cb The callback.
*/
logout(cb = null) {
http.delete('me', {}, () => {
if (cb) {
cb();
}
});
},
/**
* Update the current user's profile.
*