简体   繁体   English

我无法使用 Auth::attempt php laravel 登录

[英]I can not log in with Auth::attempt php laravel

I receive data from the login form, but for some reason it is not authorized.我从登录表单收到数据,但由于某种原因它没有被授权。 The data is correctly saved to the database.数据正确保存到数据库中。 I'm new to laravel and I don't understand at all why this is happening, but I want to know.我是 laravel 的新手,我完全不明白为什么会发生这种情况,但我想知道。 login.blade.php登录名.blade.php

<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{

    public function login(Request $request){
        if(Auth::check()){
            return redirect(route('user.private'));
        }

$formFields = $request->only(['email','password']);
        if (Auth::attempt($formFields)){

            return redirect(route('user.private'));
        }
        return redirect(route('user.login'))->withErrors([

            'email' => 'Bad'
        ]);

    }
}

LoginController.php:登录控制器.php:

<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{

    public function login(Request $request){
        if(Auth::check()){
            return redirect(route('user.private'));
        }

$formFields = $request->only(['email','password']);
        if (Auth::attempt($formFields)){

            return redirect(route('user.private'));
        }
        return redirect(route('user.login'))->withErrors([

            'email' => 'Bad'
        ]);

    }
}

web.php:网页.php:

<?php
Route::name('user.')->group(function (){
    Route::view('/private','private')->middleware('auth')->name('private');
    Route::get('/login',function(){
        if(Auth::check()){
            return redirect(route('user.private'));
        }
//        Проверка на авторизацию пользователя. Если уже авторизован, то редирект на страницу профиля
        return view('login');
    })->name('login');
    Route::post('/login',[\App\Http\Controllers\LoginController::class,'login']);
    Route::get('/logout',function(){
        Auth::logout();
        return redirect('/');
    })->name('logout');
    Route::get('/registration', function () {
        if(Auth::check()){
            return redirect(route('user.private'));
        }
        return view('registration');
    })->name('registration');
    Route::post('/registration',[\App\Http\Controllers\RegisterController::class,'save']);
});

From database enter image description here From startup/login enter image description here从数据库在此处输入图像描述从启动/登录在此处输入图像描述

Take a look at Laravel's documentation for this, it seems your code is a little different: https://laravel.com/docs/8.x/authentication#authenticating-users看看 Laravel 的文档,你的代码似乎有点不同: https ://laravel.com/docs/8.x/authentication#authenticating-users

As a cleanup note, I'd remove the Auth::check in your web.php as you have already included it in your login controller.作为清理说明,我会删除 web.php 中的 Auth::check,因为您已经将其包含在登录控制器中。

The password field value in the database can't be a clear one, you need to bcrypt it.数据库中的password字段值不能是明确的,需要bcrypt。

Got to the console and run php artisan tinker then put the result of bcrypt as the value of the field到控制台并运行php artisan tinker然后将 bcrypt 的结果作为字段的值

User::where('email','test1234@test')->update(['password' => bcrypt('test')]);

Now you can login with test1234@test as email and test as password现在您可以使用test1234@test作为电子邮件和test作为密码登录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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