Exemplo n.º 1
0
 public function action_callback()
 {
     // Opauth can throw all kinds of nasty bits, so be prepared
     try {
         // get the Opauth object
         $opauth = \Auth_Opauth::forge(false);
         // and process the callback
         $status = $opauth->login_or_register();
         // fetch the provider name from the opauth response so we can display a message
         $provider = $opauth->get('auth.provider', '?');
         // deal with the result of the callback process
         switch ($status) {
             // a local user was logged-in, the provider has been linked to this user
             case 'linked':
                 // inform the user the link was succesfully made
                 \Messages::success(sprintf(__('login.provider-linked'), ucfirst($provider)));
                 // and set the redirect url for this status
                 $url = 'dashboard';
                 break;
                 // the provider was known and linked, the linked account as logged-in
             // the provider was known and linked, the linked account as logged-in
             case 'logged_in':
                 // inform the user the login using the provider was succesful
                 \Messages::success(sprintf(__('login.logged_in_using_provider'), ucfirst($provider)));
                 // and set the redirect url for this status
                 $url = 'dashboard';
                 break;
                 // we don't know this provider login, ask the user to create a local account first
             // we don't know this provider login, ask the user to create a local account first
             case 'register':
                 // inform the user the login using the provider was succesful, but we need a local account to continue
                 \Messages::info(sprintf(__('login.register-first'), ucfirst($provider)));
                 // and set the redirect url for this status
                 $url = 'user/register';
                 break;
                 // we didn't know this provider login, but enough info was returned to auto-register the user
             // we didn't know this provider login, but enough info was returned to auto-register the user
             case 'registered':
                 // inform the user the login using the provider was succesful, and we created a local account
                 \Messages::success(__('login.auto-registered'));
                 // and set the redirect url for this status
                 $url = 'dashboard';
                 break;
             default:
                 throw new \FuelException('Auth_Opauth::login_or_register() has come up with a result that we dont know how to handle.');
         }
         $url = str_replace('#_=_', '', $url);
         // redirect to the url set
         \Response::redirect($url);
     } catch (\OpauthException $e) {
         \Messages::error($e->getMessage());
         \Response::redirect_back();
     } catch (\OpauthCancelException $e) {
         // you should probably do something a bit more clean here...
         exit('It looks like you canceled your authorisation.' . \Html::anchor('users/oath/' . $provider, 'Click here') . ' to try again.');
     }
 }
Exemplo n.º 2
0
 protected function link_provider($userid)
 {
     // do we have an auth strategy to match?
     if ($authentication = \Session::get('auth-strategy.authentication', array())) {
         // don't forget to pass false, we need an object instance, not a strategy call
         $opauth = \Auth_Opauth::forge(false);
         // call Opauth to link the provider login with the local user
         $insert_id = $opauth->link_provider(array('parent_id' => $userid, 'provider' => $authentication['provider'], 'uid' => $authentication['uid'], 'access_token' => $authentication['access_token'], 'secret' => $authentication['secret'], 'refresh_token' => $authentication['refresh_token'], 'expires' => $authentication['expires'], 'created_at' => time()));
     }
 }
Exemplo n.º 3
0
 public function action_callback()
 {
     // Opauth can throw all kinds of nasty bits, so be prepared
     try {
         // get the Opauth object
         $opauth = \Auth_Opauth::forge(false);
         // and process the callback
         $status = $opauth->login_or_register();
         // fetch the provider name from the opauth response so we can display a message
         $provider = $opauth->get('auth.provider', '?');
         // deal with the result of the callback process
         switch ($status) {
             // a local user was logged-in, the provider has been linked to this user
             case 'linked':
                 // inform the user the link was succesfully made
                 // and set the redirect url for this status
                 Session::set('success', 'You have connected your ' . $provider . ' account!');
                 break;
                 // the provider was known and linked, the linked account as logged-in
             // the provider was known and linked, the linked account as logged-in
             case 'logged_in':
                 // inform the user the login using the provider was succesful
                 // and set the redirect url for this status
                 break;
                 // we don't know this provider login, ask the user to create a local account first
             // we don't know this provider login, ask the user to create a local account first
             case 'register':
                 // inform the user the login using the provider was succesful, but we need a local account to continue
                 // and set the redirect url for this status
                 switch ($provider) {
                     case 'Twitter':
                         $user_login = $opauth->get('auth.raw.screen_name');
                         $email = $opauth->get('auth.raw.screen_name') . '@twitter.com';
                         break;
                     case 'Google':
                         $user_login = str_replace('@gmail.com', '', $opauth->get('auth.raw.email'));
                         $email = $opauth->get('auth.raw.email');
                         break;
                     case 'Facebook':
                         $user_login = $opauth->get('auth.raw.username');
                         $email = $opauth->get('auth.raw.username') . '@facebook.com';
                         break;
                 }
                 // call Auth to create this user
                 $found_user = Model_User::query()->where('username', $user_login)->or_where('email', $email)->get_one();
                 if (empty($found_user) === false) {
                     if ($found_user->email == $email) {
                         // FORCE LOGIN AND REGISTER
                         Auth::force_login($found_user->id);
                     } else {
                         // Username already taken
                         Session::set('error', $user_login . ' , Username already taken, please register manually or try a differnt account');
                         Response::Redirect(Uri::Base());
                     }
                 } else {
                     $user_id = \Auth::create_user($user_login, md5($opauth->get('auth.credentials.token')), $email, \Config::get('application.user.default_group', 3), array('fullname' => $opauth->get('auth.info.name')));
                     Controller_Auth::Create_User($opauth, $user_id);
                 }
                 $opauth->login_or_register();
                 Session::set('success', 'You have connected your ' . $provider . ' account!');
                 break;
                 // we didn't know this provider login, but enough info was returned to auto-register the user
             // we didn't know this provider login, but enough info was returned to auto-register the user
             case 'registered':
                 // inform the user the login using the provider was succesful, and we created a local account
                 // and set the redirect url for this status
                 break;
             default:
                 throw new \FuelException('Auth_Opauth::login_or_register() has come up with a result that we dont know how to handle.');
         }
         // redirect to the url set
         \Response::redirect(Uri::Base());
     } catch (\OpauthException $e) {
         Session::set('error', ucfirst($e->getMessage()) . '!');
         \Response::redirect_back();
     } catch (\OpauthCancelException $e) {
         Session::set('error', 'Something went wrong!');
         \Response::redirect_back();
     }
 }