如何在laravel中使用忘记密码url发送自定义标头信息

时间:2017-06-13 13:31:04

标签: php laravel laravel-5.4

我已经在laravel中创建了一个忘记密码的自定义链接,当用户点击忘记密码时,该密码会邮寄给用户。现在,我想添加我的自定义标头,如时间戳,以验证我创建的网址,以便如果有人改变网址,那么用户将无法重置密码的可能性如何?我做了这么多。

public function forgotPassword(Request $request)
{
    $validator = Validator::make(
        array(
            'email' => $request->emailId
        ),
        array(
            'email' => 'required|email'
        )
    );
    if ($validator->fails()) {
        $errors = $validator->errors();
        if ($errors->first('email')) {
            $message = $errors->first('email');
        } else {
            $message = __('apiMessages.parametersRequired');
        }
        $this->setMeta("422", $message);
        return response()->json($this->setResponse());
    }
    try {
        $validateUser = User::where(array(
            'email' => $request->emailId,
            'userStatus' => Constant::STATUS_1))
            ->first();
        if (!$validateUser) {
            $this->setMeta('403', __('apiMessages.invalidEmailAddress'));
            return response()->json($this->setResponse());
        }
        $now = trim(Carbon::now()->timestamp);
        $userId = $validateUser->userId;
        $tokenUserId = '';
        $timestampToken = '';
        $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
        while ($userId > 0) {
            $index = $userId % 64;
            $userId = ($userId - $index) / 64;
            $tokenUserId = $alphabet{$index} . $tokenUserId;
        }
        while ($now > 0) {
            $index = $now % 64;
            $now = ($now - $index) / 64;
            $timestampToken = $alphabet{$index} . $timestampToken;
        }
        $token = $tokenUserId . '$' . $timestampToken;

        $url = url('/') . '/api/resetPassword/' . $token;
        $link = '<a href="' . $url . '"> Link </a>';

        $name= $validateUser->firstName .' '.$validateUser->lastName;
        $subject = "Forgot Password";
        $msg = "Please go to this link and reset your password : " . $url;

        //$this->send($request->emailId, $name, $subject, $msg);
        $mailData = array(
            'to' => $request->emailId,
            'name' => $name,
            'subject' => $subject,
            'msg' => $msg
        );
        dispatch(new SendEmail($mailData));

        $this->setMeta("200", __('apiMessages.forgotPasswordEmail'));
        /*$this->setMeta("200", $link);
        $this->setData("timestamp", $now1);*/
        return response()->json($this->setResponse());
    } catch (QueryException $e) {
        $this->setMeta("500", __('apiMessages.queryError'));
        return response()->json($this->setResponse());
    }
}

提前致谢:)

0 个答案:

没有答案