dongyan8929 2018-03-06 07:52
浏览 165

Laravel将自定义代码添加到AuthController。 PHP

I am pretty clue less when it comes to Laravel I've been trying to get this working for sometime now and have just gotten error after error.

My end goal is to have some custom code run upon registration that puts the password in plain text (as an example) into a database (as an example). This is because I need to send there password off to an API which will create an account for them on another platform aswell.

This is the code I'm working with:

/**
     * Handle a registration request for the application.
     *
     * @param RegisterRequest $request
     * @param RoleRepository $roles
     * @return \Illuminate\Http\Response
*/
public function postRegister(RegisterRequest $request, RoleRepository $roles)
{
    // Determine user status. User's status will be set to UNCONFIRMED
    // if he has to confirm his email or to ACTIVE if email confirmation is not required
    $status = settings('reg_email_confirmation')
        ? UserStatus::UNCONFIRMED
        : UserStatus::ACTIVE;

    $role = $roles->findByName('User');

    // Add the user to database
    $user = $this->users->create(array_merge(
        $request->only('username', 'password'),
        ['status' => $status, 'role_id' => $role->id]
    ));

    event(new Registered($user));

    //My Custom Code
    $conn = mysqli_connect('localhost', 'u_dev', 'dbpassword', 'dev');

    $stmt = $conn->prepare('INSERT INTO ironlogin (ironlogin_password) VALUES (?)');
    $stmt->bind_param('s', $ironPassword);

    $stmt->execute();
    $stmt->close();
    $conn->close();
    //End My Custom Code

    $message = settings('reg_email_confirmation')
        ? trans('app.account_create_confirm_email')
        : trans('app.account_created_login');

    return redirect('login')->with('success', $message);
}

I am stuck on how I'd go about putting the users password into that database for example.

  • 写回答

1条回答 默认 最新

  • doumu1951 2018-03-06 12:57
    关注

    I highly recommend NOT storing the password in plain text, but I'm sure you know thats not a good idea!

    You could easily extend or modify the register controller to add the extra logic to the create method to run before/after the user is created or send it off as a job, since you will have access to the original raw password.

    https://github.com/laravel/laravel/blob/master/app/Http/Controllers/Auth/RegisterController.php

    Edit: above link is to the github version of the controller which you want to edit!

    评论

报告相同问题?

悬赏问题

  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序