所以我对一个登录我的控制器进行了ajax调用,我知道这是因为Auth::check($user)
和返回的cookie数据,例如laravel会话cookie。
但是,我的问题出在我的routes.php
,我仍然无法访问授权的网页,并继续点击welcome
视图。
我的routes.php:
use App\punsmodel;
use Illuminate\Http\Request;
Route::post('google' , [
'as' => 'verify.index',
'uses' => 'verify@verifyIdToken'
]);
if (Auth::guest()) {
Route::get('/', function () {
return view('welcome');
});
} else {
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
return view('mainview');
});
Route::get('mainview', function () {
return view('mainviewMolly');
});
});
}
我的控制器:
namespace App\Http\Controllers;
use App\Email;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Google_Client;
use Auth;
use App\User;
class verify extends Controller
{
public function verifyIdToken(Request $request)
{
$user = User::where('name', 'Molly')->first();
Auth::login($user);
if (Auth::check($user))
{
return response()->json(['Logged In' => "Yes!"]);
}
}
}
所以我对上面的控制器做了一个ajax请求,当我得到json响应时说我登录了,然而当我刷新主页时,我仍然得到欢迎页面而不是mainview
。为什么会这样?
我的ajax请求:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
method: "POST",
url: "http://mysite.come/google",
data: { email: profile.getEmail(),
id: id_token }
}).done(function() {
$("#google").text("You Are Indeed Who I Thought You Were! :)");
}).fail(function() {
$("#google").text("You Are Not Who I Thought You Were, Please Go Away!");
setTimeout(function() {
window.location = "https://en.wikipedia.org/wiki/Special:Random";
//}, 4000);
}, 9999999000);
})