Laravel 5.6 Prevent Block Multiple Login Of Same Credentials – Laravel

Laravel 5.6 Prevent Block Multiple Login Of Same Credentials – Laravel

In this post we will give you information about Laravel 5.6 Prevent Block Multiple Login Of Same Credentials – Laravel. Hear we will give you detail about Laravel 5.6 Prevent Block Multiple Login Of Same Credentials – LaravelAnd how to use it also give you demo for it if it is necessary.

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

There are the Following The simple About Laravel 5.6 Prevent Block Multiple Login Of Same Credentials Full Information With Example and source code.

Another must read:  Laravel 6 CRUD Tutorial With Example from scratch

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 IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

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	

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

//Create Laravel Auth:
php artisan make:auth	

Change in SigninController.php File:

app/Http/Controllers/Auth/SigninController.php

namespace AppHttpControllersAuth;

use AppHttpControllersController;
use IlluminateFoundationAuthAuthenticatesMembers;
use IlluminateHttpRequest;
use AppHttpRequests;
use AppUser;
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 :

Another must read:  PHP Laravel 6 Global Variable Example

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 onlinecode 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.

Hope this code and post will helped you for implement Laravel 5.6 Prevent Block Multiple Login Of Same Credentials – Laravel. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

2  +  8  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US