LoginController 未对用户进行身份验证

我首先创建了一个注册和登录,一切都很顺利,一旦我完成了我的注册,登录将无法验证它总是说无效数据,相信我有一些有效数据它只是不起作用


我尝试将凭据从“密码”更改为“密码”(用户名相同),但仍然没有任何效果


LoginController,同样在你问我之前,我确实将它们与 web.php 链接到登录表单,并且我确实为令牌添加了 @csrf,所以一切都应该顺利进行


namespace App\Http\Controllers;


use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;

use Illuminate\Support\Facades\DB;

use Illuminate\Support\Facades\Input;


use App\User;


class LoginController extends Controller {

    /**

     * Handle an authentication attempt.

     *

     * @param  \Illuminate\Http\Request $request

     *

     * @return Response

     */

    public function authenticate(Request $request){


        $username = $request->input('username');

        $password = $request->input('password');


        $credentials = ['username' => $username, 'password' => $password];




        if (Auth::check()) {

            return redirect('/register');

        } else {


            if (Auth::attempt($credentials)) {


                // Authentication passed...

                $request->session()->flash('loginsuccess', 'loginsuccess');

                return redirect('/');

            } else {

                $request->session()->flash('loginfailed', 'loginfailed');

                return redirect('/');

            }

        }

    }

}

我希望用户在登录后进行身份验证。(PS 我是 Laravel 的新手)


青春有我
浏览 200回答 3
3回答

慕尼黑5688855

这是自定义身份验证吗?如果是这样,我怀疑您将散列密码存储在寄存器中,但未在登录时对其进行散列,这意味着密码不匹配。php artisan make:auth如果您还没有使用 Laravel,我建议您使用它。

繁星点点滴滴

我要检查几件事,在您注册新用户后密码是否被加密?如果打印凭据会发生什么?public function authenticate(Request $request){    $username = $request->input('username');    $password = $request->input('password');    dd($password);    dd($username);// remove the first dd after your check to see the username output此外,这个逻辑对我来说没有意义:    if (Auth::check()) {        return redirect('/register');    ...如果用户已经通过您的应用程序的身份验证,为什么要重定向用户?您应该将他重定向到主页,如下所示:    return redirect('/home'); // or ('/') depends on your routes此外,Laravel 提供了一个简单的身份验证脚手架,例如,您可以创建一个新项目laravel new project设置数据库并运行以下命令,php artisan make:auth这将为您构建登录和注册逻辑。

慕哥9229398

检查 config/auth.php 并确保您已设置用户提供程序: /*    |--------------------------------------------------------------------------    | User Providers    |--------------------------------------------------------------------------    |    | All authentication drivers have a user provider. This defines how the    | users are actually retrieved out of your database or other storage    | mechanisms used by this application to persist your user's data.    |    | If you have multiple user tables or models you may configure multiple    | sources which represent each model / table. These sources may then    | be assigned to any extra authentication guards you have defined.    |    | Supported: "database", "eloquent"    |    */    'providers' => [        'users' => [            // Make sure these values are correct            'driver' => 'eloquent',            'model' => App\Models\User::class,        ],您是否创建了自定义 RegisterController?如果是这样,请确保在创建模型时对用户的密码进行哈希处理:$user = User::create([    'username' => $input['username'],    'password'=> Hash::make($input['password']),]);我将从这两个项目开始。如果问题仍然存在,您可能还希望在问题中包含错误消息以帮助追踪问题。
打开App,查看更多内容
随时随地看视频慕课网APP