首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新记录的用户密码laravel 5

更新记录的用户密码laravel 5
EN

Stack Overflow用户
提问于 2015-08-04 16:10:45
回答 4查看 3.2K关注 0票数 2

我想为登录的用户添加密码更新选项,因此我使用了以下代码

控制器auth\authController.php

代码语言:javascript
复制
public function updatePassword()
    {

        $user = Auth::user();
        $rules = array(
            'old_password' => 'required',
            'password' => 'required|alphaNum|between:6,16|confirmed'
        );

        $validator = Validator::make(Input::all(), $rules);

        if ($validator->fails()) {
            return Redirect::route('change-password', $user->id)->withErrors($validator);
        } else {
            if (!Hash::check(Input::get('old_password'), $user->password)) {
                return Redirect::route('change-password', $user->id)->withErrors('Your old password does not match');
            } else {
                $user->password = Input::get('password');
                $user->save();
                return Redirect::route('change-password', $user->id)->with("message", "Password have been changed");
            }
        }
    }

路线

代码语言:javascript
复制
Route::post('change-password', 'Auth\AuthController@updatePassword');
Route::get('change-password', 'Auth\AuthController@updatePassword');

即时消息收到以下错误

代码语言:javascript
复制
FatalErrorException in AuthController.php line 123:
Class 'App\Http\Controllers\Auth\Auth' not found

对于行"$user = Auth::user();“

EN

回答 4

Stack Overflow用户

发布于 2016-08-27 05:31:54

你的问题有隐藏的答案..我有类似的问题@faz..实际上我已经用他的问题的代码做了这个把戏

实现这一目标的正确方法- -

代码语言:javascript
复制
 protected function postChangePassword(ChangePasswordFormRequest $request){

    $user = Auth::user();
    $current_password = Input::get('current_password');
    $password = bcrypt(Input::get('password'));

    $user_count = DB::table('users')->where('id','=',$this->user_id)->count();

    if (Hash::check($current_password, $user->password) && $user_count == 1) {
        $user->password = $password;
        try {
            $user->save();
            $flag = TRUE;
        }
        catch(\Exception $e){
            $flag = FALSE;
        }
        if($flag){
            return redirect('/u/0/change/password')->with('success',"Password changed successfully.");
        }
        else{
            return redirect('/u/0/change/password')->with("danger","Unable to process request this time. Try again later");
        }
    }
    else{
        return redirect('/u/0/change/password')->with("warning","Your current password do not match our record");
    }
}

请注意,对于哈希和身份验证,我们需要包括顶部的类和我通过构造函数$this->user_id = Auth::user()->id;获得的user_id。我想我已经帮助了很多人。

票数 1
EN

Stack Overflow用户

发布于 2015-08-04 16:17:57

其名称空间问题,请尝试:

代码语言:javascript
复制
//if this method is not protected by a middleware for only authenticated users
//verify that user is currently logged in:

if(!$user = \Auth::user()) abort(503); //or return redirect()->route('login')

$rules = array(
    'old_password' => 'required',
    'password' => 'required|alphaNum|between:6,16|confirmed'
);

或者在AuthController的顶部添加命名空间

代码语言:javascript
复制
use Auth;

class AuthController{
   ....
}
票数 0
EN

Stack Overflow用户

发布于 2015-08-04 16:19:09

您没有导入Auth类。

将此代码添加到文件的顶部。在命名空间之后。

使用Illuminate\Support\Facades\Auth;

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31803973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档