dimanche 29 octobre 2017

Login for multiple roles and permission

Hey I am making a project in which their are multiple users with different who are going to login. The users according to their role will be shown different pages. I have made a piece of code but whenever a user logins he/she can access the page of another user.

This is my login controller

 <?php

 namespace App\Http\Controllers;

 use Illuminate\Http\Request;
 use App\User;
 use Auth;

 class LoginController extends Controller
{
public function login(Request $request)
{


    if(Auth::attempt([

        'email' => $request->email,
        'password' => $request->password

    ]))
    {
        $user = User::where('email', $request->email)->first();

        if($user->is_admin())
        {
            return redirect()->route('dashboard');
        }

        return redirect()->route('home'); 
    }
    redirect()->back();
}

}

These are my routes

  <?php


  Auth::routes();

  Route::get('/home', 'HomeController@index')->name('home');
   Route::post('/login/custom',[
 'uses' => 'LoginController@login',
 'as' => 'login.custom'
  ]);
  Route::get('/login/custom',[
 'uses' => 'LoginController@login',
 'as' => 'login.custom'
 ]);
  Route::group(['middleware' => 'auth'], function(){

Route::get('/home',function(){

    return view('home');
})->name('home');

  Route::get('/dashboard',function(){

  return view('dashboard');
 })->name('dashboard');

});

And this is my model

 <?php

 namespace App;

 use Illuminate\Notifications\Notifiable;
 use Illuminate\Foundation\Auth\User as Authenticatable;

  class User extends Authenticatable
  {
   use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

public function is_admin(){

    if($this->admin)
    {
        return true;
    }
    return false;
}

}

Please tell me what I am doing wrong.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2idVGwo
via IFTTT

Aucun commentaire:

Enregistrer un commentaire