public function callback($req, $res)
 {
     if ($req->query('error_reason')) {
         return $res->redirect('/');
     }
     // generate forceLogin redirect_uri
     if ($req->query('forceLogin')) {
         $this->app['config']->set('instagram.redirect_uri', $this->app['config']->get('instagram.redirect_uri') . '?forceLogin=t');
     }
     $instagram = $this->app['instagram'];
     /* authenticate the user with the instagram API */
     $authenticatedUser = false;
     try {
         if ($instagram->Users->Authorize($req->query('code'))) {
             $authenticatedUser = $instagram->Users->getCurrentUser();
         }
     } catch (\Exception $e) {
         $this->app['logger']->error($e);
     }
     if (!$authenticatedUser) {
         $this->app['errors']->push(['context' => 'user.login', 'error' => 'invalid_token', 'message' => 'Instagram: Login error. Please try again.']);
         $usersController = new \app\users\Controller($this->app);
         return $usersController->loginForm($req, $res);
     }
     /* fetch the user's full profile */
     try {
         $user_profile = $instagram->Users->Info($authenticatedUser['id'])->data;
     } catch (\Exception $e) {
         $this->app['logger']->error($e);
         return $res->setCode(500);
     }
     /* log the user in or kick off signup */
     $currentUser = $this->app['user'];
     $iid = $user_profile['id'];
     // generate parameters to update profile
     $profileUpdateArray = ['id' => $iid, 'access_token' => $instagram->getAccessToken()];
     // instagram id matches existing user?
     $user = User::findOne(['where' => ['instagram_id' => $iid]]);
     if ($user) {
         // check if we are dealing with a temporary user
         if (!$user->isTemporary()) {
             if ($user->id() != $currentUser->id()) {
                 if ($req->query('forceLogin') || !$currentUser->isLoggedIn()) {
                     // log the user in
                     $this->app['auth']->signInUser($user->id(), 'instagram');
                 } else {
                     // inform the user that the instagram account they are trying to
                     // connect belongs to someone else
                     return new View('switchingAccounts/instagram.tpl', ['title' => 'Switch accounts?', 'otherUser' => $user, 'otherProfile' => $user->instagramProfile()]);
                 }
             }
             $profile = new InstagramProfile($iid);
             // create or update the profile
             if ($profile->exists()) {
                 $profile->set($profileUpdateArray);
             } else {
                 $profile = new InstagramProfile();
                 $profile->create($profileUpdateArray);
             }
             // refresh profile from API
             $profile->refreshProfile($user_profile);
             return $this->finalRedirect($req, $res);
         } else {
             // show finish signup screen
             $req->setSession('iid', $iid);
             return $res->redirect('/signup/finish');
         }
     }
     if ($currentUser->isLoggedIn()) {
         // add to current user's account
         $currentUser->set('instagram_id', $iid);
     } else {
         // save this for later
         $req->setSession('iid', $iid);
     }
     $profile = new InstagramProfile($iid);
     // create or update the profile
     if ($profile->exists()) {
         $profile->set($profileUpdateArray);
     } else {
         // create profile
         $profile = new InstagramProfile();
         $profile->create($profileUpdateArray);
     }
     // refresh profile from API
     $profile->refreshProfile($user_profile);
     // get outta here
     if ($currentUser->isLoggedIn()) {
         $this->finalRedirect($req, $res);
     } else {
         $res->redirect('/signup/finish');
     }
 }
Ejemplo n.º 2
0
 public function callback($req, $res)
 {
     if ($req->query('denied')) {
         return $res->redirect('/');
     }
     $twitter = $this->twitter($req->session('oauth_token'), $req->session('oauth_token_secret'));
     $token_credentials = $twitter->getAccessToken($req->query('oauth_verifier'));
     if (!isset($token_credentials['oauth_token'])) {
         $this->app['errors']->push(['context' => 'user.login', 'error' => 'invalid_token', 'message' => 'Twitter: Invalid token. Please try again.']);
         $usersController = new \app\users\Controller();
         $usersController->injectApp($this->app);
         return $usersController->loginForm($req, $res);
     }
     $twitter = $this->twitter($token_credentials['oauth_token'], $token_credentials['oauth_token_secret'], false);
     // fetch profile
     $user_profile = $twitter->get('account/verify_credentials');
     if (isset($user_profile->errors)) {
         return $res->setBody('There was an error signing you into Twitter:<br/><pre>' . print_r($user_profile->errors, true) . '</pre>');
     }
     /* log the user in or kick off signup */
     $currentUser = $this->app['user'];
     $tid = $user_profile->id;
     // generate parameters to update profile
     $user_profile = (array) json_decode(json_encode($user_profile), true);
     $profileUpdateArray = ['id' => $tid, 'access_token' => $token_credentials['oauth_token'], 'access_token_secret' => $token_credentials['oauth_token_secret']];
     // twitter id matches existing user?
     $users = User::find(['where' => ['twitter_id' => $tid]]);
     if ($users['count'] == 1) {
         $user = $users['models'][0];
         // check if we are dealing with a temporary user
         if (!$user->isTemporary()) {
             if ($user->id() != $currentUser->id()) {
                 if ($req->query('forceLogin') || !$currentUser->isLoggedIn()) {
                     // log the user in
                     $this->app['auth']->signInUser($user->id(), 'twitter');
                 } else {
                     // inform the user that the twitter account they are trying to
                     // connect belongs to someone else
                     return new View('switchingAccounts/twitter', ['title' => 'Switch accounts?', 'otherUser' => $user, 'otherProfile' => $user->twitterProfile()]);
                 }
             }
             $profile = new TwitterProfile($tid);
             // create or update the profile
             if ($profile->exists()) {
                 $profile->set($profileUpdateArray);
             } else {
                 $profile = new TwitterProfile();
                 $profile->create($profileUpdateArray);
             }
             // refresh profile from API
             $profile->refreshProfile($user_profile);
             return $this->finalRedirect($req, $res);
         } else {
             // show finish signup screen
             $req->setSession('tid', $tid);
             return $res->redirect('/signup/finish');
         }
     }
     if ($currentUser->isLoggedIn()) {
         // add to current user's account
         $currentUser->set('twitter_id', $tid);
     } else {
         // save this for later
         $req->setSession('tid', $tid);
     }
     $profile = new TwitterProfile($tid);
     // create or update the profile
     if ($profile->exists()) {
         $profile->set($profileUpdateArray);
     } else {
         $profile = new TwitterProfile();
         $profile->create($profileUpdateArray);
     }
     // refresh profile from API
     $profile->refreshProfile($user_profile);
     // get outta here
     if ($currentUser->isLoggedIn()) {
         $this->finalRedirect($req, $res);
     } else {
         $res->redirect('/signup/finish');
     }
 }