Laravel登录问题(自定义登录)

时间:2017-09-02 11:43:06

标签: php laravel

我已经使用Laravel进行了自定义登录和注册过程。注册工作正常,但登录无效

用户模型(User.php)

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
     ''
    ];


    public function post(){
        return $this->hasMany('\App\Blog');
    }
}

我的UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use \App\User;

use Auth;


class UserController extends Controller
{
    public function register(Request $request){

        $this->validate($request,[
            'name'=>'required',
            'email'=>'required|unique:users|email',
            'password'=>'required'

            ]);
        User::create($request->all());
        return redirect('/')->with('msg','Registration Successful');


    }

    public function login(Request $request){

        $this->validate($request,[
            'email'=>'required|email',
            'password'=>'required'
            ]);

        if (Auth::attempt(array(
                'email' => $request->email,
                'password' => $request->password 
        ))){

            return redirect('/')->with('msg', 'Success');
        } else {
            return redirect('/login')->with('msg','Invalid Login');
        }




    }
}

表: Table Screenshot here 我尝试了所有可能的方法来解决这个错误,但我无法得到任何适当的解决方案。如果注册工作正常,为什么不登录过程?

1 个答案:

答案 0 :(得分:0)

您需要对密码进行哈希处理,以便可以在代码中添加这一行,并在创建语句附近更新代码

$attributes->password = Hash::make($attributes->password);
$attributes->password = Hash::make($attributes->password);
User::create($attributes);

此后,您的身份验证将生效