Laravel redirect to optional page aftet logout - laravel

in laravel as we know we can change
protected $redirectAfterLogout = 'auth/login';
to redirect login after logout, but i have some pages and i would like to redirect to them for example redirect to pages/aboutUs or pages/contactUs or pages/home optionally and i cant change $redirectAfterLogout variable to them
how can i implementing this solution?

Just change
protected $redirectAfterLogout = 'auth/login';
but remember, that the action in controller that you redirect to, can not have in __constructor:
$this->middleware('auth');
Check also if your web middleware does not require form user to be logged in.
Then redirection will work.
Good Luck!

Related

Previous URL with subdomain

I have subdomain account.asu.dev.The controller has the code:
public function __construct()
{
$this->middleware('auth');
}
This construct directs the user to the login page. The problem is that _previous takes auth.asu.dev value, but should account.asu.dev. For this reason, the user is taken to the main page, but had to return to the subdomain. How to fix it?
Laravel 5.4
If you are manually loging in users, you could do at the end of the login
return redirect()->intended('home');
This will redirect them where they were going or simply to the home page. Or simply replace home with whatever page you want them to go after the login works.

Laravel 5.3 determine redirection based on factors

I realize that I can change where my user is directed after login by changing
protected $redirectTo = '/home';
in the LoginController.php
However, I want to check if there are any items in a shopping cart and if so, direct the user to the checkout page. I may also check to see if they have purchased a service that is active and direct them to the dashboard, then anyone else to the homepage.
Anyone know how to apply some logic to the redirection?
You can override sendLoginResponse() method in app\Http\Controllers\Auth\LoginController.php to perform the check and redirect to whatever route you want.
Original sendLoginResponse() method is in vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php. Copy-paste it to LoginController.php and then work with it. Do not change anything in an original trait.

how redirect back after login and logout - laravel 5

Now after login or logout I am forwarded to the homepage.
in this article have read that
I can define redirection path in Auth Controller
protected $redirectPath = '/';
I can also change this line in Middleware RedirectIfAuthenticated.php:
return new RedirectResponse(url('/'));
But my goal is to
either login by AJAX
or, after login or logout, redirect back to the page where I have pressed the button.
Both answers highly appreciated.
Thx.
To redirect to the previous page you can use back() like so:
return redirect()->back();
You can read more in the Laravel Docs.
How about using something like this ?
if(Session::get('redirect') == null) {
Session::flash('redirect', 'ok');
return redirect()->back();
}
For anyone who have to redirect user back to the page where they clicked the button for a protected page that redirected back to login. Here is my simple approach to it.
First on your nav, menu or whatever, your href value would be something like this:
Some Page
What this simply do is, if your user is not logged in, its uses the lgin page as url, which simply cut off the need to redirect and adds the current page they are in which would be the page they would redirected back after login as a get param to the login url. Unlike using sessions to retrieve http_referer which would be wrong if user were redirected back to login due to validation error, this would stick around on your url.
Example of login url may be http://localhost:2008/signin?redirect_to=protected-page
Then simply after login you would just check if get param redirected_to is available then redirect user back to that page or a default page if its not available as a get a param, probably your home or dashboard maybe, an example below might help.
$data = $request->all();
$referer = $data['redirect_to'] ? url($data['redirect_to']) : url('/');
if($allowAccess) return redirect($referer);
I hope this helps someone!

Codeigniter redirect to variable url

I'm working on a project in codeigniter. What I have done until now is that if you type for instance
http://localhost/ci_project/dashboard
it will redirect to a page called: restricted.
However, what I want to do is that when you type dashboard on the url, codeigniter will redirect to login controller. If the login is successfull, go to the dashboard or whatever controller that the user added into the URL.
Basically the idea is to get a variable from the URL given by the user.
Thanks a bunch!
You can use the redirect function to send your users to any page you like:
redirect($URL, 'refresh');
You can read more about this function here (at the bottom of the page:)
http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
Place an if statement in the target controller function (I'm assuming the index() function of the Dashboard class) to test if the user is logged in, then redirect as needed.

Logout Link not working

So I am trying to click a logout link and end the session, and it doesnt seem to be working.
Link is located here:
application -> views-> modules -> header.php
like looks like this:
Logout
in the admin controller I have the function:
public function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('admin', 'refresh');
}
My routes files for admin look like this:
$route['admin/(:any)'] = 'admin/view/$1';
$route['admin'] = 'admin';
Admin views folder is located: views -> admin
Question: When I click on my logout link it does nothing. Like in my admin if there is no session it gets redirected to a login screen. That works but when I log out if it was redirecting to the admin page which is suppose to redirect to the login page if there is no session why is nothing happening?
Why are you redirecting to admin on logout? Just out of curiosity.
Try this, it sounds like your link itself is the issue. Using the base_url() means you don't have to worry about relative urls getting messed up depending on the depth you are in the site.
Logout
$route['admin/(:any)'] = 'admin/view/$1';
This code is redirecting admin/logout to admin/view/logout
So, like you said, adding $route['admin/logout'] = 'admin/logout'; near the top of your routes should take you to the correct page

Resources