首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用auth在laravel-4中更改密码

如何用auth在laravel-4中更改密码
EN

Stack Overflow用户
提问于 2013-12-19 06:39:05
回答 4查看 16.9K关注 0票数 3

我用的是LALAVEL-4-AUTH组件。我想要做一个功能,将改变用户的密码。我的看法如下:

密码-文本框;new_password -文本框;confirm_new_password -文本框

我还检查了密码重置手册,但在该文档(http://laravel.com/docs/security#password-reminders-and-reset)中,他们发送邮件进行密码重置。意见如下:

代码语言:javascript
复制
   @extends('layouts.main') 
@section('title') Change Password 
@stop
@section('content')
{{ Form::open(array('url'=>'users/user-password-change', 'class'=>'block small center login')) }}
<h3 class="">Change Password</h3>
   <h6>Please change your password below.</h6>
    <ul>
        @foreach($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>

    {{ Form::password('password', array('class'=>'input-block-level', 'placeholder'=>'Old Password')) }}
    {{ Form::password('new_password', array('class'=>'input-block-level', 'placeholder'=>'New Password')) }}
    {{ Form::password('confirm_new_password', array('class'=>'input-block-level', 'placeholder'=>'Confirm New Password')) }}


    {{ Form::submit('Register', array('class'=>'k-button'))}}
{{ Form::close() }}
@stop

控制器代码如下:

代码语言:javascript
复制
public function postUserPasswordChange(){
    $validator = Validator::make(Input::all(), User::$change_password_rules);
    if($validator->passes()){
        $user=new UserEventbot;
        $user->password=Hash::make(Input::get('new_password'));
        $user->save();
        return Redirect::to('users/change-password');
    }else {
        return Redirect::to('users/change-password')->with('message', 'The following errors occurred')->withErrors($validator)->withInput();
    }
}

请帮助我在这方面,如何首先与数据库表用户匹配这个密码,然后整个过程。谢谢你。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-12-19 09:44:16

尝试如下:

代码语言:javascript
复制
public function postUserPasswordChange(){
    $validator = Validator::make(Input::all(), User::$change_password_rules);
    if($validator->passes()){

        $user = UserEventbot::findOrFail(Auth::user()->id);

        $user->password = Hash::make(Input::get('new_password'));
        $user->save();
        return Redirect::to('users/change-password');
    }else {
        return Redirect::to('users/change-password')->with('message', 'The following errors occurred')->withErrors($validator)->withInput();
    }
}
票数 5
EN

Stack Overflow用户

发布于 2014-04-26 19:32:13

要将当前用户密码与数据库中的密码匹配,您可以这样做,

代码语言:javascript
复制
// retrieve the authenticated user
$user = Auth::user();

检索表单中用户指定的当前密码,

代码语言:javascript
复制
$current_password = Input::get('current_password');

我们可以查看当前密码是否已指定,并按以下方式对照散列密码进行检查,

代码语言:javascript
复制
if (strlen($current_password) > 0 && !Hash::check($current_password, $user->password)) {
        return Redirect::to('/user/edit-profile')->withErrors('Please specify the good current password');
    }

这里要注意的是函数

Hash::check(CURRENT_PASSWORD_ENTERED_IN_FORM,HASHED_VERSION_OF_PASSWORD_STORED_IN_AUTH_USER)

最后,如果一切顺利,您可以在Auth和数据库中更新当前的用户密码,如下所示,

代码语言:javascript
复制
// authenticated user
$user = Auth::user();
$user->password = Hash::make($new_password);

// finally we save the authenticated user
$user->save();
票数 8
EN

Stack Overflow用户

发布于 2014-05-17 21:32:50

这是我自己的解决方案,我有3个输入= old_password、密码、password_confirmation、表单和action=post。验证器检查要求,散列检查旧密码是否匹配

代码语言:javascript
复制
public function changePassword() 
{
    $user = Auth::user();
    $rules = array(
        'old_password' => 'required|alphaNum|between:6,16',
        'password' => 'required|alphaNum|between:6,16|confirmed'
    );

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

    if ($validator->fails()) 
    {
        return Redirect::action('UserController@show',$user->id)->withErrors($validator);
    } 
    else 
    {
        if (!Hash::check(Input::get('old_password'), $user->password)) 
        {
            return Redirect::action('UserController@show',$user->id)->withErrors('Your old password does not match');
        }
        else
        {
            $user->password = Hash::make(Input::get('password'));
            $user->save();
            return Redirect::action('UserController@show',$user->id)->withMessage("Password have been changed");
        }
    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20674966

复制
相关文章

相似问题

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