This Article help you to build a Login module in laravel5.2.Complete code and details are described here.
This is a module for superadmin login.code explains how a login module works in laravel.
routes.php
1 2 3 4 5 6 7 8 |
<?php Route::get('superadmin', 'SuperadminController@index'); Route::post('/superadmin/authenticate', 'LoginController@authenticate'); Route::group(['middleware' => ['auth']], function () { Route::resource('/superadmin/home', 'SuperadminController@home'); Route::resource('/superadmin/logout', 'LoginController@logout'); }); ?> |
Here i created the application inside a folder named “studentapplication”.if you type the url http://localhost/studentapplication/public/superadmin first it will execute the routes.php file. from there
it will redirect to the view page stored in the superadmin folder,actualy superadmin folder contains the blade files.this files saved in resources/views/superadmin.
view page will display one username,password and a login button.view files are listed as below.
superadmin.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<!DOCTYPE html> <html lang="en"> <head> <title>Super Admin Login</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Super admin login example using laravel</h2> <form method="POST" action="{{ url('superadmin/authenticate') }}"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="form-group"> <label for="username">User Name:</label> <input type="text" class="form-control" id="username" name="username" placeholder="Enter Username"> </div> <div class="form-group"> <label for="password">Password:</label> <input type="password" class="form-control" id="password" name="password" placeholder="Enter password"> </div> <div class="checkbox"> <label><input type="checkbox"> Remember me</label> </div> <button type="submit" class="btn btn-default">Submit</button> </form> </div> </body> </html> |
home.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html lang="en"> <head> <title>Super Admin Login</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <form method="POST" action="{{ url('superadmin/authenticate') }}"></form> <div class="container"> <h2>Welcome <?php echo Session::get('username')?></h2> <span><a href="{{url('superadmin/logout')}}">Logout</a></span> </div> </body> </html> |
Note:in the database password as stored as bcrypt hash format.its required for auth::attempt() function.
Database Details:Created a table called tbl_super_admin.you can see the sql files below.table contains five fields.
1.id-primary key
2.user_name
3.password
4.remember_token
5.updated_at
remember_token,updated_at are the required fields in laravel login.if we are using a table for login purpose then these two fields are
mandatory.
you can create the database table using the below query
tbl_superadmin.sql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
CREATE TABLE IF NOT EXISTS `tbl_super_admin` ( `id` int(11) NOT NULL, `user_name` varchar(100) NOT NULL, `password` varchar(100) NOT NULL, `remember_token` varchar(100) NOT NULL, `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; INSERT INTO `tbl_super_admin` (`id`, `user_name`, `password`, `remember_token`, `updated_at`) VALUES (1, 'superadmin', '$2y$10$hVu41hqUsV5olqpLZta2aOLLbOExbspxI7ZZSv/uytpB53VEkyAjm', 'hjigEGakStHpC7KmXc6H1a6w5aTZL74VdfxZVeH0IIdpHF9FB42M421y0nJW', '2017-03-29 09:30:23'); ALTER TABLE `tbl_super_admin` ADD PRIMARY KEY (`id`); ALTER TABLE `tbl_super_admin` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2; |
Controller functions:using two controllers for handling the login functionality and display the pages.SuperadminController,Logincontroller
SuperadminController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php namespace App\Http\Controllers; use App\Http\Requests; use Request; use App\Http\Requests\CreateStudentsRequest; use App\Http\Controllers\Controller; use Session; use Illuminate\Support\Facades\Input; use Auth; class SuperadminController extends Controller { public function index() { return view('superadmin.superadmin'); } public function home() { return view('superadmin.home'); } } ?> |
index function will display the view page.Login page for the application it contains username,password fields and one Login button
home fucntion will display the page after successfull login.
LoginController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?php namespace App\Http\Controllers; use App\Http\Requests; use Illuminate\Http\Request; use App\Http\Requests\CreateStudentsRequest; use App\Http\Controllers\Controller; use Session; use Illuminate\Support\Facades\Input; use Auth; class LoginController extends Controller { public function __construct() { if (\Request::is('superadmin/*')) { $this->loginUrls = array( 'login_url' => 'superadmin', 'role_type' => 'super_admin', 'home_url' => 'superadmin/home', ); } } public function authenticate(Request $request) { $attempt = Auth::attempt(['user_name' => $request->input('username'), 'password' => $request->input('password')]); Session::put('username', $request->input('username')); return redirect($this->loginUrls['home_url']); } public function login(Request $request) { if ($this->loginUrls['role_type'] == 'super_admin') { if (Auth::guest()) { return view('login.login', $this->loginUrls); } else { return redirect('/superadmin/home'); } } } public function logout() { if ($this->loginUrls['role_type'] == 'super_admin') { Auth::logout(); Session::forget('username'); } return redirect($this->loginUrls['login_url']); } } ?> |
Model:Created on model file SuperUser.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; class SuperUser extends Authenticatable { protected $table = 'tbl_super_admin'; protected $primaryKey = 'id'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'user_name', 'password', ]; public function getAuthPassword() { return $this->password; } } |
Apart from the model,view,routes,and controllers file few changes are required in the below files.
Config/auth.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?php return [ 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\SuperUser::class, ], ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], ], ]; ?> |
app/Http/kernel.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php namespace App\Http; use Illuminate\Foundation\Http\Kernel as HttpKernel; class Kernel extends HttpKernel { protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, ]; protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, ], 'api' => [ 'throttle:60,1', ], ]; protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, ]; } |
Middleware/Authenticate.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Auth; class Authenticate { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string|null $guard * @return mixed */ public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->guest()) { if ($request->ajax() || $request->wantsJson()) { return response('Unauthorized.', 401); } else { return redirect()->guest('superadmin'); } } return $next($request); } } ?> |