Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

490
Visualizações
Laravel : Redirect the user to the same page after login

I have a cart products page, if a person clicks on a product add to cart button they will be redirected to the login page.

After a successful login, I need to send the user back to same products page again.

I used the following way in login controller.

But it's not actually working the way i want. Means it redirect to the index page again, I have used the return redirect()->back(); also..but doesn't solve the problem

if (auth()->attempt(array('email' => $request->input('email'),
             'password' => $request->input('password')))){
 if(Auth::user()->name == 'Admin'){
                 return redirect()->to('home');
            }
            else{
           //     return redirect()->back();
                return redirect()->intended('/');
            }
     }

How to solve the issue?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

In this case back() and intended() will not work.

You can save last product page to the session. Very simplified example:

session(['last_product_page_id' => $product->id]);

And then use this data to redirect user after login:

if (session()->has('last_product_page_id')) {
    return redirect()->route('product', session('last_product_page_id'));
}
about 4 years ago · Santiago Trujillo Relatório

0

May be Mezenin Solution is right But i solve it other way. (I took the concept from above solution)

As I'm using Laravel 5.4 ..I'm giving solution based on recent file directory..

I have changed the showLoginFrom controller which is inside AuthenticateUsers trait to this :

  public function showLoginForm()
    {
        Session::put('url.intended',URL::previous());
        return view('auth.login');        
    }

And inside my login controller I have changed :

if (auth()->attempt(array('email' => $request->input('email'),
             'password' => $request->input('password')))){
 if(Auth::user()->name == 'Admin'){
                 return redirect()->to('home');
            }
            else{
            return Redirect::to(Session::get('url.intended'));
            }
     }

And that's how when a user logged in, he will be automatically redirected to the previous page..

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda