Skip to content

Instantly share code, notes, and snippets.

@rozsazoltan
Last active October 6, 2022 11:00
Show Gist options
  • Save rozsazoltan/80b6d0766b39c54135b0831cd6acbc2e to your computer and use it in GitHub Desktop.
Save rozsazoltan/80b6d0766b39c54135b0831cd6acbc2e to your computer and use it in GitHub Desktop.
Logout other session with current user, when user athenticated again.

rozsazoltan - gist License

Laravel: új hitelesítést követően minden más munkafolyamat megszakítása

Logout other session with current user, when user athenticated again.

<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
// ...
/**
** Szükség van a \Illuminate\Session\Middleware\AuthenticateSession köztes szoftver deffiniálására
* - a munkameneteket azonosítja
*/
protected $routeMiddleware = [
// ...
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
// ...
];
// ...
}
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
/**
** Építsük be a Laravel alapértelmezett felhasználó hitelesítőjét a Controllerünkbe.
*/
use AuthenticatesUsers;
/**
** Bejelentkezési kérelem feldolgozása
* - a függvény a "Illuminate\Foundation\Auth\AuthenticatesUsers" fájlból lett átkonvertálva,
* mivel a használni kívánt authenticated függvény védett, így erre is szükség van.
* - a függvényen módosítani nem kötelező, ha egyéni logikát alkalmazol, akkor azt itt teheted meg.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
*
* @throws \Illuminate\Validation\ValidationException
*/
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if (method_exists($this, 'hasTooManyLoginAttempts') &&
$this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
if ($request->hasSession()) {
$request->session()->put('auth.password_confirmed_at', time());
}
return $this->sendLoginResponse($request);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
/**
** Ha a felhasználó sikeresen bejelentkezett
* - a függvény a "Illuminate\Foundation\Auth\AuthenticatesUsers" fájlból lett átkonvertálva,
* mivel itt tudjuk megadni, hogy mi történjen a siekres bejelentkezést követően.
*
* @param \Illuminate\Http\Request $request
* @param mixed $user
* @return mixed
*/
protected function authenticated(Request $request, $user)
{
// Jelentkeztesd ki az összes többi munkamenetet, amely a jelenlegi felhasználói fiókot használja
// - szükség van a felhasználó jelszavára a művelet végrehajtásához.
Auth::logoutOtherDevices($request->password);
}
// ...
}
<?php
// ...
/**
** Győződjön meg róla, hogy minden útvonal, ahol hitelesítésre van szükség, lefuttatja az "auth.session" néven deklarált köztes szoftvert.
* - Például:
*/
Route::group([
'prefix' => 'admin',
'as' => 'admin.',
'middleware' => [
'auth',
'auth.session',
// ...
],
], function () {
// ...
});
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment