public function getLink(Request $request) { // валидация $paramsToValidate = array('provider' => $request->provider); $validatorRules = array('provider' => ['string', 'required']); $validator = Validator::make($paramsToValidate, $validatorRules); if ($validator->fails()) { return $this->response->validatorError($validator->messages()); } switch ($request->provider) { case 'facebook': $fb = new Facebook(); return $this->response->onSuccess($fb->getOAuthLink()); break; default: return $this->response->onError('unknown social provider'); break; } }
public function register(Request $request) { $fb = new Facebook(); // валидация $paramsToValidate = array('email' => $request->email, 'password' => $request->password, 'provider' => $request->provider); $validatorRules = array('email' => ['required', 'email'], 'password' => ['required', 'min:3'], 'provider' => ['string']); $validator = Validator::make($paramsToValidate, $validatorRules); if ($validator->fails()) { return $this->response->validatorError($validator->messages()); } // проверим не занят ли емейл $user = User::findByEmail($request->email); if ($user) { return $this->response->onError('Email is already belongs to some of our users'); } // зарегистрируем и авторизуем пользователя $user = User::create(array('name' => '', 'email' => $request->email, 'password' => md5($request->password))); Auth::login($user, true); // если юзер зарегался через соц сети, то получим данные из них (фото, имя) if ($request->provider === 'facebook' && $fb->token()) { $fbUserData = $fb->api('/me'); $fbPhoto = $fb->getPhoto($fbUserData->id); $user->name = $fbUserData->name; if (isset($fbPhoto->data->url)) { $photoDir = '/var/www/whatitmeans/data/www/what-it-means.ru/files/users/photos/' . $user->id; try { File::saveFromUrl($photoDir, 'main_photo.jpg', $fbPhoto->data->url); } catch (Exception $e) { } } UserSocGlue::create(array('user_id' => $user->id, 'provider' => 'facebook', 'provider_user_id' => $fbUserData->id)); $user->save(); } // вернем ответ return $this->response->onSuccess('User created'); }