本文整理汇总了PHP中Laravel\Socialite\Facades\Socialite::user方法的典型用法代码示例。如果您正苦于以下问题:PHP Socialite::user方法的具体用法?PHP Socialite::user怎么用?PHP Socialite::user使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Laravel\Socialite\Facades\Socialite
的用法示例。
在下文中一共展示了Socialite::user方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: handleProviderCallback
/**
* Obtain the user information from Tokenpass.
*
* This is the route called after Tokenpass has granted (or denied) permission to this application
* This application is now responsible for loading the user information from Tokenpass and storing
* it in the local user database.
*
* @return Response
*/
public function handleProviderCallback(Request $request)
{
try {
// check for an error returned from Tokenpass
$error_description = Tokenpass::checkForError($request);
if ($error_description) {
return view('account.authorization-failed', ['error_msg' => $error_description]);
}
// retrieve the user from Tokenpass
$oauth_user = Socialite::user();
// get all the properties from the oAuth user object
$tokenly_uuid = $oauth_user->id;
$oauth_token = $oauth_user->token;
$username = $oauth_user->user['username'];
$name = $oauth_user->user['name'];
$email = $oauth_user->user['email'];
$email_is_confirmed = $oauth_user->user['email_is_confirmed'];
// find an existing user based on the credentials provided
$existing_user = User::where('tokenly_uuid', $tokenly_uuid)->first();
// if an existing user wasn't found, we might need to find a user to merge into
$mergable_user = $existing_user ? null : User::where('username', $username)->orWhere('email', $email)->where('tokenly_uuid', null)->first();
if ($existing_user) {
// update the user
$existing_user->update(['tokenly_uuid' => $tokenly_uuid, 'oauth_token' => $oauth_token, 'name' => $name, 'username' => $username, 'email' => $email]);
// login
Auth::login($existing_user);
} else {
if ($mergable_user) {
// an existing user was found with a matching username
// migrate it to tokenpass
if ($mergable_user['tokenly_uuid']) {
throw new Exception("Can't merge a user already associated with a different tokenly account", 1);
}
// update if needed
$mergable_user->update(['tokenly_uuid' => $tokenly_uuid, 'oauth_token' => $oauth_token, 'name' => $name, 'username' => $username, 'email' => $email]);
// login
Auth::login($mergable_user);
} else {
// no user was found - create a new user based on the information we received
//make sure these fields are all "fillable" in your User model
$new_user = User::create(['tokenly_uuid' => $tokenly_uuid, 'oauth_token' => $oauth_token, 'name' => $name, 'username' => $username, 'email' => $email]);
// login
Auth::login($new_user);
}
}
return redirect('/account/login');
} catch (Exception $e) {
// some unexpected error happened
return view('account.authorization-failed', ['error_msg' => 'Failed to authenticate this user.']);
}
}