在我的laravel 5.4安装中,Auth不会持续存在。我已经阅读了很多其他帖子,所有这些都与修改路由中间件有关,但我没有太多运气。
登录后,我基本上会进入重新直接循环,因为我已经过身份验证,重定向回我的主页,但该主页(通过路由)并不认为我已登录要求我再次登录!
routes(web.php)
Route::group(['middleware' => 'web'], function () {
// Frontpage
Route::get('/', ['as' => 'home', function () {
return redirect('audiolibrary');
}]);
// AUTH
Route::get('login', [
'as' => 'auth.login', 'uses' => 'AuthController@login',
]);
Route::get('logout', [
'as' => 'auth.login', 'uses' => 'AuthController@logout',
]);
});
Route::group(['middleware' => ['web', 'auth']], function () {
// AUDIO LIBRARY
Route::get('audiolibrary', [
'as' => 'audiolibrary.index', 'uses' => 'AudioLibraryController@index',
]);
});
AuthController.php
namespace App\Http\Controllers;
use Auth;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Auth\Authenticatable;
class AuthController extends Controller
{
public function login(Request $request)
{
$code = $request->get('code');
$googleService = \OAuth::consumer('Google');
if (!is_null($code)) {
$token = $googleService->requestAccessToken($code);
$google = json_decode($googleService->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
if(User::where('email', '=', $google['email'])->first()){
$user = User::where('email', '=', $google['email'])->first();
Auth::login($user);
return redirect('/');
} else {
$user = new User();
$user->name = $google['name'];
$user->email = $google['email'];
$user->save();
Auth::login($user);
}
} else {
$url = $googleService->getAuthorizationUri();
return redirect((string)$url);
}
}
}