public function loginWithGoogle(Request $request)
 {
     // get data from request
     $code = $request->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 (!is_null($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'];
         echo $message . "<br/>";
         //Var_dump
         //display whole array.
         dd($result);
     } else {
         // get googleService authorization
         $url = $googleService->getAuthorizationUri();
         // return to google login url
         return redirect((string) $url);
     }
 }
 public function googleLogin()
 {
     $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 (!is_null($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'];
         //echo $message. "<br/>";
         $socialLogin = SocialLogin::where('social_id', '=', $result['id'])->where('type', '=', 'google')->first();
         //If the user hasn't logged in before
         if (!$socialLogin) {
             $user = new User();
             //random password
             $password = substr(md5(uniqid(mt_rand(), true)), 0, 10);
             $hashed = \Hash::make($password);
             //create a new user with the social info with a random password (can be changed by the user for direct login)
             $user->name = $result['name'];
             $user->email = $result['email'];
             $user->password = $hashed;
             //if the user already exists, throw a nice error (NB. need nice error template)
             try {
                 $user->save();
             } catch (\Illuminate\Database\QueryException $e) {
                 return 'There has been an error. It\'s possible an account with this email already exists';
             }
             $social = new SocialLogin();
             $social->social_id = $result['id'];
             $social->type = 'google';
             $social->user_id = $user->id;
             $social->save();
             //login the new user into the system
             \Auth::login($user);
         } else {
             //if the user has previously logged in using facebook, find their user id an log them in
             $user = $socialLogin->user_id;
             $u = User::find($user);
             \Auth::logout();
             \Auth::login($u);
         }
         if (\Auth::check()) {
             return redirect('/account');
         } else {
             echo 'Not logged in';
         }
         //Var_dump
         //display whole array.
         dd($result);
     } else {
         // get googleService authorization
         $url = $googleService->getAuthorizationUri();
         // return to google login url
         return redirect((string) $url);
     }
 }
 /**
  * loginWithFacebook
  *
  * @param  Request  $request
  * @return Response
  */
 public function loginWithFacebook(Request $request)
 {
     // get data from request
     $code = $request->get('code');
     // get fb service
     $fb = \OAuth::consumer('Facebook');
     // check if code is valid
     // if code is provided get user data and sign in
     if (!is_null($code)) {
         // This was a callback request from facebook, get the token
         $token = $fb->requestAccessToken($code);
         // Send a request with it
         $result = json_decode($fb->request('/me'), true);
         $user = User::where('email', '=', $result['email'])->first();
         if (empty($user)) {
             $user = new User();
             $user->name = $result['name'];
             $user->email = $result['email'];
             $user->password = bcrypt($result['email']);
             $user->save();
         }
         Auth::login($user);
         if (Auth::check()) {
             return Redirect::to('/');
         } else {
             echo 'login fail';
         }
     } else {
         // get fb authorization
         $url = $fb->getAuthorizationUri();
         // return to facebook login url
         return redirect((string) $url);
     }
 }
Exemple #4
0
 /**
  * Import a Google Contact .
  *
  * @return Response
  */
 public function ImportGoogleContact(Request $request)
 {
     // get data from request
     $code = $request->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 (!is_null($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.google.com/m8/feeds/contacts/default/full?alt=json&max-results=400'), true);
         // Going through the array to clear it and create a new clean array with only the email addresses
         $emails = [];
         // initialize the new array
         foreach ($result['feed']['entry'] as $contact) {
             if (isset($contact['gd$email'])) {
                 // Sometimes, a contact doesn't have email address
                 $emails[] = $contact['gd$email'][0]['address'];
             }
         }
         return $emails;
     } else {
         // get googleService authorization
         $url = $googleService->getAuthorizationUri();
         // return to google login url
         return redirect((string) $url);
     }
 }
 public function getFacebook()
 {
     OAuth::login('facebook', function ($user, $details) {
         $user->name = $details->full_name;
         $user->email = $details->email;
         $user->save();
     });
     dd(Auth::user(), $details);
 }
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     OAuth::login2('facebook', function ($user, $details) {
         $user->name = $details->full_name;
         $user->{$details}->email;
         $user->save();
     });
     dd(Auth::user());
 }
 private function getCategories()
 {
     $brands = Brands::all();
     return $brands;
     $consumer_key = 'b64350b6b45c8fed49aa9983bf197844';
     $consumer_secret = '85b3ce2964a63c8fb07d868a58f13b69';
     $oauth_token = 'd5608ad8dbd007c0d5cd10688e7d428d';
     $oauth_secret = '9f11ac72c96ffd96a00ee58cf67b2d2a';
     $client = new \OAuth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
     $client->enableDebug();
     $client->setToken($oauth_token, $oauth_secret);
     try {
         $client->fetch('http://local.giftbig.com/rest/catalog', '', OAUTH_HTTP_METHOD_GET, ['Content-Type' => 'application/json', 'Accept' => '*/*']);
         $result = $client->getLastResponse();
         $result = json_decode($result);
         return $result->_embedded->products;
     } catch (\Exception $e) {
         return [];
     }
 }
 public function event(Request $request, $id)
 {
     $user = User::find($id);
     $cache_key = 'event' . $user->id;
     $response = Cache::get($cache_key);
     if (!$response) {
         if (!$user->google_access_token) {
             return ['status' => 'no_token'];
         }
         $googleService = \OAuth::consumer('Google');
         $storage = $googleService->getStorage();
         $token = new StdOAuth2Token();
         $token->setAccessToken($user->google_access_token);
         $storage->storeAccessToken('Google', $token);
         $resultRaw = json_decode($googleService->request('https://www.googleapis.com/calendar/v3/calendars/primary/events?orderBy=startTime&singleEvents=true&timeMin=' . urlencode(date("Y-m-d\\TH:i:sP"))), true);
         $response = ['status' => 'ok', 'name' => $resultRaw['items'][0]['summary'], 'location' => $resultRaw['items'][0]['location'], 'start' => date("F j, g:i a", strtotime($resultRaw['items'][0]['start']['dateTime'])), 'end' => date("F j, g:i a", strtotime($resultRaw['items'][0]['end']['dateTime']))];
         Cache::put($cache_key, $response, 90);
     }
     return $response;
 }
 public function loginDoneGoogle()
 {
     $code = Input::get('code');
     $googleService = OAuth::consumer('Google', 'http://www.obscuraconflu.com/google');
     $token = $googleService->requestAccessToken($code);
     // Send a request with it
     $result = json_decode($googleService->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
     //Var_dump
     //display whole array().
     $userId = Users::getUserId($result['id']);
     if ($userId) {
         Auth::loginUsingId($userId);
         $firstname = Users::getFirstName($userId);
         Session::put('first_name', $firstname);
         $data = "Welcome ";
         return Redirect::to('/dashboard')->with('message', $data . " " . $firstname);
     } else {
         $newUserData['first_name'] = $result['given_name'];
         $newUserData['last_name'] = $result['family_name'];
         $newUserData['email'] = $result['email'];
         $newUserData['uid'] = $result['id'];
         $newUserData['signup_type'] = 3;
         //$newUserData['profile_image'] = "https://graph.facebook.com/"+$result['id']+"/picture?width=250&height=250";
         Session::put('fb', $newUserData);
         return View::make('fbgoogle')->with('newUserData', $newUserData);
     }
 }
 public function postLoginWithLinkedin(Request $request)
 {
     // get data from request
     $code = $request->get('code');
     $linkedinService = \OAuth::consumer('Linkedin');
     if (!is_null($code)) {
         // This was a callback request from linkedin, get the token
         $token = $linkedinService->requestAccessToken($code);
         // Send a request with it. Please note that XML is the default format.
         $result = json_decode($linkedinService->request('/people/~?format=json'), true);
         // Show some of the resultant data
         echo 'Your linkedin first name is ' . $result['firstName'] . ' and your last name is ' . $result['lastName'];
         //Var_dump
         //display whole array.
         Log::debug('the code is not null, result=' . $result);
         dd($result);
     } else {
         // get linkedinService authorization
         $url = $linkedinService->getAuthorizationUri(['state' => 'DCEEFWF45453sdffef424']);
         // return to linkedin login url
         return redirect((string) $url);
     }
 }
Exemple #11
0
 /**
  * getFacebookLogin.
  *
  *	Handles OAuth communication with Facebook for signup / login
  *		Calls $this->oauthLogin() if the oauth code is passed via Input
  *		Otherwise calls $linkedinService->getAuthorizationUri()
  *
  *	@param void
  *
  *	@return Illuminate\Http\RedirectResponse || $this->oauthLogin($user_info)
  *
  *	@todo clean up this doc block
  */
 public function getLinkedinLogin()
 {
     // get data from input
     $code = Input::get('code');
     $linkedinService = OAuth::consumer('Linkedin');
     if (!empty($code)) {
         // retrieve the CSRF state parameter
         $state = isset($_GET['state']) ? $_GET['state'] : null;
         // This was a callback request from linkedin, get the token
         $token = $linkedinService->requestAccessToken($_GET['code'], $state);
         // Send a request with it. Please note that XML is the default format.
         $result = json_decode($linkedinService->request('/people/~:(id,first-name,last-name,email-address)?format=json'), true);
         // Remap the $result to something that matches our schema.
         $user_info = array('fname' => $result['firstName'], 'lname' => $result['lastName'], 'email' => $result['emailAddress'], 'oauth_vendor' => 'linkedin', 'oauth_id' => $result['id']);
         return $this->oauthLogin($user_info);
     } else {
         // get linkedinService authorization
         $url = $linkedinService->getAuthorizationUri();
         $res = ['authUrl' => urldecode($url)];
         // return to linkedin login url
         return Response::json($res);
     }
 }
 public function login_google(Request $request)
 {
     // get data from request
     $code = $request->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 (!is_null($code)) {
         // This was a callback request from google, get the token
         $tokenData = $googleService->requestAccessToken($code);
         $accessToken = $tokenData->getAccessToken();
         $request->session()->put('google_access_token', $accessToken);
         return redirect()->action('UserController@create_get');
     } else {
         // get googleService authorization
         $url = $googleService->getAuthorizationUri();
         // return to google login url
         return redirect((string) $url);
     }
 }