In laravel when user is logged in and hit /login it redirects to root homepage - laravel

In my laravel site, users can log in.
When a user is logged in, and hit /login, it redirects to root homepage at /.
I want this redirect to hit /dashboard instead of /.
The routes were created using make:auth.
This is a problem when im using WebView in my android app. The standard page in the android WebApp is /login - but if the user is logged in and then exits the app and open it again, they hit / instead of fx /dashboard.
How do I redirect users from /login to /dashboard, if they hit /login while already logged in?

How do I redirect users from /login to /dashboard, if they hit /login while already logged in?
At app/http/Middleware/RedirectIfAuthenticated.php :
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/dashboard'); // redirect path wherever to redirect users when they already login
}
return $next($request);
}

In your LoginController, set :
protected $redirectTo = '/dashboard';

Related

Return to calling page instead of dashboard after Login (Laravel 8)

In my Laravel 8 Application link to login route is provided on different locations, i.e., user can login from multiple pages. By default, after login user is redirected to the dashboard. I am looing for a way to return the user to same page from where he clicked on the login button.
I am using Laravel with Jetstream and Livewire. The documentation says that I can do this by changing "public const HOME = '/ dashboard';" to the desired destination, but in my case the destination can be more than a single url.
I tried 'redirect()->back()' in place of '/ dashboard' but it does not work. Please help what change is required.
Save previous url when you redirect user to login page and after authentication of user redirect him on same page with the session you saved. Try this:
public function showLoginForm()
{
session(['link' => url()->previous()]);
return view('auth.login');
}
protected function authenticated(Request $request, $user)
{
return redirect(session('link'));
}

On Google login redirect to https://myaccount.google.com/ when multiple Google account is logged in at the same time in browser - Laravel

I am tired with Google login, when single Gmail account logged in browser everything fine, but when two or more Gmail account logged in browser then login successfully but redirect in the google account setting (url:https://myaccount.google.com), here is my controller code. I want to redirect to back page.
public function redirect()
{
return Socialite::driver('google')->redirect();
}
public function callback()
{
$googleUser = Socialite::driver('google')->user();
$existUser = User::where('email',$googleUser->email)->first();
if($existUser) {
Auth::loginUsingId($existUser->id);
}
else {
$user = new User;
$user->first_name = $googleUser->name;
$user->email = $googleUser->email;
$user->google_id = $googleUser->id;
$user->password = bcrypt(rand(1,10));
$user->save();
Auth::loginUsingId($user->id);
}
return back();
}
Redirect to this page, I want to redirect back page
This happens because of return back(); since your server is redirecting back to Google through the HTTP Referer header. This header is unreliable, please do not use it.
Instead, before login with google, save the current URL in the user session and after the login callback, redirect to that URL. In this way, you will be able to bring the user back to the original page.

How to validate routes if a user is admin or not?

//This is the middle ware
public function handle($request, Closure $next)
{
if(auth()->user()->isAdmin()) //isAdmin is a function in the User model which checks if the user is admin or not
{
return redirect('/admin');
} else {
return redirect('/home');
}
return $next($request);
}
//I already registered this middleware in kernel as well as verifyUser
Route::middleware(['auth', 'verifyUser'])->group(function() {
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/admin', 'AdminController#index')->name('admin');
Route::get('/users/profile', 'UserController#view')->name('users.view-profile');
Route::get('/users/edit_profile', 'UserController#edit')->name('users.edit-profile');
});
Th main problem here is it shows this error in the browser
The page isn’t redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept cookies.
You're telling Laravel to redirect admins to /admin, and non-admins to /home.
However, you've made /admin and /home subject to that middleware, too, so when the user gets to /home it redirect them to /home again (and again, and again, and again, forever).
You likely need two changes:
A new middleware, applied only to admin routes, that only redirects non-admins away from those routes.
Put your home/admin logic as a one-off post-login step instead of on every pageview. See the path customization section of the Authentication docs.

Manually setting url.intended and wrong browser history

Based on a eloquent created event I set url.intended in the session to a special page.
$this->request->session()->put('url.intended', '/my-special-page');
So when a user creates a row on a specific table (via a form), and is not logged in, it redirects them to login (after middleware) and upon successful login, redirects them to /my-special-page.
All of this is working, but when I create the row, login, get redirected to /my-special-page and hit back, I'm presented with the page users would see if they login normally.
Why is this happening and how can, if the user hits back, got back to the page they were previously on?
What is happening:
form submit -> login -> special-page -> user hits back -> normal page for logged in users
What should be happening:
form submit -> login -> special-page -> user hits back -> form page
You can achieve what you want by editing RedirectIfAuthenticated middleware:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
return $next($request);
}
Just edit the redirect line so it becomes return redirect('/my-special-page');

user already logged in but it try to open login link then how to redirect specified path in laravel

http://localhost/admin/login
I have logged in then I am redirecting to
localhost/admin/
Now I am open localhost/admin/login
then I want to redirect on
localhost/admin/dashboard not on
localhost/admin/
You need to put it in your auth controller.
protected $redirectTo = '/admin/dashboard';
You can use like this,
header('Location: http://www.example.com/');
You have to check if the user is logged :
if (Auth::check()) {
return Redirect::to('localhost/admin/');
}else{
log-in part
}
You have to simply check for the that particular user is logged into system using below code -
if (Auth::check()) {
return Redirect::to('admin/dashboard');
}else{
return Redirect::to('login/admin');
}
If user is logged in then redirect to dashboard of admin &
If user isn't logged in then redirect to login blade of the project.

Resources