Monday, 8 October 2018

How to login with username, email and mobile no?


checkstatus is a helper
<?php
namespace App\Http\Middleware;

use Closure;
use Auth;
use Session;

class CheckStatus
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        if(Auth::check() && Auth::user()->status != '1'){
            Auth::logout();
            $request->session()->flash('alert-danger', 'Your Account is deactivated.');
            return redirect('/login')->with('erro_login', 'Your error text');
        }
        return $response;
    }
}
**********************************************************************************
Add below lines in login controller.

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use App\User;

class LoginController extends Controller


use AuthenticatesUsers;
protected $redirectTo = RouteServiceProvider::HOME;


public function __construct()
    {
         $this->middleware(['guest','checkstatus'])->except('logout');
    }
   
public function login(Request $request)
   {     
         $email = $request->email;
         if(is_numeric($email))
         {
            $auth = Auth::guard('web')->attempt(['mobile'=>$email,'password'=>$request->password]);
         }
         elseif (filter_var($email, FILTER_VALIDATE_EMAIL))
         {
            $auth = Auth::guard('web')->attempt(['email'=>$email,'password'=>$request->password]);
          }
         else
         {
               $auth = Auth::guard('web')->attempt(['name'=>$email,'password'=>$request->password]);
          }

         if($auth)
         { 
           return redirect()->route('home');         
                  $request->session()->put('email', $email);
          } else {
              session()->flash('login', 'Either Email or Password is incorrect.');
              return redirect()->route('login');
          }

      }

public function getLogin()
    {
          if(Auth::guard('web')->check()) {       
           return redirect()->route('login');
          }
          return view('auth.login');
    }
}

Note: active field should be there in user table with int(1). 
 
Login form

<div class="flash-message">
@if(Session::has('login'))
<p class="alert alert-danger" role="alert">{{ Session::get('login') }} <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a></p>
@endif
</div>           

<div class="flash-message">
@foreach (['danger', 'warning', 'success', 'info'] as $msg)
@if(Session::has('alert-' . $msg))
<p class="alert alert-{{ $msg }}">{{ Session::get('alert-' . $msg) }} <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a></p>
@endif
@endforeach
</div>

<form method="POST" action="{{ route('login') }}">
@csrf
<div class="form-group row">
  <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('Name/Mobile/E-Mail') }}</label>
  <div class="col-md-6">
    <input id="email" type="text" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
    @error('email') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div>
</div>
<div class="form-group row">
  <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
  <div class="col-md-6">
    <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password">
    @error('password') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div>
</div>
<div class="form-group row">
  <div class="col-md-6 offset-md-4">
    <div class="form-check">
      <input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>
      <label class="form-check-label" for="remember"> {{ __('Remember Me') }} </label>
    </div>
  </div>
</div>

<div class="form-group row mb-0">
  <div class="col-md-8 offset-md-4">
    <button type="submit" class="btn btn-primary"> {{ __('Login') }} </button>
    @if (Route::has('password.request')) <a class="btn btn-link" href="{{ route('password.request') }}"> {{ __('Forgot Your Password?') }} </a> @endif </div>
</div>
</form>

No comments:

Post a Comment

Machine Learning - Potato Leaf Disease Prediction

Step 1: import numpy as np import pandas as pd import splitfolders import matplotlib.pyplot as plt import tensorflow as tf from tensorflow i...