Created
March 4, 2019 18:12
-
-
Save AhsanAbrar/2288d03af80cbfd6781370e59cc5003d to your computer and use it in GitHub Desktop.
Laravel login with username, email and mobile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateUsersTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('users', function (Blueprint $table) { | |
$table->bigIncrements('id'); | |
$table->string('name'); | |
$table->string('username')->unique(); | |
$table->string('mobile')->unique(); | |
$table->string('email')->unique(); | |
$table->timestamp('email_verified_at')->nullable(); | |
$table->string('password'); | |
$table->rememberToken(); | |
$table->timestamps(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('users'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@extends('layouts.app') | |
@section('content') | |
<div class="container"> | |
<div class="row justify-content-center"> | |
<div class="col-md-8"> | |
<div class="card"> | |
<div class="card-header">{{ __('Login') }}</div> | |
<div class="card-body"> | |
<form method="POST" action="{{ route('login') }}"> | |
@csrf | |
<div class="form-group row"> | |
<label for="identity" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail / Username / Mobile #') }}</label> | |
<div class="col-md-6"> | |
<input id="identity" type="text" class="form-control{{ $errors->has('identity') ? ' is-invalid' : '' }}" name="identity" value="{{ old('identity') }}" required autofocus> | |
@if ($errors->has('identity')) | |
<span class="invalid-feedback" role="alert"> | |
<strong>{{ $errors->first('identity') }}</strong> | |
</span> | |
@endif | |
</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{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required> | |
@if ($errors->has('password')) | |
<span class="invalid-feedback" role="alert"> | |
<strong>{{ $errors->first('password') }}</strong> | |
</span> | |
@endif | |
</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> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Auth; | |
use Illuminate\Support\Str; | |
use Illuminate\Http\Request; | |
use App\Http\Controllers\Controller; | |
use Illuminate\Validation\ValidationException; | |
use Illuminate\Foundation\Auth\AuthenticatesUsers; | |
class LoginController extends Controller | |
{ | |
/* | |
|-------------------------------------------------------------------------- | |
| Login Controller | |
|-------------------------------------------------------------------------- | |
| | |
| This controller handles authenticating users for the application and | |
| redirecting them to your home screen. The controller uses a trait | |
| to conveniently provide its functionality to your applications. | |
| | |
*/ | |
use AuthenticatesUsers; | |
/** | |
* Where to redirect users after login. | |
* | |
* @var string | |
*/ | |
protected $redirectTo = '/home'; | |
/** | |
* Create a new controller instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
$this->middleware('guest')->except('logout'); | |
} | |
/** | |
* Get the login username to be used by the controller. | |
* | |
* @return string | |
*/ | |
public function username() | |
{ | |
$field = $this->guessField( | |
$identity = request('identity') | |
); | |
request()->merge([$field => $identity]); | |
return $field; | |
} | |
/** | |
* Guess the identity type. | |
* | |
* @param $identity | |
* @return string | |
*/ | |
protected function guessField($identity) | |
{ | |
if (ctype_digit($identity)) | |
return 'mobile'; | |
return Str::contains($identity, '@') ? 'email' : 'username'; | |
} | |
/** | |
* Validate the user login request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return void | |
* | |
* @throws \Illuminate\Validation\ValidationException | |
*/ | |
protected function validateLogin(Request $request) | |
{ | |
$this->validate( | |
$request, | |
[ | |
'identity' => 'required|string', | |
'password' => 'required|string', | |
], | |
[ | |
'identity.required' => 'Username or mobile # is required', | |
'password.required' => 'Password is required', | |
] | |
); | |
} | |
/** | |
* Get the failed login response instance. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Symfony\Component\HttpFoundation\Response | |
* | |
* @throws \Illuminate\Validation\ValidationException | |
*/ | |
protected function sendFailedLoginResponse(Request $request) | |
{ | |
throw ValidationException::withMessages([ | |
'identity' => [trans('auth.failed')], | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment