/** * Obtain the user information from Twitter. * * @return Response */ public function handleTwitterProviderCallback() { /* * -------------------------------------------------------------------------- * Login with twitter * -------------------------------------------------------------------------- * Initiating twitter driver and retrieve authenticate twitter login, * check if the user has been registered before, if they doesn't exist * create the new one then authenticating them and redirect. */ $user = Socialite::driver('twitter')->user(); $contributor = Contributor::whereVendor('twitter')->whereToken($user->id); if ($contributor->count() == 0) { /* * -------------------------------------------------------------------------- * Populate twitter data * -------------------------------------------------------------------------- * Collect the twitter basic data and create new contributor, * the data including banner as contributor cover, twitter avatar * as contributor avatar and twitter profile. */ $contributor = new Contributor(); $avatar = file_get_contents($user->avatar_original); file_put_contents('images/contributors/twitter-' . $user->id . '.jpg', $avatar); $cover = file_get_contents($user->user['profile_banner_url']); file_put_contents('images/covers/twitter-' . $user->id . '.jpg', $cover); $contributor->token = $user->id; $contributor->api_token = str_random(60); $contributor->name = $user->name; $contributor->username = $user->nickname . '.twitter'; $contributor->password = Hash::make(uniqid()); $contributor->email = $user->nickname . '@domain.com'; $contributor->vendor = 'twitter'; $contributor->status = 'activated'; $contributor->location = $user->user['location']; $contributor->about = $user->user['description']; $contributor->twitter = 'https://twitter.com/' . $user->nickname; $contributor->avatar = 'twitter-' . $user->id . '.jpg'; $contributor->cover = 'twitter-' . $user->id . '.jpg'; $contributor->save(); /* * -------------------------------------------------------------------------- * Create register activity * -------------------------------------------------------------------------- * Create new instance of Activity and insert register activity. */ Activity::create(['contributor_id' => $contributor->id, 'activity' => Activity::registerActivity($contributor->username, 'twitter')]); $this->sendAdminContributorNotification($contributor); } Auth::login($contributor->first()); return redirect()->route('account.stream'); }
/** * Register via twitter. * * @param Request $request * @return mixed */ public function registerTwitter(Request $request) { $twitterId = $request->input('id'); $contributor = Contributor::whereVendor('twitter')->whereToken($twitterId)->first(); if (empty($contributor)) { $isInvalid = Contributor::whereEmail($request->input('email'))->orWhere('username', '=', $request->input('username'))->first(); if ($isInvalid) { return response()->json(['request_id' => uniqid(), 'status' => 'exist', 'login' => 'restrict', 'message' => 'Email is already exist, registered via ' . $isInvalid->vendor, 'timestamp' => Carbon::now()], 400); } $contributor = new Contributor(); $cover = file_get_contents($request->input('cover')); file_put_contents('images/covers/twitter-' . $twitterId . '.jpg', $cover); $avatar = file_get_contents($request->input('avatar')); file_put_contents('images/contributors/twitter-' . $twitterId . '.jpg', $avatar); $contributor->token = $twitterId; $contributor->api_token = str_random(60); $contributor->name = $request->input('name'); $contributor->username = $request->input('username'); $contributor->password = Hash::make(uniqid()); $contributor->email = $request->input('username') . '@twitter.com'; $contributor->vendor = 'twitter'; $contributor->status = 'activated'; $contributor->location = $request->input('location'); $contributor->about = $request->input('about'); $contributor->twitter = 'https://www.twitter.com/' . $request->input('username'); $contributor->avatar = 'twitter-' . $twitterId . '.jpg'; $contributor->cover = 'twitter-' . $twitterId . '.jpg'; if ($contributor->save()) { Activity::create(['contributor_id' => $contributor->id, 'activity' => Activity::registerActivity($contributor->username, 'twitter')]); $this->sendAdminContributorNotification($contributor); $facebookUser = Contributor::whereToken($twitterId)->firstOrFail(); $user = $contributor->profile($facebookUser->username, true); $user->article_total = $user->articles()->where('status', 'published')->count(); $user->followers_total = $user->followers()->count(); $user->following_total = $user->following()->count(); return response()->json(['request_id' => uniqid(), 'status' => $user->status, 'login' => 'granted', 'message' => 'Registering twitter is success', 'timestamp' => Carbon::now(), 'user' => $user], 200); } else { return response()->json(['request_id' => uniqid(), 'status' => 'failure', 'login' => 'restrict', 'message' => Lang::get('alert.database.generic'), 'timestamp' => Carbon::now()], 500); } } else { $user = $contributor->profile($contributor->username, true); $user->article_total = $user->articles()->where('status', 'published')->count(); $user->followers_total = $user->followers()->count(); $user->following_total = $user->following()->count(); return response()->json(['request_id' => uniqid(), 'status' => $user->status, 'login' => 'granted', 'message' => 'Login twitter is success', 'timestamp' => Carbon::now(), 'user' => $user], 200); } }