我正在我的 Laravel 项目中实现一个不同于用户的表。这是一个联系人表,这些联系人不是用户,但具有类似的用户数据,例如电子邮件、姓名和 email_verified_at
列。
虽然联系人与用户相关联,但我想确保联系人电子邮件经过验证,并且它们存在。我想向他们发送电子邮件通知,一旦点击就会验证他们,但是,我的默认控制器希望登录用户登录,这很好,然后希望让用户发送电子邮件。< /p>
我正在尝试保留该逻辑,并且能够简单地构造一个用户对象并将该联系人电子邮件发送到该电子邮件,但是我的 $contactToVerify
只返回 Call to a member function hasVerifiedEmail() on array
而我不是知道为什么。
resend
函数负责发送电子邮件。
联系人看起来像:
{
"contactData":{
"id":2,
"first_name":"john",
"last_name":"doe",
"email":"johndoe@outlook.com",
"canNotify":true,
"email_verified_at":null,
"team_id":3,
"notification_preferences":"{\"ssl\": {\"expiry\": [\"mail\", \"database\"]}, \"domains\": {\"expiry\": [\"mail\", \"database\"]}, \"monitors\": {\"up\": [\"mail\", \"database\"], \"down\": [\"mail\", \"database\"]}}",
"created_at":"2021-02-16T19:32:51.000000Z",
"updated_at":"2021-02-16T20:54:55.000000Z"
}
}
team_id
指的是登录用户,因为一个用户可以有各种不需要是用户的联系人。
我的控制器是:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Auth\VerifiesEmails;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Redirect;
use App\Models\User;
use App\Models\NotificationContact;
class VerificationController extends Controller
{
/*
|--------------------------------------------------------------------------
| Email Verification Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling email verification for any
| user that recently registered with the application. Emails may also
| be re-sent if the user didn't receive the original email message.
|
*/
use VerifiesEmails;
/**
* Where to redirect users after verification.
*
* @var string
*/
protected $redirectTo = 'http://127.0.0.1:8000/account/domains';
/**
* Mark the authenticated user's email address as verified.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function verify(Request $request)
{
$frontendURL = $this->getSetting('api_frontend_url') != null ? $this->getSetting('api_frontend_url') : 'https://example.com/';
$user = User::find($request->route('id'));
JWTAuth::fromUser($user);
if (! hash_equals((string) $request->route('id'), (string) $user['id'])) {
throw new AuthorizationException;
}
if (! hash_equals((string) $request->route('hash'), sha1($user['email']))) {
throw new AuthorizationException;
}
if ($user->hasVerifiedEmail()) {
return Redirect::to("${frontendURL}account/domains");
}
if ($user->markEmailAsVerified()) {
event(new Verified($user));
}
if ($user->hasVerifiedEmail()) {
return Redirect::to("${frontendURL}account/domains");
}
if ($response = $this->verified($request)) {
return $response;
}
if ($request->wantsJson()) {
return response()->json(['success' => true, 'message' => 'An email verification has been sent to: ' . $user['email'] . '. Check your inbox and spam within the next 15 minutes. You\'ll have 2 hours to verify your email'], 200);
}
}
/**
* Resend the email verification notification.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function resend(Request $request)
{
$frontendURL = $this->getSetting('api_frontend_url') != null ? $this->getSetting('api_frontend_url') : 'https://example.com/';
$isCustomVerification = (bool) $request->input('isCustomVerification');
if ($isCustomVerification) {
$contactToVerify = $request->input('contactData');
if ($contactToVerify->hasVerifiedEmail()) {
return Redirect::to("${frontendURL}account/domains");
}
$contactToVerify->sendEmailVerificationNotification();
if ($request->wantsJson()) {
return response()->json(['success' => true, 'message' => 'An email verification has been sent to: ' . $userToVerify->email . '. Check your inbox and spam within the next 15 minutes, You\'ll have 2 hours to verify your email'], 200);
}
}
if ($request->user()->hasVerifiedEmail()) {
return Redirect::to("${frontendURL}account/domains");
}
$request->user()->sendEmailVerificationNotification();
if ($request->wantsJson()) {
return response()->json(['success' => true, 'message' => 'An email verification has been sent to: ' . $request->user()->email . '. Check your inbox and spam within the next 15 minutes, You\'ll have 2 hours to verify your email'], 200);
}
}
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:api')->only('resend');
$this->middleware('signed')->only('verify');
$this->middleware('throttle:60,1')->only('verify', 'resend');
}
}