dongzhilian0188 2017-06-26 22:18
浏览 73
已采纳

setPasswordAttribute用户模型螺丝内置auth,如何纠正?

I created CRUD for managing users in my app. Now, my issue is that I am using User::create(request()->all()), and I had implemented a setPasswordAttribute method within my model which automatically bcrypted the password, and it worked well...however...

I just discovered that this screws with some of Laravels built-in traits which the auth uses such as ResetsPasswords. So me implementing that setPasswordAttribute method actually caused the auth scaffolding to start double-bcrypting the password, causing it to fail when the user tried logging in after resetting their password.

For the sake of clean code, I'm wondering the best way I can correct this. I know I can just use save instead of create, but I'd like to keep my controllers small and tidy as possible.

What is the best way to handle this? I was thinking of simply bcrypting the value of the actual request input, but that seems hacky.

  • 写回答

1条回答 默认 最新

  • douman6245 2017-06-26 22:50
    关注

    You could use another name for this field in your html form, for example plain_password, and then use a mutator with this name that mutates the password field.
    This way, you don't have to write extra code and the Laravel code itself wont use this mutator.

    public function setPlainPassword($value) {
         $this->attributes['password'] = bcrypt($value);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 某东JD算法逆向算法
  • ¥15 求GCMS辅导数据分析
  • ¥30 SD中的一段Unet下采样代码其中的resnet是谁跟谁进行残差连接
  • ¥15 Unet采样阶段的res_samples问题
  • ¥60 Python+pygame坦克大战游戏开发实验报告
  • ¥15 R语言regionNames()和demomap()无法选中中文地区的问题
  • ¥15 Open GL ES 的使用
  • ¥15 我如果只想表示节点的结构信息,使用GCN方法不进行训练可以吗
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部