diff --git a/app/Http/Middleware/RedirectIfAuthenticated.php b/app/Http/Middleware/RedirectIfAuthenticated.php index e4cec9c8..e3a4beef 100644 --- a/app/Http/Middleware/RedirectIfAuthenticated.php +++ b/app/Http/Middleware/RedirectIfAuthenticated.php @@ -18,7 +18,7 @@ class RedirectIfAuthenticated public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { - return redirect('/home'); + return response()->json(['message' => __('auth.already_authenticated')], 400); } return $next($request); diff --git a/resources/js/langs/locales.js b/resources/js/langs/locales.js index 88486fa5..086b1823 100644 --- a/resources/js/langs/locales.js +++ b/resources/js/langs/locales.js @@ -6,6 +6,7 @@ export default { "register": "Register", "hello": "Hi {username} !", "throttle": "Too many login attempts. Please try again in {seconds} seconds.", + "already_authenticated": "Already authenticated", "confirm": { "logout": "Are you sure you want to log out?" }, @@ -267,6 +268,7 @@ export default { "register": "Créer un compte", "hello": "Hi {username} !", "throttle": "Trop de tentatives de connexion. Veuillez réessayer dans {seconds} secondes.", + "already_authenticated": "Déjà authentifié", "confirm": { "logout": "Etes-vous sûrs de vouloir vous déconnecter ?" }, diff --git a/resources/lang/en/auth.php b/resources/lang/en/auth.php index 842de3a2..60aa9e19 100644 --- a/resources/lang/en/auth.php +++ b/resources/lang/en/auth.php @@ -18,6 +18,7 @@ return [ 'register' => 'Register', 'hello' => 'Hi {username} !', 'throttle' => 'Too many login attempts. Please try again in :seconds seconds.', + 'already_authenticated' => 'Already authenticated', 'confirm' => [ 'logout' => 'Are you sure you want to log out?', ], diff --git a/resources/lang/fr/auth.php b/resources/lang/fr/auth.php index 899fb19c..a232a101 100644 --- a/resources/lang/fr/auth.php +++ b/resources/lang/fr/auth.php @@ -18,6 +18,7 @@ return [ 'register' => 'Créer un compte', 'hello' => 'Hi {username} !', 'throttle' => 'Trop de tentatives de connexion. Veuillez réessayer dans :seconds secondes.', + 'already_authenticated' => 'Déjà authentifié', 'confirm' => [ 'logout' => 'Etes-vous sûrs de vouloir vous déconnecter ?', ], diff --git a/tests/Feature/Auth/LoginTest.php b/tests/Feature/Auth/LoginTest.php index ddf19f28..42037489 100644 --- a/tests/Feature/Auth/LoginTest.php +++ b/tests/Feature/Auth/LoginTest.php @@ -46,6 +46,32 @@ class LoginTest extends TestCase } + /** + * test User login via API + * + * @test + */ + public function testUserLoginAlreadyAuthenticated() + { + + $response = $this->json('POST', '/api/login', [ + 'email' => $this->user->email, + 'password' => 'password' + ]); + + $response = $this->actingAs($this->user, 'api') + ->json('POST', '/api/login', [ + 'email' => $this->user->email, + 'password' => 'password' + ]); + + $response->assertStatus(400) + ->assertJson([ + 'message' => __('auth.already_authenticated') + ]); + } + + /** * test User login with missing values via API *