public function twitchCallback(Request $request) { $twitch = app('twitch'); $code = $request->get('code'); $state = $request->get('state'); $result = $twitch->checkAuth($code, $state); if ($result) { $identity = $twitch->getIdentity(); if (!$identity) { return Redirect::back()->withErrors(['twitch' => 'Failed twitch login']); } $localUser = User::oauth($identity->_id, 'twitch')->first(); if ($localUser) { if ($localUser->is_active == 0) { LogMapper::log('client_login', $localUser->id, 'banned'); return redirect('/')->withErrors(['login' => 'Your account is deactivated. Please contact admin ASAP']); } Auth::loginUsingId($localUser->id); $this->updateTwitchProfile($localUser, $identity); $localUser->last_activity = \Carbon\Carbon::now(); LogMapper::log('twitch_login', $localUser->id); return redirect('/user/twitcher'); } else { $data = ['name' => $identity->name, 'email' => $identity->email, 'password' => '']; $localUser = $this->create($data); $localUser->role = 'user'; $localUser->type = 'twitcher'; $localUser->provider = 'twitch'; $localUser->oauth_id = $identity->_id; $referrer = $request->cookie('referrer', 0); if ($referrer > 0) { $referrer = User::find($referrer); if ($referrer) { $localUser->referral_id = $referrer->id; } NotificationMapper::referralAdded($localUser, $referrer); } $localUser->save(); Auth::loginUsingId($localUser->id); $this->updateTwitchProfile($localUser, $identity); LogMapper::log('twitch_register', $localUser->id); NotificationMapper::registration($localUser); return redirect('/user/twitcher/profile'); } } else { return Redirect::back()->withErrors(['twitch' => 'Failed twitch login']); } }