laravel中的 Hash::make與Hash::check做登陸驗證和密碼加密

1.使用前先引用

use Illuminate\Support\Facades\Hash;

2.加密

$pwd = Hash::make($request->newPassword);    //加密存儲

3.匹配

if (Hash::check('qwe123456', $pwd)) {
// 密碼匹配...
}

 

舉例代碼:

//手機郵箱登錄
public function index(Request $request)
{
    $account= request('account');
    $password = request('password');
    $user = WechatAccount::where('phone',$account)->orwhere('email',$account)->first()->toarray();
    if($user){
        if (Hash::check($password, $user->password) || $user->password == md5($password)) {
            Redis::hdel('wechat_token:' . $user->wechat_token, 'WechatInfo');
            $tokenKey = str_random(64);
            $user->login_ip = $request->ip();
            $user->login_time = time();
            $user->wechat_token = $tokenKey;
            unset($user->password);
            if ($user->save()) {
                unset($user->wechat_token);
                if (Redis::hmset('wechat_token:' . $tokenKey, ['WechatInfo' => $user]) == 'OK' && Redis::expire('wechat_token:' . $tokenKey, 7200)) {
                    $vdata = [
                        'status' => 1,
                        'msg' => '登錄成功',
                        'wechat_token' => $tokenKey
                    ];
                }
            }
        } else {
            $vdata = [
                'status' => -1,
                'msg' => '密碼錯誤,請重新輸入',
            ];
        }
    }else{
        $vdata = [
            'status'=>-1,
            'msg' => '您需要註冊後,纔可以登錄呦!',
        ];

    }
    return response()->json($vdata);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章