虽然这个问题是关于一个特定的包,但它应该与其他软件包相关,因为它实际上是关于Laravel框架中正确地捕捉下面情况的位置。
我使用雄辩-奥奥斯通过linkedin和facebook登录到我的应用程序。
授权和登录都可以单独工作。
但是,如果我以前通过一个社交平台授权和登录,然后尝试授权另一个平台,我就会得到一个PDO异常,即在我的用户表上的唯一电子邮件约束上插入失败。
我应该把插入的陷阱设在哪里?
我可以看到,我可以直接添加代码到供应商文件夹,但不认为这是好的做法,因为这肯定会被覆盖,如果我在未来的某个时候升级包?
我目前正在做的是在登录函数中向vendor/adamwathan/eloquent-oauth/src/OAuthManager.php文件中添加一个用户签入
public function login($providerAlias, Closure $callback = null)
{
if (! $this->stateManager->verifyState())
{
throw new InvalidAuthorizationCodeException;
}
$details = $this->getProvider($providerAlias)->getUserDetails();
//start of existing user check
$thisUser = User::where('email', $details->email)->first();
if ($thisUser !== null)
{
$autoOpenModalLogin = true; //instruct the page to open the login modal
return redirect()->guest('home')->with('$autoOpenModalLogin', $autoOpenModalLogin);
}
//end of existing user check
return $this->authenticator->login($providerAlias, $details, $callback);
}我认为把它添加到某个路线中可能会更好--但我似乎无法避免这个错误。为了捕获PDO错误,我还需要在下面添加其他内容吗?
Route::get('{provider}/login', function ($provider) {
try {
OAuth::login($provider, function ($user, $userDetails) {
$user->email = $userDetails->email;
$user->name = $userDetails->firstName . ' ' . $userDetails->lastName;
$user->first_name = $userDetails->firstName;
$user->last_name = $userDetails->lastName;
$user->save();
});
return view('home');
} catch (ApplicationRejectedException $e) {
// User rejected application
} catch (InvalidAuthorizationCodeException $e) {
// Authorization was attempted with invalid
// code,likely forgery attempt
}
});我想,如果我保留它,插入的用户签入供应商的文件,我只需要更新那些,如果我曾经更新的软件包?
但是,有没有比我所做的更好的方法呢?
发布于 2015-07-28 01:48:13
好的,所以我为一个PDOexception添加了一个陷阱,这似乎正是我所需要的!先检查一下,然后在这里确认。
Route::get('{provider}/login', function ($provider) {
try {
OAuth::login($provider, function ($user, $userDetails) {
$user->email = $userDetails->email;
$user->name = $userDetails->firstName . ' ' . $userDetails->lastName;
$user->first_name = $userDetails->firstName;
$user->last_name = $userDetails->lastName;
$user->save();
});
return view('home');
} catch (ApplicationRejectedException $e) {
// User rejected application
} catch (InvalidAuthorizationCodeException $e) {
// Authorization was attempted with invalid
// code,likely forgery attempt
}
catch(PDOException $e){
$autoOpenModalLogin = true;
return redirect()->guest('home')->with('$autoOpenModalLogin', $autoOpenModalLogin);
}
});https://stackoverflow.com/questions/31666038
复制相似问题