Laravel Auth Filter and Logout - php

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!.

Related

Create job only if you are login Laravel

When you click on create, it redirects to the next page.
If the user is logged in, redirects it to jobs/create, and if it is not to /login.
<h3>Create</h3>
What would an if statement look like in this case?
It would be something like
<h3>Create</h3>
You do not need an if statement. Laravel has built in middleware to detect if a user is authenticated, and redirects them to the login if they are not.
Either add the Auth middleware to your route like this:
Route::get('create', 'JobController#create')->middleware('auth');
Or add it to a constructor function at the top of your controller to exclude anyone who isn't logged in from accessing any of the functions:
public function __construct()
{
$this->middleware('auth');
}
https://laravel.com/docs/7.x/authentication#protecting-routes

How to allow guest to access home page without login in Laravel 5.6

This has to be super simple, but I can't quite figure it out.
I want guest users (not logged in) to be allowed to go to the home page and not be redirected to the login page.
what is the trick?
Usually for authentification some middleware is used, so if you want the homepage to be accessible for unauthorized users you should remove auth middleware from homepage route.
You need to delete the middleware,
it could be in the routes file or in your Controller like this
routes:
Route::group(['middleware'=>['admin']], function(){
//Your routes
});
Controller:
public function __construct()
{
$this->middleware('auth');
}

Redirect to dashboard if user is loggedin in Laravel 4.2

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.

Laravel Using Sentry in Middleware

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');
}
}

Call function in the controller from a model - codeigniter

i have implement the php sdk for the facebook login in my project.. and all works with the login except the logout.
how can i write the code to call a function from the controller to destroy the session?
tried a lot of things but canĀ“t get understand how can i do this..
hope that somebody can help me with this.
my model with the logouturl is this:
'logoutUrl' => $this->facebook->getLogoutUrl()
and the function in the controller is this:
public function logout()
{
$this->CI->session->sess_destroy('logourUrl');
$this->load->view('home');
// do more thing you want to do such as redirect
}
Try adding the below in your controller
function logout()
{ $logout = $this->facebook->getLogoutUrl(array('next'=>'url to be redirected after logout'));
$this->facebook->destroySession();
$this->session->sess_destroy();
header("Location:$logout");
}
You don't need to load the view because facebook will automatically redirect to the given url after logout.

Categories