how to multiple login in one application like admin and employee in laravel -
i have 2 protected areas in 1 application admin user can login in both areas user (admin) , employee. please give me suggestion question.
routes.php
route::get('employee/login', array( 'uses' => 'logincontroller@create', 'as' => 'login.create' )); route::post('employee/login', array( 'uses' => 'logincontroller@store', 'as' => 'login.store' ));
logincontroller.php
<?php namespace app\http\controllers; use app\http\requests; use app\http\controllers\controller; use view; use illuminate\http\request; use input; use auth; use config; use redirect; use app\employee; use db; use validator; class logincontroller extends controller { public function __construct() { config::set('auth.model', 'employee'); config::set('session.path', '/employee'); } public function create() { return view::make('employee.login'); } public function store() { if(auth::attempt(array('email' => input::get('email'), 'password' => input::get('password')))) { return redirect::intended('employee/home'); } return redirect::route('login.create') ->withinput() ->with('login_errors', true); } public function destroy() { log::logout(); return view::make('employee.destroy'); } }
login.blade.php
<html> <head> <link href='css/style.css' rel='stylesheet' type='text/css'> <link href='../css/style.css' rel='stylesheet' type='text/css'> </head> <body> <div class="head_text"> <h2 style="text-align:center;">employee login</h2> </div> <div class="login_table"> {!! form::open(array('route' => 'login.store')) !!} <h1 style="text-align:center;">login</h1> <p> {!! $errors->first('email') !!} {!! $errors->first('password') !!} </p> <p> {!! form::label('email', 'email address') !!} {!! form::text('email', input::old('email'), array('placeholder' => 'awesome@awesome.com')) !!} </p> <p> {!! form::label('password', 'password') !!} {!! form::password('password') !!} </p> <p> {!! form::submit('login') !!} </p> {!! form::close() !!} </div> </body> </html>
employee(model)
<?php namespace app\models;; use illuminate\database\eloquent\model; use illuminate\auth\authenticatable; use illuminate\auth\passwords\canresetpassword; use illuminate\auth\passwords\canresetpassword; use illuminate\contracts\auth\authenticable; use illuminate\auth\authenticable authenticabletrait; use illuminate\support\facades\auth; use illuminate\contracts\auth\authenticatable authenticatablecontract; use illuminate\contracts\auth\canresetpassword canresetpasswordcontract; class employee extends model implements authenticatablecontract, canresetpasswordcontract { use authenticatable, canresetpassword; protected $table = 'employee'; protected $fillable = [ 'firstname', 'lastname', 'email', 'birthdate', 'address', 'phone', 'mobileno', 'employeetype', 'partment', 'uniqueemployeeid', 'password', 'repassword' ]; public function setremembertoken($value) { $this->remember_token = $value; } public function getremembertokenname() { return 'remember_token'; } public function setpasswordattrribute($password) { $this->attributes['password']=hash::make($password); } public static $rules = array( 'firstname' => 'required', 'lastname' => 'required', 'email' => 'required|email', 'birthdate' => 'required', 'address' => 'required', 'phone' => 'required', 'mobileno' => 'required', 'employeetype' => 'required', 'department' => 'required', 'uniqueemployeeid' => 'required', 'password' => 'required', 'repassword' => 'required|same:password' ); public function getfullname() { return $this->firstname ; } }
auth.php
<?php return [ 'driver' => 'eloquent', 'model' => 'app\user', 'model' => 'app\employee', 'table' => 'users', 'table' => 'employee', 'password' => [ 'email' => 'emails.password', 'table' => 'password_resets', 'expire' => 60, ], ];
to achieve that, have go 1 of 2 approaches: rbac or multi authentication.
role-based access control
if user data (email, password, name,..) stored in 1 table, go rbac. rbac works assigning roles every user, have user admin or employee role. using middlewares, can restrict access protected areas based on roles of authenticated user. there's multiple documented laravel packages use achieve entrust or laravel-permissions
multi authentication
if user data isn't stored in 1 table, example have admin table , employee table. use multi authentication, can define multiple authenticatable models or user tables. can restrict access protected area based on authenticated user itself. this's built-in feature in laravel 5.2. laravel 5.1, use multiauth package.
Comments
Post a Comment