public function github() { $code = Input::get('code'); OAuth::setHttpClient('CurlClient'); $github = OAuth::consumer('GitHub'); if (!empty($code)) { $github->requestAccessToken($code); $result = json_decode($github->request('user/emails')); $email = $result[0]; $user = User::where('email', '=', $email)->get()->first(); if (count($user) > 0) { Auth::login($user); return Redirect::to('/contacts'); } else { $user = new User(); $user->email = $email; $user->save(); Auth::login($user); return Redirect::to('/contacts'); } } else { $url = $github->getAuthorizationUri(); return Redirect::to((string) $url); } }
public function loginWithGoogle() { // 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); if (User::where('email', '=', $result['email'])->first()) { // Je suis reconnu donc jme connecte $user = User::where('email', '=', $result['email'])->first(); Auth::login($user); return Redirect::to('/'); } else { // Cet email n'est pas dans la base donc on va ajouter l'utilisateur $user_id = DB::table('users')->insertGetId(array('email' => $result['email'], 'g_id' => $result['id'], 'first_name' => $result['given_name'], 'last_name' => $result['family_name'], 'gender' => $result['gender'])); $user = User::find($user_id); Auth::login($user); return Redirect::to('/'); } } else { // get googleService authorization $url = $googleService->getAuthorizationUri(); // return to google login url return Redirect::to((string) $url); } }