public function handle()
 {
     $userFacebookTokens = UserSocialToken::where('type', '=', 'facebook')->where(function ($query) {
         $query->where("expires_at", '>', Carbon::now())->orWhereNull("expires_at");
     })->get();
     foreach ($userFacebookTokens as &$userFacebookToken) {
         $this->dispatch(new GetLatestFacebookPostsForUser($userFacebookToken));
     }
 }
 public function handle()
 {
     $userTwitterTokens = UserSocialToken::where('type', '=', 'twitter')->where(function ($query) {
         $query->where("expires_at", '>', Carbon::now())->orWhereNull("expires_at");
     })->whereNotNull('user_social_token.user_id')->get();
     foreach ($userTwitterTokens as &$userTwitterToken) {
         $this->dispatch(new GetLatestTwitterPostsForUser($userTwitterToken));
     }
 }
        }
    } catch (Exception $e) {
        abort(422, 'Error logging into twitter!');
    }
}]);
Route::get('instagram/login/{userId}', ['as' => 'instagram.login', function ($userId) {
    Request::session()->put('instagram_user_id', $userId);
    return Socialite::driver('instagram')->redirect();
}]);
Route::get('instagram/callback', ['as' => 'instagram.callback', function () {
    try {
        if (Request::session()->has('instagram_user_id')) {
            $user = Socialite::driver('instagram')->user();
            $tokenId = 0;
            if (!($socialAccount = UserSocialToken::where('type', '=', 'instagram')->where('user_id', '=', Request::session()->get('instagram_user_id'))->whereNull('expires_at')->first())) {
                $userSocialToken = new UserSocialToken();
                $userSocialToken->type = 'instagram';
                $userSocialToken->expires_at = null;
                $userSocialToken->short_lived_token = $user->token;
                $userSocialToken->long_lived_token = $user->token;
                $userSocialToken->entity_id = $user->id;
                $userSocialToken->entity_name = $user->nickname;
                $userSocialToken->user_id = Request::session()->get('instagram_user_id');
                $userSocialToken->save();
                $tokenId = $userSocialToken->id;
            } else {
                $socialAccount->short_lived_token = $user->token;
                $socialAccount->long_lived_token = $user->token;
                $socialAccount->save();
                $tokenId = $socialAccount->id;
            }