Support the ongoing development of Laravel.io →
posted 9 years ago
Authentication
Last updated 2 years ago.
0

Hi, You can override the built-in authorization and registration methods if you create them in your AuthController (App\Http\Controllers\Auth\AuthController.php). You can find them in AuthenticatesAndRegistersUsers.php (trait). Example: in your AuthController create custom postLogin method

public function postLogin(Request $request)
{
    //pass through validation rules
    $this->validate($request, ['email' => 'required', 'password' => 'required']);
		
    $credentials = [
        'email' => trim($request->get('username')),
        'password' => trim($request->get('password'))
    ];

    $remember = $request->has('remember');
    
    //log in the user
    if ($this->auth->attempt($credentials, $remember) {
        return redirect()->intended('/');
    }

    //show error if invalid data entered
    return redirect()->back()->withErrors('Login/Pass do not match')->withInput();
}
Last updated 9 years ago.
0

Hi! thanks for your response. generally I get the idea but now another error occured:

ReflectionException in RouteDependencyResolverTrait.php line 53: Class App\Http\Controllers\Auth\Request does not exist

I suppose I have to load that via use or adjust the namespace - I tried some back and forth but none worked. Here's the whole file now:

<?php 
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller {

	use AuthenticatesAndRegistersUsers;

	public function __construct(Guard $auth, Registrar $registrar)
	{
		$this->auth = $auth;
		$this->registrar = $registrar;

		$this->middleware('guest', ['except' => 'getLogout']);
		
	}

	//protected $redirectPath = '/dashboard';

	public function postLogin(Request $request)
	{
		//pass through validation rules
		$this->validate($request, ['username' => 'required', 'password' => 'required']);
	
		$credentials = [
			'username' => trim($request->get('username')),
			'password' => trim($request->get('password'))
		];
	
		$remember = $request->has('remember');
	
		//log in the user
		if ($this->auth->attempt($credentials, $remember)) {
			return redirect()->intended('/');
		}
	
		//show error if invalid data entered
		return redirect()->back()->withErrors('Login/Pass do not match')->withInput();
	}
}

I slightly adjusted it to actually use the username field from my edited login-form:

<input type="username" class="form-control" name="username" value="{{ old('username') }}">
Last updated 9 years ago.
0
use Illuminate\Http\Request;
0

ah! that's it! I owe you a beer ;-)

0

Sign in to participate in this thread!

Eventy

Your banner here too?

port22 port22 Joined 30 May 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.