Hello buddies ,
Coming back after the long gap , here i am providing details for how to create a custom login in latest laravel 5.8 .
After installing your project , Create a auth using artisan commands . Here i am giving example for admin prefix routs
Model : User.Php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Laravel\Passport\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements MustVerifyEmail
{
use HasApiTokens, Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
?>
Routes.Php
Route::prefix('/admin')->group(function () {
Route::get('users/login', 'ADMIN\UsersController@login');
Route::get('users/logout', 'ADMIN\UsersController@logout');
Route::get('users/dashboard', 'ADMIN\UsersController@dashboard');
Route::post('users/login', ['as'=>'users/login','uses'=>'ADMIN\UsersController@loginPost']);
});
Http/Controller/ADMIN/UsersController.php
<?php
namespace App\Http\Controllers\ADMIN;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class UsersController extends Controller
{
use AuthenticatesUsers;
public function __construct()
{
// $this->middleware('auth', ['except' => ['dashboard']]);
// $this->middleware('auth',['except' => ['login','loginPost']]);
$this->middleware('auth', ['only' => ['dashboard']]);
}
protected $redirectAfterLogout = 'admin/users/login';
protected $loginRedirect = 'admin/users/dashboard';
public function login()
{
if (Auth::check())
{
return redirect('admin/users/dashboard');
}
return view('admin/users/login');
}
public function loginPost(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required',
]);
if (auth()->attempt(['email' => $request->input('email'), 'password' => $request->input('password')]))
{
$user = auth()->user();
return redirect(property_exists($this, 'loginRedirect') ? $this->loginRedirect : '/')->with('success','Logged in successfully');
}else{
return back()->with('error','Invalid User Name and Password');
}
}
public function dashboard()
{
return view('admin/users/dashboard');
}
public function logout()
{
Auth::logout();
return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/')->with('success','Logged out successfully');
}
}
?>
In this controller i have created a login method and login post method . After login it will directs to dashboard page .
App/Http/Middleware/Authenticate.php
Overwrite this method
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
Session::flash('error', 'You are not authorised to access this page. please login here');
return url('admin/users/login');
}
}
Also on the top paste this : Use Session .
Hope its easy to create blade files using this controller . hope you enjoyed this tutorial .
Coming back after the long gap , here i am providing details for how to create a custom login in latest laravel 5.8 .
After installing your project , Create a auth using artisan commands . Here i am giving example for admin prefix routs
Model : User.Php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Laravel\Passport\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements MustVerifyEmail
{
use HasApiTokens, Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
?>
Routes.Php
Route::prefix('/admin')->group(function () {
Route::get('users/login', 'ADMIN\UsersController@login');
Route::get('users/logout', 'ADMIN\UsersController@logout');
Route::get('users/dashboard', 'ADMIN\UsersController@dashboard');
Route::post('users/login', ['as'=>'users/login','uses'=>'ADMIN\UsersController@loginPost']);
});
Http/Controller/ADMIN/UsersController.php
<?php
namespace App\Http\Controllers\ADMIN;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class UsersController extends Controller
{
use AuthenticatesUsers;
public function __construct()
{
// $this->middleware('auth', ['except' => ['dashboard']]);
// $this->middleware('auth',['except' => ['login','loginPost']]);
$this->middleware('auth', ['only' => ['dashboard']]);
}
protected $redirectAfterLogout = 'admin/users/login';
protected $loginRedirect = 'admin/users/dashboard';
public function login()
{
if (Auth::check())
{
return redirect('admin/users/dashboard');
}
return view('admin/users/login');
}
public function loginPost(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required',
]);
if (auth()->attempt(['email' => $request->input('email'), 'password' => $request->input('password')]))
{
$user = auth()->user();
return redirect(property_exists($this, 'loginRedirect') ? $this->loginRedirect : '/')->with('success','Logged in successfully');
}else{
return back()->with('error','Invalid User Name and Password');
}
}
public function dashboard()
{
return view('admin/users/dashboard');
}
public function logout()
{
Auth::logout();
return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/')->with('success','Logged out successfully');
}
}
?>
In this controller i have created a login method and login post method . After login it will directs to dashboard page .
App/Http/Middleware/Authenticate.php
Overwrite this method
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
Session::flash('error', 'You are not authorised to access this page. please login here');
return url('admin/users/login');
}
}
Also on the top paste this : Use Session .
Hope its easy to create blade files using this controller . hope you enjoyed this tutorial .
Laravel 5.8 Custom Login
Reviewed by AD
on
April 24, 2019
Rating:
No comments: