I have a dashboard feature that requires facebook login. If the token is expired, I catch that.
So far so good , however, I'd like the user to go off to login and come back to the same place.
This is the code:
try {
//make FB client
$fb = new Facebook( [
'app_id' => config( 'app.FACEBOOK_APPID' ),
'app_secret' => config( 'app.FACEBOOK_APPSECRET' ),
'default_graph_version' => 'v2.8',
] );
//Pull events realted to this user
$events = $fb->get( $endpoint, $accessToken )->getDecodedBody();
//catch exception if token is expired
} catch ( FacebookSDKException $e ) {
//redirect to facebook login
return redirect()->action('Auth\SocialiteLoginController@redirectToProvider', ['facebook']);
}
return $events;
My issue is , the return redirect()
statement, does not redirect the user to Facebook, but, it sends back the RedirectResponse
object to the calling function.
Better use socialite drive it will handle objects.
Redirect to facebook driver
return Socialite::driver('facebook')->redirect();
need to declare facebook redirect url in .env , facebook credentials page and local route page also.
public function handleFacebookCallback(Request $request){
$AccessToken = Socialite::driver('facebook')->getAccessTokenResponse($request->code);
if($token = $AccessToken['access_token']){
$facebook =Socialite::driver('facebook')->userFromToken($token);
if($facebook->id){
//insert process and get auth userkey
redirect('/login page');
}else{
redirect('/dashboard'); //not auth user
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.