/** * @param \Laravel\Socialite\Contracts\User $userData * @return User */ public function findByUsernameOrCreate($userData) { $user = User::firstOrCreate(['username' => $userData->getNickname(), 'email' => $userData->getEmail()]); $user->full_name = $userData->getName(); $user->save(); return $user; }
public static function confirmFacebook(User $localUser, SocialiteUser $facebookUser) { $nickname = $facebookUser->getNickname(); if ($nickname == '') { $nickname = $facebookUser->getId(); } $profile = $localUser->profile; $profile->facebook = 'http://facebook.com/' . $nickname; $profile->confirmed_facebook = true; $profile->save(); return $profile; }
/** * @param \Laravel\Socialite\Contracts\User $user * @return array */ private function githubUserToArray(GithubUser $user) { return ['id' => $user->getId(), 'name' => $user->getNickname(), 'email' => $user->getEmail(), 'github_id' => $user->getId(), 'github_url' => 'https://github.com/' . $user->getNickname(), 'image_url' => $user->getAvatar()]; }
/** * @param \Laravel\Socialite\Contracts\User $login * @return User */ private function createOrFindUser($login) { $user = User::firstOrNew(['email' => $login->getEmail()]); if ($user->exists) { return $user; } $user->name = $login->getName() ?: $login->getNickname(); $user->nickname = $login->getNickname(); $user->password = Hash::make($login->getId() . time()); $user->avatar = $login->getAvatar(); $user->admin = User::count() == 0; $user->save(); return $user; }
/** * Create new user if it does not exists. * * @param Provider * @param \Laravel\Socialite\Contracts\User * @return User */ public static function findOrCreate(Provider $provider, SocialUser $socialUser) { // If user already exists reuse it $user = self::where(['uuid' => $socialUser->getId(), 'provider_id' => $provider->id])->withTrashed()->first(); if ($user) { return $user; } // Create a new user $user = new static(); $user->uuid = $socialUser->getId(); $user->name = $socialUser->getName(); $user->nickname = $socialUser->getNickname(); $user->email = $socialUser->getEmail(); $user->avatar = $socialUser->getAvatar(); $user->provider_id = $provider->id; $user->language_id = app('language')->id; $user->role_id = Role::whereIsDefault(true)->firstOrFail()->id; $user->save(); return $user; }
/** * Validate data from a social provider. * * @param \Laravel\Socialite\Contracts\User * @return \Illuminate\Contracts\Support\MessageProvider */ public function validate(SocialUser $socialUser) { $input = ['uuid' => $socialUser->getId(), 'name' => $socialUser->getName(), 'nickname' => $socialUser->getNickname(), 'email' => $socialUser->getEmail(), 'avatar' => $socialUser->getAvatar()]; $customMessages = ['same' => _('Your account is not verified')]; return Validator::make($input, $this->rules, $customMessages)->setAttributeNames($this->labels)->errors(); }
protected function fillUser(User $user, array $user_data, UserContract $data, string $provider) { $mappings = ['name' => 'name', 'email' => 'email', 'avatar' => ['avatar', 'id'], 'picture' => 'avatar_original', 'bio' => ['bio', 'description'], 'birthday' => ['birthday', 'birth_year'], 'gender' => 'gender', 'rel:location' => 'location.name', 'rel:timezone' => 'timezone', 'rel:locale' => 'locale', 'rel:website' => 'website', "rel:{$provider}" => 'link', 'tagline' => ['work', 'company', 'headline', 'occupation']]; $relations_data = ['links' => []]; $getValue = function ($key) use($user_data, $data) { if (strpos($key, '.') === false) { return $user_data[$key] ?? $data->{$key} ?? null; } else { $value = $user_data; $sub_key = strtok($key, '.'); do { $value = $value[$sub_key]; } while ($sub_key = strtok('.')); return $value; } }; foreach ($mappings as $field => $key) { if (strpos($field, 'rel:') === false && !$user->{$field}) { //not a relation and is still unset if (is_array($key)) { foreach ($key as $key_option) { if ($value = $getValue($key_option)) { break; } } } else { $value = $getValue($key); } if (!isset($value)) { continue; //no data, so we can skip this field } switch ($field) { case 'name': if ($provider == 'google') { $value = trim($value['givenName'] . ' ' . $value['familyName']); if (!$value) { //yeah, hosted accounts may not even have a NAME (!!!) $email = $data->getEmail(); $value = substr($email, 0, strpos($email, '@')); //takes the email username... $value = strtr($value, ['.' => ' ', '_' => ' ', '-' => ' ']); //..replaces common placeholders.. $value = ucwords($value); //...and call it a "name" } } break; case 'birthday': switch ($provider) { case 'live': //TODO: user might have month + day without year. We should split the birthday field to accomodate this if ($user_data['birth_month'] && $user_data['birth_day'] && $user_data['birth_year']) { $value = date('Y-m-d', mktime(0, 0, 0, $user_data['birth_month'], $user_data['birth_day'], $user_data['birth_year'])); } break; default: $value = \DateTime::createFromFormat('m/d/Y', $value)->format('Y-m-d'); } break; case 'tagline': switch ($provider) { case 'github': $value = 'Developer' . ($value ? ' @ ' . $value : ''); break; case 'facebook': $last_job = current($value); $value = $last_job['position']['name'] . ' @ ' . $last_job['employer']['name']; break; case 'live': $value = isset($value[0]['employer']['name']) ? ($value[0]['position']['name'] ?? 'works') . ' @ ' . $value[0]['employer']['name'] : ''; break; } break; case 'avatar': switch ($provider) { case 'live': $value = "https://apis.live.net/v5.0/{$data->getId()}/picture"; break; } break; case 'gender': $value = strtoupper($value[0]); break; } $user->{$field} = $value; } else { $relation = substr($field, 4); switch ($relation) { case 'website': switch ($provider) { case 'twitter': $url = static::unshortenUrl($user_data['url']); break; case 'github': $url = $user_data['blog']; break; //seriously github? blog? hahah //seriously github? blog? hahah default: $url = $user_data['website'] ?? ''; break; } if ($url) { if (!preg_match('|^https?://|', $url)) { $url = 'http://' . $url; } $url = substr($url, 4); //personal website prefix length $relations_data['links']['personal website'] = $url; } break; case $provider: //provider's profile switch ($provider) { case 'twitter': case 'github': case 'bitbucket': $username = $data->getNickname(); break; case 'linkedin': $profile = $user_data['publicProfileUrl']; $username = substr($profile, strpos($profile, '/in/') + 4); break; case 'google': $username = $data->getId(); if ($user_data['isPlusUser']) { $relations_data['links']['google+'] = $data->getId(); } if (isset($user_data['urls']) && is_array($user_data['urls'])) { //TODO: should we use our entries from SocialNetwork instead? how? maybe spliting the URL field into verification_url (regex) and profile_url? $valid_networks = ['facebook', 'linkedin', 'twitter', 'youtube']; foreach ($user_data['urls'] as $entry) { $label = strtolower($entry['label']); $handle = substr($entry['value'], (strrpos($entry['value'], '/') ?: -1) + 1); if (in_array($label, $valid_networks)) { $relations_data['links'][$label] = $handle; } else { foreach ($valid_networks as $network) { if (strpos($entry['value'], $network . '.com')) { $relations_data['links'][$network] = $handle; } } } } } break; default: $username = $data->getId(); } session()->set('signup.main_provider_link', compact('provider', 'username')); break; case 'location': //facebook: location.name //twitter, github, bitbucket (empty?): location //linkedin: location.name (Rio de Janeiro Area, Brazil) + location.country.code (br) //google: placesLived[].primary=true ~ value //live: requires complete address permission :/ //TODO: add a location relationship here; don't forget to test with a testuser with no location! break; case 'locale': //facebook, live: locale //twitter: lang (en) //google: language //github, bitbucket: none? //TODO: add a language relationship here; don't forget to test with a testuser with no locale! break; case 'timezone': //facebook: timezone //TODO DAFUK timezone comes as -2 USELESS HALP //twitter: utc_offset (-10800) / timezone (Brasilia) //github, bitbucket, google, live: none? //TODO: add a timezone relationship here; don't forget to test with a testuser with no timezone! break; } } } // !ddd($user->getAttributes(), $username, $data, $relations_data); session()->set('signup.relations', $relations_data); }
private function populateSession(YoutubeUser $youtubeUser) { $session = $this->request->session(); $userSessionValues = ['youtube_id' => $youtubeUser->getId(), 'name' => $youtubeUser->getNickname(), 'avatar' => $youtubeUser->getAvatar(), 'token' => $youtubeUser->token]; $session->put('user', $userSessionValues); }