我遇到一些令人毛骨悚然的问题。我问这个问题是因为我很久没找到解决方案了。我使用Socialite在我的网站上创建用户。问题是来自Facebook和Google服务的回调。我总是说“无法访问此网站”。这是我的主域-当我先前在子域中进行测试时,一切正常。我尝试创建其他Facebook应用-无法正常工作。我尝试在回调URL的末尾添加和删除'/'-不起作用。
有我的代码(在子域上工作正常)。
config / services.php
'facebook' => [
'client_id' => env('FACEBOOK_ID'),
'client_secret' => env('FACEBOOK_SECRET'),
'redirect' => env('FACEBOOK_URL'),
],
'google' => [
'client_id' => env('GOOGLE_ID'),
'client_secret' => env('GOOGLE_SECRET'),
'redirect' => env('GOOGLE_URL'),
],
Auth / AuthController.php
<?php
namespace App\Http\Controllers\Auth;
use Auth;
use Socialite;
use App\User;
use Session;
use Redirect;
use App\Http\Controllers\Controller;
class AuthController extends Controller
{
/**
**_ Redirect the user to the OAuth Provider.
**_ @return Response**/
public function redirectToProvider($provider)
{
return Socialite::driver($provider)->redirect();
}
//Obtain the user information from provider. Check if the user already exists in our
//database by looking up their provider_id in the database.
//If the user exists, log them in. Otherwise, create a new user then log them in. After that
//redirect them to the authenticated users homepage.
//@return Response
public function handleProviderCallback($provider)
{
$user = Socialite::driver($provider)->user();
$authUser = $this->findOrCreateUser($user, $provider);
try{
if($authUser != null){
try{
Auth::login($authUser, true);
Session::flash('message', "Nice to see you.");
return Redirect::to('offers');
}catch(\Exception $e){
Session::flash('message', "Can't sign in a user.");
return Redirect::to('offers');
}
}else{
Session::flash('message', "Can't sign in a user.");
return Redirect::to('offers');
}
}catch(\Exception $e){
Session::flash('message', "Can't sign in a user.");
return Redirect::to('offers');
}
}
//If a user has registered before using social auth, return the user
//else, create a new user object.
//@param $user Socialite user object
//@param $provider Social auth provider
//@return User
public function findOrCreateUser($user, $provider)
{
$authUser = User::where('provider_id', $user->id)->first();
if ($authUser) {
return $authUser;
}
try{
Session::flash('message', "Thank you. You created an account. Enjoy.");
return User::create([
'name' => $user->name,
'email' => $user->email,
'provider' => $provider,
'provider_id' => $user->id
]);
}catch(\Exception $e){
Session::flash('message', "Can't sign up a user.");
return;
}
}
}
routes / api.php
Route::get('auth/{provider}', 'Auth\AuthController@redirectToProvider');
Route::get('auth/{provider}/callback', 'Auth\AuthController@handleProviderCallback');
有效的OAuth重定向URI(在fb开发人员页面中):
https://www.last-bee.com/api/auth/facebook/callback
我非常感谢您为解决该问题提供的帮助/提示。 :)