Пример #1
0
 public function google()
 {
     $settings = Setting::first();
     if ($settings->user_registration) {
         // get data from input
         $code = Input::get('code');
         // get google service
         $googleService = OAuth::consumer('Google');
         // check if code is valid
         // if code is provided get user data and login
         if (!empty($code)) {
             // This was a callback request from google, get the token
             $token = $googleService->requestAccessToken($code);
             // Send a request with it
             $result = json_decode($googleService->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
             // $message = 'Your unique Google user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
             // dd($result);
             $oauth_userid = $result['id'];
             $oauth_username = Helper::slugify($result['name']);
             $oauth_email = $result['email'];
             if (!isset($result['picture'])) {
                 $oauth_picture = NULL;
             } else {
                 $oauth_picture = $result['picture'];
             }
             if (isset($oauth_userid) && isset($oauth_username) && isset($oauth_email)) {
                 $google_auth = OauthGoogle::where('oauth_userid', '=', $oauth_userid)->first();
                 if (isset($google_auth->id)) {
                     $user = User::find($google_auth->user_id);
                 } else {
                     // Execute Add or Login Oauth User
                     $user = User::where('email', '=', $oauth_email)->first();
                     if (!isset($user->id)) {
                         $username = $this->create_username_if_exists($oauth_username);
                         $email = $oauth_email;
                         $password = Hash::make($this->rand_string(15));
                         $avatar = $oauth_picture != NULL ? ImageHandler::uploadImage($oauth_picture, 'avatars', $username, 'url') : NULL;
                         $user = $this->new_user($username, $email, $password, $avatar);
                         $this->new_user_points($user->id);
                         $new_oauth_user = new OauthGoogle();
                         $new_oauth_user->user_id = $user->id;
                         $new_oauth_user->oauth_userid = $oauth_userid;
                         $new_oauth_user->save();
                     } else {
                         // Redirect and send error message that email already exists. Let them know that they can request to reset password if they do not remember
                         return Redirect::to('/')->with('error', Lang::get('lang.oauth_email_used'));
                     }
                 }
                 // Redirect to new User Login;
                 Auth::login($user);
                 $this->add_user_login_point();
                 return Redirect::to('/')->with('success', Lang::get('lang.google_success'));
             } else {
                 // Something went wrong, redirect and send error msg
                 echo Lang::get('lang.oauth_error');
                 echo '<br />Info retrieved:<br />';
                 echo '<br />userid: ' . $oauth_userid;
                 echo '<br />username: '******'<br />email: ' . $oauth_email;
                 echo '<br />picture: ' . $oauth_picture;
             }
         } else {
             // get googleService authorization
             $url = $googleService->getAuthorizationUri();
             // return to facebook login url
             return Response::make()->header('Location', (string) $url);
         }
     } else {
         return Redirect::to('/')->with(array('note' => Lang::get('lang.signup_reg_closed'), 'note_type' => 'error'));
     }
 }
Пример #2
0
 public function google()
 {
     if (Session::has('authtoken')) {
         Session::put('auth', '1');
     }
     $settings = Setting::first();
     if ($settings->user_registration) {
         // get data from input
         $code = Input::get('code');
         // get google service
         $googleService = OAuth::consumer('Google');
         // check if code is valid
         // if code is provided get user data and sign in
         if (!empty($code)) {
             // This was a callback request from google, get the token
             $token = $googleService->requestAccessToken($code);
             // Send a request with it
             $result = json_decode($googleService->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
             // $message = 'Your unique Google user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
             // dd($result);
             $oauth_userid = $result['id'];
             $oauth_username = slugify($result['name']);
             $oauth_email = $result['email'];
             if (!isset($result['picture'])) {
                 $oauth_picture = NULL;
             } else {
                 $oauth_picture = $result['picture'];
             }
             if (isset($oauth_userid) && isset($oauth_username) && isset($oauth_email)) {
                 $google_auth = OauthGoogle::where('oauth_userid', '=', $oauth_userid)->first();
                 if (isset($google_auth->id)) {
                     $user = User::find($google_auth->user_id);
                 } else {
                     // Execute Add or Login Oauth User
                     $user = User::where('email', '=', $oauth_email)->first();
                     if (!isset($user->id)) {
                         $username = $this->create_username_if_exists($oauth_username);
                         $email = $oauth_email;
                         $password = Hash::make($this->rand_string(15));
                         $avatar = $oauth_picture != NULL ? $this->uploadImageFromURL($oauth_picture, $username) : NULL;
                         $user = $this->new_user($username, $email, $password, $avatar);
                         $this->new_user_points($user->id);
                         $new_oauth_user = new OauthGoogle();
                         $new_oauth_user->user_id = $user->id;
                         $new_oauth_user->oauth_userid = $oauth_userid;
                         $new_oauth_user->save();
                     } else {
                         // Redirect and send error message that email already exists. Let them know that they can request to reset password if they do not remember
                         return Redirect::to('signin')->with('errors', 'This email is already in use.');
                     }
                 }
                 // Redirect to new User Login;
                 Auth::login($user, true);
                 if (Session::has('authtoken')) {
                     $user = User::where('id', '=', Auth::user()->id)->first();
                     if (count($user) != 0) {
                         $api_key = md5(microtime() . rand());
                         $token = Session::get('authtoken');
                         $user->token = $token;
                         $user->api_key = $api_key;
                         $user->api_key_date = date("Y-m-d H:i:s");
                         $user->save();
                     }
                     Session::forget('authtoken');
                     Session::forget('auth');
                     return "<script type='text/javascript'> window.close();</script>";
                 }
                 return Redirect::intended('/');
             } else {
                 // Something went wrong, redirect and send error msg
                 echo 'Some Oauth information was not able to get retrieved. Please try again.';
                 echo '<br />Info retrieved:<br />';
                 echo '<br />userid: ' . $oauth_userid;
                 echo '<br />username: '******'<br />email: ' . $oauth_email;
                 echo '<br />picture: ' . $oauth_picture;
             }
         } else {
             // get googleService authorization
             $url = $googleService->getAuthorizationUri();
             // return to facebook login url
             return Response::make()->header('Location', (string) $url);
         }
     } else {
         return Redirect::to('signin')->with(array('errors' => 'Sorry, Registration has been closed.'));
     }
 }