简体   繁体   English

Laravel Hash:检查总是返回假

[英]Laravel Hash:check always return false

I'm doing dd () Dump and Die and it returns me different hashs I don't know the reason here is my controller and my Seeder can't detect the error yet I'm learning how to handle the laravel creating my first api.我正在做 dd () Dump and Die 它返回给我不同的哈希我不知道这里的原因是我的 controller 和我的播种机无法检测到错误但我正在学习如何处理 laravel 创建我的第一个 controller . * *

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Auth;
use App\Hash;

class ApiLoginController extends Controller
{
    public function login(Request $request){
        $usuario = User::where('email', $request->email)->first();

        if($usuario && Hash::check($request->senha, $usuario->password))
        {
            return response()->json($usuario);
        }
        return response()->json(['messagem' => 'Erro']);

    }
}
<?php

use Illuminate\Database\Seeder;
use App\User;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {


        $user = new User;
        $user->name = 'Hernandes';
        $user->email = 'hernandes@gmail.com';
        $user->password = bcrypt('12345');
        $user->save();
    }
}

Hello I changed the code but it did not work I looked at the documentation but still not able to, the result still false, I wanted one more tip of what I can do, and thanks for the help您好,我更改了代码但它不起作用我查看了文档但仍然无法,结果仍然是错误的,我想要一个我能做的更多提示,感谢您的帮助

ApiLoginController ApiLoginController

<?php

    namespace App\Http\Controllers;

    use Hash;
    use App\User;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Auth;


    class ApiLoginController extends Controller
    {
        public function login(Request $request){
            $usuario = User::where('email', $request->email)->first();

            if($usuario && hash::check($request->senha, $usuario->senha)){
                return response()->json($usuario);
            }
            return response()->json(['messagem' => 'Erro']);

        }


    }

UsersTableSeeder用户TableSeeder

<?php

    use Illuminate\Database\Seeder;
    use App\User;

    class UsersTableSeeder extends Seeder
    {
        /**
         * Run the database seeds.
         *
         * @return void
         */
        public function run()
        {


            $user = new User;
            $user->name = 'Hernandes';
            $user->email = 'hernandes@gmail.com';
            $user->password = Hash::make('password');        
            $user->save();
        }



}

In you controller the following condition has some issues:在您 controller 中,以下情况存在一些问题:

if($usuario && hash::check($request->senha, $usuario->senha)){

  1. hash::check() should be Hash::check() hash::check()应该是Hash::check()
  2. You are using $usuario->senha .您正在使用$usuario->senha But in seeder you are saving the field as password .但是在播种机中,您将该字段保存为password Which is the correct one?哪个是正确的?
  3. The blade view or the request is not shown in the question.问题中未显示刀片视图或请求。 Make sure you are passing the password in the filed called senha .确保您在名为senha的字段中传递密码。 Otherwise, you won't get any value in $request->senha .否则,您将不会在$request->senha中获得任何价值。

Tip: Before checking hash you can see if they are containing the correct value using dd($request->senha, $usuario->senha);提示:在检查 hash 之前,您可以使用dd($request->senha, $usuario->senha);查看它们是否包含正确的值。 at first.首先。

You are bcrypting the value on save and checking a Hash value on login controller.您正在对保存值进行加密,并在登录 controller 时检查 Hash 值。 They're two different algorithms.它们是两种不同的算法。 See my comment on the original question for more information.有关更多信息,请参阅我对原始问题的评论。 Either change your user->save method to be password = Hash::make('userpassword') and check the hash in the login controller or decrypt the password in the login controller and leave your user save method the same. Either change your user->save method to be password = Hash::make('userpassword') and check the hash in the login controller or decrypt the password in the login controller and leave your user save method the same.

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

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