Reputation: 225
I use codeigniter platform to build a website with facebook login api.
When I tried to login with facebook account at first time, it worked fine.
However, if I use facebook login again on next day, I get this error message.
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user.
It looks like I don't do redirect or didn't get a new token.
here is the facebook login script.
function fblogin()
{
// Get User Details
$fb_id = $this->facebook->getUser();
// User is found
if( isset($fb_id) )
{
// Get Facebook User profile
$user_profile = $this->facebook->api('/me');
// User already has an account log them in
if( $this->join->chk_user_facebook_id($fb_id) == USER_ID_EXIST)
{
$u_data = $this->user->get_facebook_user_info($fb_id);
//update facebook token
$facebook_token = $this->input->get('code');
$query = 'UPDATE '.T_USER_ACCOUNT.' SET u_facebook_token = "'.$facebook_token.'" WHERE u_facebook_id = "'.$fb_id.'" ';
$this->db->query($query);
//save session
$l_data = array(
SESSION_USERID => $u_data[0]['u_id'],
SESSION_USERNAME => $u_data[0]['u_name'],
SESSION_USEREMAIL => $u_data[0]['u_email'],
SESSION_USERAUTH => $u_data[0]['u_auth']
);
$this->session->set_userdata($l_data);
redirect('/home');
}
// This is a new user add them to users table and login
else
{
$facebook_id = $user_profile['id'];
$j_data = array(
'u_id' => $facebook_id,
'u_facebook_id' => $facebook_id,
'u_name' => $user_profile['name'],
'u_first_name' => $user_profile['first_name'],
'u_last_name' => $user_profile['last_name'],
'u_facebook_token' => $this->input->get('code'),
);
$this->join->facebook_join($j_data);
// Check if offline access
$offline = $this->config->item('facebook_offline', 'tank_auth_social');
// follow admin account
$this->load->model('follow/following_model', 'following');
$this->following->exec($facebook_id, GPON_DEFAULT_ID);
// Get user information
$data = $this->user->get_facebook_user_info($fb_id);
$l_data = array(
SESSION_USERID => $data[0]['u_id'],
SESSION_USERNAME => $data[0]['u_name'],
SESSION_USEREMAIL => $data[0]['u_email'],
SESSION_USERAUTH => $data[0]['u_auth']
);
$this->session->set_userdata($l_data);
// Find facebook friends in the service and follow them.
$this->chk_facebook_friend();
redirect('/account/welcome');
}
}
else
{
redirect('/auth/login');
}
}
Upvotes: 1
Views: 3957
Reputation: 14438
You should replace if( isset($fb_id) )
with if( $fb_id) )
because the function $this->facebook->getUser()
returns 0 when there is no user.
Upvotes: 1