Today, We want to share with you Laravel 5.6 Prevent Block Multiple Login Of Same Credentials.In this post we will show you Laravel prevent multiple logins from same user, hear for how to prevent multiple login for same user in Laravel we will give you demo and example for implement.In this post, we will learn about Laravel 5.6 – Prevent Block Multiple Login Of Same Credentials with an example.
Laravel 5.6 Prevent Block Multiple Login Of Same Credentials
Contents
There are the Following The simple About Laravel 5.6 Prevent Block Multiple Login Of Same Credentials Full Information With Example and source code.
As I will cover this Post with live Working example to develop how to prevent multiple login for same user in Laravel, so the prevent multiple logins in Laravel website for this example is following below.
Step 1: Laravel Migration
Update and Add some fields in Members Table Migration:
use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateMembersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('members', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->string('session_id'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('members'); } }
//Laravel migration using following command php artisan migrate
//Create Laravel Auth: php artisan make:auth
Change in SigninController.php File:
app/Http/Controllers/Auth/SigninController.php
namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\AuthenticatesMembers; use Illuminate\Http\Request; use App\Http\Requests; use App\User; use DB; class SigninController extends Controller { use AuthenticatesMembers; /** * Where to redirect members after login. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } public function login(Request $request) { $this->validate($request, [ 'email' => 'required', 'password' => 'required', ]); $member = \DB::table('members')->where('email', $request->input('email'))->first(); if (auth()->guard('web')->attempt(['email' => $request->input('email'), 'password' => $request->input('password')])) { $new_sessid = \Session::getId(); //get new session_id after member sign in if($member->session_id != '') { $last_session = \Session::getHandler()->read($member->session_id); if ($last_session) { if (\Session::getHandler()->destroy($member->session_id)) { } } } \DB::table('members')->where('id', $member->id)->update(['session_id' => $new_sessid]); $member = auth()->guard('web')->member(); return redirect($this->redirectTo); } \Session::put('login_error', 'Sorry, Your email and password wrong!!'); return back(); } public function logout(Request $request) { \Session::flush(); \Session::put('success','Good Lcuk, you are logout Successfully'); return redirect()->to('/login'); } }
Include in app.blade.php File:
resources/views/layouts/app.blade.php
<script src="https://www.gstatic.com/firebasejs/4.9.1/firebase.js"></script> <script type="text/javascript"> var session_id = "{!! (Session::getId())?Session::getId():'' !!}"; var member_id = "{!! (Auth::member())?Auth::member()->id:'' !!}"; // Initialize Firebase var config = { apiKey: "firebase.api_key", authDomain: "firebase.auth_domain", databaseURL: "firebase.database_url", storageBucket: "firebase.storage_bucket", }; firebase.initializeApp(config); var database = firebase.database(); if({!! Auth::member() !!}) { firebase.database().ref('/members/' + member_id + '/session_id').set(session_id); } firebase.database().ref('/members/' + member_id).on('value', function(myssval) { var v = myssval.val(); if(v.session_id != session_id) { toastr.warning('Your Member account login from another device!!', 'Warning Alert', {timeOut: 3000}); setTimeout(function() { window.location = '/login'; }, 4000); } }); </script>
Run Your Project
php artisan serve http://localhost:8000/login
Angular 6 CRUD Operations Application Tutorials
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about Laravel 5.6 Prevent Block Multiple Login Of Same Credentials.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donβt forget to share.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.