Custom Login

 <?php


namespace App\Http\Controllers\Auth;

use App\Model\UserLogin;
use Illuminate\Http\Request;
use Session;
use Illuminate\Support\Facades\Auth;
use App\Model\Users;

class AdminLoginController extends LoginController
{
    /*
    |--------------------------------------------------------------------------
    | Admin Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating admin users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    protected $redirectTo = '/admin/dashboard';

    /**
     * Set current Guard.
     *
     * @var string
     */
    protected $guard = 'admin';
    //protected $guard = 'web';

     /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //parent::__construct();
    }

    /**
     * Show the application's login form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showLoginForm()
    {
        if(!\Auth::guard('admin')->check()){
          
            return view('admin.auth.login');
        }
        return redirect(route('AdminDashboard'));
    }

     /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return \Auth::guard($this->guard);
    }

     /**
     * Get the needed authorization credentials from the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    protected function credentials(Request $request)
    {   
        return [
            'email' => $request->email,
            'password' => $request->password,
        ];
        
    }
    
    

    /**
     * The user has been authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function authenticated(Request $request, $user)
    {   
        if(\Auth::guard('admin')->check())
        {
            return redirect('admin/dashboard');
        }
        else
        {   
            $this->guard('admin')->logout();
            return redirect('admin')->with('error'trans('auth.failed'));
        }
        
    }

    /**
     * Send the response after the user was authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    protected function sendLoginResponse(Request $request)
    {  
        $request->session()->regenerate();
        $this->clearLoginAttempts($request);
     
       // Store login detail
        $userLogin = new UserLogin();
        $userLogin->userId = $this->guard('admin')->user()->id;
        $userLogin->loginStatus = 1;
        $userLogin->ipAddress =\Request::ip();
        $userLogin->platform = 'WEB';
        $userLogin->isLogin = 1;
        $userLogin->save();

        session()->put('lastLoginId',$userLogin->id);

        $lastLoginId = session()->get('lastLoginId');
        return $this->authenticated($request, $this->guard()->user())
            ?: redirect()->intended($this->redirectPath());
    }

    /**
     * Log the user out of the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function logout(Request $request)
    {

        $lastLoginId = session()->get('lastLoginId');

        $userLogin = UserLogin::find($lastLoginId);
        if(!empty($userLogin))
        {
            $userLogin->loginStatus = 0;
            $userLogin->save();
        }
        
        $this->guard('admin')->logout();

        $request->session()->invalidate();

        return $this->loggedOut($request) ?: redirect('/admin/login');
    }

    /**
     * Get the needed authorization credentials from the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    // protected function credentials(Request $request)
    // {
    //     //die('$request->password');
    //     return array_merge($request->only($this->username(), 'password'));
    // }
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserLoginsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_logins'function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('userId');
            $table->boolean('loginStatus')->default(0);
            $table->string('ipAddress');
            $table->string('platform');
            $table->boolean('isLogin')->default(0);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('user_logins');
    }
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

<?php

namespace App\Model;

class UserLogin extends CustomModel
{

    protected $fillable = [
        'userId',
        'deviceToken',
        'fcmToken',
        'deviceType',
        'isLogin'
    ];
}


Comments

Popular posts from this blog

API

Encryption and Decryption By PHP

Seeder