我正在尝试创建一个除了通过 Socialite 插件登录用户之外什么都不做的应用程序。一切正常,我被重定向到 facebook,创建了新用户,然后当用户被重定向到“/”时,用户神秘地注销了。
(注意,这不会在本地发生。在我的本地机器上一切正常)。
Facebook 登录控制器:
public function handleFacebookCallback()
{
$socialiteUser = Socialite::driver('facebook')->stateless()->user();
$user = $this->findOrCreateUser('facebook', $socialiteUser);
Auth::login($user, true);
return redirect('/');
}
路由文件:
Route::get('/', 'Home@home')->name('home')->middleware('auth');
Route::get('/home', 'Home@home')->name('home')->middleware('auth');
Route::get('/login', 'Controller@welcome')->name('welcome');
Auth::routes();
Route::get('/logout', 'Auth\LoginController@logout');
Route::get('facebook', function() {
return view('facebook');
});
Route::get('auth/facebook', 'Auth\FacebookController@redirectToFacebook');
Route::get('auth/facebook/callback', 'Auth\FacebookController@handleFacebookCallback');
登录控制器:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
中间件/RedirectIfAuthenticated.php:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
return $next($request);
}
}
我的中间件文件夹中没有定义的“auth”中间件。它是通过 laravel 自动完成的。
再次:该项目在本地按预期工作,但不在服务器上。
最后一点:当我在重定向到“/”之前立即 dd(Auth::check()) 时,即使在服务器上,我也会得到“true”,但是当我在登录页面上 dd 相同的东西时,我会得到 false。因此,用户以某种方式在重定向上被注销。