Laravel 7 custom authentication does not work

Viewed 307

I am trying make custom authentication in a Laravel 7 application. Referring to official documentation I should add a public function authenticate() in LoginController, so I tried it:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    /**
     * Handle an authentication attempt.
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return Response
     */
    public function authenticate(Request $request)
    {
         eval(\Psy\sh()); //console debug line

        $credentials = $request->only('email', 'password');

        if (Auth::attempt($credentials)) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
    }
}

But it seems the function is not running on login. I tested by adding debug line eval(\Psy\sh());, but it just not running. Any idea how to make the function run?

2 Answers

I think that older subversions of Laravel 7 doesn't have authenticated method but with Laravel 7.30.6, you can replace the attemptLogin method in your LoginController like this:

public function attemptLogin(Request $request)
{
    $credentials = $request->only('email', 'password');
    return Auth::attempt($credentials, $request->filled('remember'));
}

or if you want to specify additional conditions you can replace the attemptLogin like this:

public function attemptLogin(Request $request)
{
    $credentials = $request->only('email', 'password');
    return Auth::attempt(['email' => $credentials['email'], 'password' => $credentials['password'], 'role_id' => 1], $request->filled('remember'));
}

the latter according to the documentation: Manually Authenticating Users in Laravel 7.x

Error is here...

public function authenticate(Request $request)

Laravel 7.x version builtin Authentication not contain method authenticate inside LoginController.php

so please replace it with authenticated as below.

public function authenticated(Request $request)
{
    $credentials = $request->only('email', 'password');
    if (Auth::attempt($credentials)) {
        return redirect()->intended('dashboard');
    }
}
Related