/** * Handle the event. * * @param \App\WS\Message $message * @param \App\WS\Connection $conn * @return void */ public function handle($message, $conn) { $request = Request::capture(); $request->query->add(['code' => $message->data('code')]); $provider = $message->data('provider'); $user = Socialite::driver($provider)->stateless()->setRequest($request)->user(); if ($u = User::findByProvider($provider, $user->getId())) { return $this->send($message->from(), $message->event(), $u->setHidden([])); } if (User::where('email', $user->getEmail())->exists()) { return $this->send($message->from(), $message->event(), 'The email has already been taken.', 422); } $user = User::create(['name' => $user->getName(), 'email' => $user->getEmail(), 'avatar' => $user->getAvatar(), 'provider' => $provider, 'provider_id' => $user->getId(), 'api_token' => str_random(32)]); return $this->send($message->from(), $message->event(), $user->setHidden([]), 201); }
/** * Handle the event. * * @param \App\WS\Message $message * @param \App\WS\Connection $conn * @return void */ public function handle($message, $conn) { $clients = $conn->clients(); foreach ($clients as $client) { if ($message->from() !== $client) { $this->send($client, $message->event(), $message->data()); } } }
/** * Handle the event. * * @param \App\WS\Message $message * @param \App\WS\Connection $conn * @return void */ public function handle($message, $conn) { $url = Socialite::driver($message->data('provider'))->stateless()->redirect()->headers->get('Location'); return $this->send($message->from(), $message->event(), $url); }
/** * Handle the event. * * @param \App\WS\Message $message * @param \App\WS\Connection $conn * @return void */ public function handle($message, $conn) { $user = User::findByToken($message->data('api_token')); return $this->send($message->from(), $message->event(), $user->setHidden([])); }
/** * Resolve user token. * * @param \App\WS\Message $message * @return void */ protected function resolveToken(Message $message) { $conn = $message->from(); $hash = $this->clients->getHash($conn); if (!isset($this->tokens[$hash])) { if ($token = $message->get('api_token')) { $this->tokens[$hash] = $token; } else { $this->tokens[$hash] = null; } } $message->setApiToken($this->tokens[$hash]); }