我没有使用默认路由进行登录和注销,而我构建的这些自定义路由工作正常。我正在使用社交网站在我的应用中实施谷歌登录。这些是关于谷歌登录的web.php片段:
Route::get('/auth/google', 'LoginController@redirectToGoogle')->name('googleauth')->middleware('guest');
Route::get('/auth/google/callback', 'LoginController@handleGoogleCallback')->name('googlecall');
这是我在登录视图中得到的:
<form id="googleLoginForm" name="googleLoginForm" method="GET" action="{{ route('googleauth') }}">
{!! csrf_field() !!}
<br> <br>
<button id="btnGoogleLogin" name="btnGoogleLogin" type="submit" class="btn btn-default">G login</button>
</form>
这是我的控制器,位于app / http / controllers / auth:
namespace MyApp\Http\Controllers\Auth;
use MyApp\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Socialite;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function redirectToGoogle(){
return Socialite::driver('google')->redirect();
}
public function handleGoogleCallback(){
$user = Socialite::driver('google')->user();
$user->token;
}
}
我认为我已正确安装了社交网站,我还使用google api控制台生成了密码和客户端ID。少了什么东西?我无法理解为什么它不起作用
答案 0 :(得分:1)
您必须在路由声明中附加名称空间:
Route::get('/auth/google', 'Auth\LoginController@redirectToGoogle')->name('googleauth')->middleware('guest');
Route::get('/auth/google/callback', 'Auth\LoginController@handleGoogleCallback')->name('googlecall');
或者你可以group them by namespace,例如:
Route::namespace('Auth')->group(function () {
Route::get('/auth/google', 'LoginController@redirectToGoogle');
Route::get('/auth/google/callback', 'LoginController@handleGoogleCallback');
});