Laravel 5 社会名流 - 动态更改身份验证重定向路径
Laravel 5 Socialite - change auth redirect path dynamically
我正在尝试将 Socialite 整合到一个网站中,但遇到了一个问题,这应该是一件相当微不足道的事情 - 在社交提供商验证后更改用户重定向的位置。
我将使用 Socialite 进行三项操作:
- Register - 向提供商进行身份验证,然后重定向到注册表。他们的姓名和电子邮件地址已为他们填写,需要更多字段。
- 登录 - 典型的登录场景,根据用户table检查提供者信息,如果匹配
,他们将登录
- Link - 用户已经有一个活跃的帐户,但想要 link 一个外部帐户(Facebook 或 Google)。这对用户进行身份验证并将适当的社交媒体 ID 添加到用户 table 中的行中。
问题是您在 services.php 文件中指定了身份验证重定向,我不知道如何在与第三方提供商成功验证后更改用户重定向的位置。
这是我目前所拥有的 -
路线:这似乎在社交提供者重定向时起作用,因为缺少 driver=facebook&action=login
(例如)。理想情况下,我想指定在发出初始身份验证请求时将用户重定向到的位置——这只是我第一次尝试解决这个问题。
get('social-auth', function(AuthenticateUser $authenticateUser, Request $request) {
$driver = $request->input('driver');
$action = $request->input('action');
return $authenticateUser->execute($request->has('code'), $driver, $action);
});
AuthenticateUser class - 希望这里的评论足够了:
<?php namespace App;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Laravel\Socialite\Contracts\Factory as Socialite;
use Auth;
use Flash;
class AuthenticateUser
{
/**
* @var Socialite
*/
private $socialite;
/**
* @var Auth
*/
private $auth;
public function __construct(Socialite $socialite, Auth $auth)
{
$this->socialite = $socialite;
$this->auth = $auth;
}
/**
* @param boolean $hasCode Whether or not we have been authenticated already
* @param string $driver Driver to use, Facebook or Google
* @param string $type Type of call - Login, Register, or Link
* @return array|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function execute($hasCode, $driver, $type)
{
if (!$hasCode) {
return $this->getAuthorizationFirst($driver);
}
// Get the User details from the Social Provider
$socialUser = $this->socialite->driver($driver)->user();
$socialUserArray = $socialUser->user;
if ($driver == 'facebook') {
// Get Facebook specific fields
$first_name = $socialUserArray['first_name'];
$last_name = $socialUserArray['last_name'];
} else if ($driver == 'google') {
// Get Google specific fields
$first_name = $socialUserArray['name']['givenName'];
$last_name = $socialUserArray['name']['familyName'];
}
$email = $socialUser->email;
$id = $socialUser->id;
// Perform an action - login, register, or link
switch ($type) {
case 'login':
// Log the user in with the Facebook ID
try {
if ($driver == 'facebook') {
$user = User::whereFacebookAuth($id)->firstOrFail();
} else {
$user = User::whereGoogleAuth($id);
}
} catch (ModelNotFoundException $e) {
flash::error('Could not find a user associated with this ' . ucfirst($driver) . ' account.');
return redirect('auth/login');
}
Auth::login($user);
return redirect('members');
break;
case 'register':
// Register using social media account
return redirect('register')
->with('social_type', $driver)
->with('social_id', $id)
->with('email', $email)
->with('first_name', $first_name)
->with('last_name', $last_name);
break;
case 'link':
// Associate this Social Media account with the current User
$driver == 'facebook' ? Auth::user()->facebook_auth = $id : Auth::user()->google_auth = $id;
Auth::user()->save();
return ['status' => 'success'];
break;
}
}
/**
* Authorize the user before proceeding
* @param $driver
* @return mixed
*/
private function getAuthorizationFirst($driver)
{
return $this->socialite->with($driver)->redirect('hopefully-somewhere-else');
// Anything inside redirect doesn't seem to do anything
}
}
我倾向于在我的网址中包含提供商。例如:
Route::get('auth/{provider}', 'AuthController@redirectToProvider');
Route::get('auth/{provider}/callback', 'AuthController@handleProviderCallback');
这样我就可以在我的控制器操作中从请求中访问提供者名称:
public function callback($provider)
{
$user = $this->socialite->driver($provider)->getUser();
try {
// Try and find user by their social profile UID
$appUser = SocialAccount::whereUid($user->getId())
->whereProvider($provider)
->firstOrFail();
Auth::loginUsingId($appUser->user_id);
} catch (ModelNotFoundException $e) {
if (Auth::user()) {
// Attach social profile to logged in user
} else {
// User is not logged in, and account does not exist
// Prompt to register
}
}
}
参加晚会但可能会帮助别人。如果您想处理登录、注册、link 社交帐户到用户个人资料等多重重定向,可以通过从您的控制器端覆盖服务配置来完成。
config(['services.google.redirect' => <Your New URL here> ]);
Socialite::driver('google')->redirect();
注意:- 有一些社交平台严格要求在其开发者控制台中提及重定向 URL。因此,您需要在此处提及每个重定向 URL。
我正在尝试将 Socialite 整合到一个网站中,但遇到了一个问题,这应该是一件相当微不足道的事情 - 在社交提供商验证后更改用户重定向的位置。
我将使用 Socialite 进行三项操作:
- Register - 向提供商进行身份验证,然后重定向到注册表。他们的姓名和电子邮件地址已为他们填写,需要更多字段。
- 登录 - 典型的登录场景,根据用户table检查提供者信息,如果匹配 ,他们将登录
- Link - 用户已经有一个活跃的帐户,但想要 link 一个外部帐户(Facebook 或 Google)。这对用户进行身份验证并将适当的社交媒体 ID 添加到用户 table 中的行中。 问题是您在 services.php 文件中指定了身份验证重定向,我不知道如何在与第三方提供商成功验证后更改用户重定向的位置。
这是我目前所拥有的 -
路线:这似乎在社交提供者重定向时起作用,因为缺少 driver=facebook&action=login
(例如)。理想情况下,我想指定在发出初始身份验证请求时将用户重定向到的位置——这只是我第一次尝试解决这个问题。
get('social-auth', function(AuthenticateUser $authenticateUser, Request $request) {
$driver = $request->input('driver');
$action = $request->input('action');
return $authenticateUser->execute($request->has('code'), $driver, $action);
});
AuthenticateUser class - 希望这里的评论足够了:
<?php namespace App;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Laravel\Socialite\Contracts\Factory as Socialite;
use Auth;
use Flash;
class AuthenticateUser
{
/**
* @var Socialite
*/
private $socialite;
/**
* @var Auth
*/
private $auth;
public function __construct(Socialite $socialite, Auth $auth)
{
$this->socialite = $socialite;
$this->auth = $auth;
}
/**
* @param boolean $hasCode Whether or not we have been authenticated already
* @param string $driver Driver to use, Facebook or Google
* @param string $type Type of call - Login, Register, or Link
* @return array|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function execute($hasCode, $driver, $type)
{
if (!$hasCode) {
return $this->getAuthorizationFirst($driver);
}
// Get the User details from the Social Provider
$socialUser = $this->socialite->driver($driver)->user();
$socialUserArray = $socialUser->user;
if ($driver == 'facebook') {
// Get Facebook specific fields
$first_name = $socialUserArray['first_name'];
$last_name = $socialUserArray['last_name'];
} else if ($driver == 'google') {
// Get Google specific fields
$first_name = $socialUserArray['name']['givenName'];
$last_name = $socialUserArray['name']['familyName'];
}
$email = $socialUser->email;
$id = $socialUser->id;
// Perform an action - login, register, or link
switch ($type) {
case 'login':
// Log the user in with the Facebook ID
try {
if ($driver == 'facebook') {
$user = User::whereFacebookAuth($id)->firstOrFail();
} else {
$user = User::whereGoogleAuth($id);
}
} catch (ModelNotFoundException $e) {
flash::error('Could not find a user associated with this ' . ucfirst($driver) . ' account.');
return redirect('auth/login');
}
Auth::login($user);
return redirect('members');
break;
case 'register':
// Register using social media account
return redirect('register')
->with('social_type', $driver)
->with('social_id', $id)
->with('email', $email)
->with('first_name', $first_name)
->with('last_name', $last_name);
break;
case 'link':
// Associate this Social Media account with the current User
$driver == 'facebook' ? Auth::user()->facebook_auth = $id : Auth::user()->google_auth = $id;
Auth::user()->save();
return ['status' => 'success'];
break;
}
}
/**
* Authorize the user before proceeding
* @param $driver
* @return mixed
*/
private function getAuthorizationFirst($driver)
{
return $this->socialite->with($driver)->redirect('hopefully-somewhere-else');
// Anything inside redirect doesn't seem to do anything
}
}
我倾向于在我的网址中包含提供商。例如:
Route::get('auth/{provider}', 'AuthController@redirectToProvider');
Route::get('auth/{provider}/callback', 'AuthController@handleProviderCallback');
这样我就可以在我的控制器操作中从请求中访问提供者名称:
public function callback($provider)
{
$user = $this->socialite->driver($provider)->getUser();
try {
// Try and find user by their social profile UID
$appUser = SocialAccount::whereUid($user->getId())
->whereProvider($provider)
->firstOrFail();
Auth::loginUsingId($appUser->user_id);
} catch (ModelNotFoundException $e) {
if (Auth::user()) {
// Attach social profile to logged in user
} else {
// User is not logged in, and account does not exist
// Prompt to register
}
}
}
参加晚会但可能会帮助别人。如果您想处理登录、注册、link 社交帐户到用户个人资料等多重重定向,可以通过从您的控制器端覆盖服务配置来完成。
config(['services.google.redirect' => <Your New URL here> ]);
Socialite::driver('google')->redirect();
注意:- 有一些社交平台严格要求在其开发者控制台中提及重定向 URL。因此,您需要在此处提及每个重定向 URL。