简体   繁体   中英

Laravel 5.6 - redirect to route after login based on URL GET parameter

I've got a standard fresh install of Laravel 5.6.

For web users, I want them to be able to log in as normal at /login and have them redirected to their user dashboard - all well and good.

However I'm developing something in Electron, and I am wanting to scrape something from the Laravel site after log in and then I want to close the window, so, I was hoping there was a way to potentially pass a GET parameter such as /login?app=true which would take me to another route rather than the dashboard.

I wouldn't be able to set anything in the session or anything. The most I can do is manipulate the DOM of the login page in Electron.

Any ideas?

You can use authenticated() method in your LoginController;

public function authenticated(Request $request, $user)
{
  if($request->has('app')) {
     if($request->get("app")==false)
        return redirect("url");

     if($request->get("app")==true)
        return redirect("another-url");
  }

  return redirect("dashboard")
}

Firstly, I started off by changing the use AuthenticatesUsers trait call to use a custom created one, this was defined in the same namespace - of course I removed the use from the top of the class.

I kind of 'extended' the trait I guess and just listened for an input with a specific name, to determine which route to return. As for adding the input, I preloaded some JS within my Electron app when loading the window to append the input and its value.

<?php
namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;

trait AuthenticatesUsers
{
    use \Illuminate\Foundation\Auth\AuthenticatesUsers {
        \Illuminate\Foundation\Auth\AuthenticatesUsers::login as parentLogin;
    }

    private $newRouteUrl = '/newroute';

    public function login(Request $request)
    {
        $this->validateLogin($request);

        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        if ($this->attemptLogin($request)) {
            if ($request->input('newRouteIdentifierInput')) {
                return $this->sendNewRouteResponse($request);
            }
            return $this->sendLoginResponse($request);
        }

        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }

    protected function sendNewRouteResponse(Request $request)
    {
        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

        return $this->authenticated($request, $this->guard()->user())
            ?: redirect()->intended($this->newRouteUrl);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM