I'm developing website with Laravel framework and Sentinel
I install Sentinel and it's work fine with login and user group data, but when i create my project i don't know how use it to differentiate user who has login as admin or not login.. i follow the indstruction code like this in controller:
public function dashboard()
{
$this->middleware('sentry.member:Admins');
return view('admin.dashboard');
}
But when i access the dashboard without login, it's still open dashboard.. how to fix it? how to redirect page to login if i'm not login yet?
In laravel to check a user while he is login or not login we do like this off-course this is works when we have installed sentry in laravel 5 framework.
public function dashboard(){
//already logged in go to dashboard or else login
if(Sentry::check()){
return Redirect::to('/admin.dashboard');
}else{
return Redirect::to('/login');
}
}
Related
I am using Laravel make:auth command and email verification of user. Now facing an issue that when any one visiting my web site the authentication will raise and redirect user to login page if the user not register it should register first and verify email before starting to visit my web site. I am wanted to show all my routs to any one without login or without registration. And also i need to verify user email when they register. it showing the routs to only registered and login users, but I am wanted to show every one.
public function __construct()
{
$this->middleware(['auth' => 'verified']);
}
after verifying email it shows the routs.
if I comment this code it will work fine but also i need to verify email.
this depends on how protective are you with the data to be shown to the visitors.
one method you can try is to use no middleware for those routes you want all of the viewers to visit and use a simple if else condition to show the respective view:
public function __construct()
{
// no middleware
}
public funtion index() {
if(Auth::check()) {
return view('logged.blade.php'); // for logged in users
}
return view('general.blade.php'); // for all visitors
}
I had implemented entrust for roles and permissions. I have 3 Roles, super-admin, admin and customer.
Super Admin has access to Web-app (eg. www.myurl.com)
Admin has access through api only i.e. mobile app (eg. www.myurl.com/api/login) via api.php route
customer had access through api i.e. mobile app
Now, I found a bug that when admin tries to login via www.myurl.com.login with his credentials he is allowed to log in!!!
On further investigating, I found that I need to change the login method and provide role check while login, but I'm unable to get through. I changed the login function as below, but still admin and customers are able to login!!
public function login(Request $request)
{
$this->validateLogin($request);
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
//I updated the following code of default login function.
$checkAdmin = $this->attemptLogin($request);
$isAdmin = Auth::user();
if ( $checkAdmin && $isAdmin->hasRole('super')) {
//With super-admin if I do dd('hi') here, I am getting control
return $this->sendLoginResponse($request);
}
//But for other roles, it is directly taking them to the super-admin (home) page!!
.
. //Rest of the login function...
I tried to make dd(1) to know the flow, but for super-user I got dd response while for other user, it was not going in that block and redirecting non-super-admin roles to home page!!
I am using Laravel 5.4 and entrust package for Roles.
I have implemented laravel 5.2 multi authentication.I have two guard web(default) and admin(created new). I have logged out user using Auth::logout() but I am not able to logout admin.
I tried to logout with Auth::guard('admin')->logout() and Auth::guard('admin')->user()->logout(). But its not working.
Try below code for logout method:
App/Http/Controllers/LoginRegisterCtrl.php
public function logout()
{
Auth::guard('web')->logout();
Auth::guard('admin')->logout();
}
Above code should work for you.
class DashboardController extends BaseController {
protected $layout = "layouts.dashboard";
public function __construct(){
//$this->beforeFilter('csrf', array('on'=>'post'));
$this->beforeFilter('auth', array('only'=>array('getIndex')));
}
public function getIndex(){
$this->layout->content= View::make('dashboard.index');
}
}
Above given code is my dashboardController. It is ok when i login and it redirects to dashboard. When the user is in dashboard, it means the user is authenticated. Now in the url bar, i hits the url of login. in my case it is
http://localhost:8000/users/login
now it redirects to login even the user is logged in. Now when i want to know how we can redirect to dashboard authomatically if the user is logged in.
I am new to laravel 4.2. I hope you will guide me. Im lost in this section
You can try to put this into your getIndex() function:
if(Session::has('login_parameters')) return Redirect::to('foo/dashboard');
I use this code in Laravel 3, hopefully it works in laravel 4.2 as well.
I am developing a website in laravel. I am facing a problem with the auth filter and the logout method.
In my home controller I want to apply the auth filter for some of the methods as follows.
public function __construct()
{
parent::__construct();
$this->filter('before',array('auth'))->except(array('index','view'));
}
I have action_logout as follows.
public function action_logout()
{
Auth::logout();
return Redirect::to("/home/index");
}
When I have logged in and trying to logout, i am not being able to access the logout action. I tried echoing something from the inside of the action but it didnt work.
changing to this..
$this->filter('before',array('auth'))->except(array('index','view','logout'));
works. Logically I should be able to logout only when i have logged in. But applying the filter above m not being able to access the logout action after loggin in. Help!.