Ad

Where To Set Session In Laravel Using AuthenticatesAndRegistersUsers, ThrottlesLogins;

- 1 answer

I used use AuthenticatesAndRegistersUsers, ThrottlesLogins; to login in laravel , using Auth::user() login is successful but I want to set session using authentication is done. and I getting session value in my home page.

Ad

Answer

What Laravel version do you use?

In Laravel 5.1 or 5.0 you should edit:

vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php

Then you have to edit the postLogin method, inside this method, you can edit what ever.

This if is for Too many login attempts.

if ($throttles && $this->hasTooManyLoginAttempts($request)) {
        return $this->sendLockoutResponse($request);
}

If you have to put your code after the user has login correctly, you must edit this IF:

if (Auth::attempt($credentials, $request->has('remember'))) {

}

Example:

/**
 * Handle a login request to the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function postLogin(Request $request)
{
    $this->validate($request, [
        $this->loginUsername() => 'required', 'password' => 'required',
    ]);

    // If the class is using the ThrottlesLogins trait, we can automatically throttle
    // the login attempts for this application. We'll key this by the username and
    // the IP address of the client making these requests into this application.
    $throttles = $this->isUsingThrottlesLoginsTrait();

    if ($throttles && $this->hasTooManyLoginAttempts($request)) {
        return $this->sendLockoutResponse($request);
    }

    $credentials = $this->getCredentials($request);

    if (Auth::attempt($credentials, $request->has('remember'))) {
            \Session::flash('new_user', 0);
        return $this->handleUserWasAuthenticated($request, $throttles);
    }

    // If the login attempt was unsuccessful we will increment the number of attempts
    // to login and redirect the user back to the login form. Of course, when this
    // user surpasses their maximum number of attempts they will get locked out.
    if ($throttles) {
        $this->incrementLoginAttempts($request);
    }

    return redirect($this->loginPath())
        ->withInput($request->only($this->loginUsername(), 'remember'))
        ->withErrors([
            $this->loginUsername() => $this->getFailedLoginMessage(),
        ]);
}
Ad
source: stackoverflow.com
Ad