【发布时间】:2020-01-03 20:31:46
【问题描述】:
我正在使用最新版本的 Laravel(通常)并尝试通过电子邮件验证实现身份验证。但是,我在用户注册后收到的电子邮件中的链接会导致 403 错误页面。不过链接似乎是正确的。
我正在与 Homestead 和 Vagrant 在 'localhost' 中工作。 注册用户正在工作,我在函数结束时获得了创建的用户,并且数据库填充得很好。
我不得不编辑 RegisterController 创建方法,因为没有发送邮件。
首先,点击链接时,我只有一个空白页面。经过一番阅读,我了解到用户需要在验证之前进行身份验证,并且注册用户后我的 RegisterController 没有对他进行身份验证。 在创建用户后,我添加了一些代码来对用户进行身份验证。 现在,当我点击链接时,我得到一个 403 错误页面。
注意:我的不是默认表的表包含 email_verified_at 字段。
这是我在 RegisterController 中的创建方法:
protected function create(Request $request)
{
//Create user in database
$user = User::create([
'Username' => $request['pc2f448466c4bad46f466f025e6cf88f9'],
'email' => $request['p07d5d4eb055cc69dbba790bb9d39a3dc'],
'Password' => md5($request['pb80ee032d334d60f5bc555af500fcf87']),
'SecretQuestion' => $request['secret_question'],
'SecretAnswer' => $request['secret_answer'],
'Nickname' => $request['p9c969905e63e2824c45b8712e6d29585']
]);
$user->sendEmailVerificationNotification();
Auth::login($user);
}
这是我的验证控制器:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\VerifiesEmails;
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 = '/';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('signed')->only('verify');
$this->middleware('throttle:6,1')->only('verify', 'resend');
}
protected function redirectTo()
{
return route('home');
}
}
我尝试在 TrustProxies 中间件中设置 protected $proxies = '*';。
.env 文件中我的应用程序常量:
APP_NAME=Furya
APP_ENV=local
APP_KEY=base64:xxxxxxxxxxxxxxxxxxxx
APP_DEBUG=true
APP_URL=http://localhost
在我的网络路由文件中,我的第一条路由是:
Auth::routes(['verify' => true]);
Route::get('/', function () {
return view('main/home');
})->name('home');
使用php artisan route:list 时,我看到Auth::routes(['verify' => true]); 路由的正确中间件。
在我的用户表 (MySQL) 中,email_verified_at 字段具有日期时间类型。
在应用程序配置中,我的应用程序 URL 是这样设置的:
'url' => env('APP_URL', 'http://sunshine.local'),
'asset_url' => env('ASSET_URL', null),
我实际收到的链接是这样的:http://sunshine.local/email/verify/177?expires=1567179404&signature=0a81e1370934c95282975c9fb7cfa379fd1945814e45894ffec5728e04ad4f7a
我不知道发生了什么,遗憾的是直到现在互联网上都没有找到对我有帮助的解决方案。
感谢您的帮助。
【问题讨论】:
-
你有控制器
app/Http/Controllers/Auth/VerificationController.php吗?如果是这样,你能把它包括在你的问题中吗? -
@UdoE。是的,我有它,但没有编辑它。我正在用它的代码编辑我的帖子。
-
好的。在您点击验证链接后,您的用户是否得到验证?我的意思是,当您检查用户表中的
email_verified_at字段时,它是null还是已更新(即使出现403 错误)?另外,您能否在web.php中提供您的家乡路线定义。似乎 url'/'以某种方式受到保护,必须通过定义的路由方法访问。 -
@UdoE。该字段保持为空。我用我的家乡路线定义更新我的帖子。
-
@TomGiru 看到我的回答。希望对您有所帮助。