1 | <?php |
||
13 | * Redirect the user to the provider authentication page. |
||
14 | * |
||
15 | * @param string $provider |
||
16 | * |
||
17 | * @return \Illuminate\Http\Response |
||
18 | */ |
||
19 | public function redirectToProvider(string $provider) |
||
20 | { |
||
21 | return Socialite::driver($provider)->redirect(); |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * Obtain the user information from Provider. |
||
26 | * |
||
27 | * @param string $provider |
||
28 | * |
||
29 | * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
||
30 | */ |
||
31 | public function handleProviderCallback(string $provider) |
||
32 | { |
||
33 | $providerUser = Socialite::driver($provider)->user(); |
||
34 | |||
35 | $attributes = [ |
||
36 | 'id' => $providerUser->id, |
||
37 | 'email' => $providerUser->email, |
||
38 | 'username' => $providerUser->nickname ?? trim(strstr($providerUser->email, '@', true)), |
||
39 | 'first_name' => trim(strstr($providerUser->name, ' ', true)), |
||
40 | 'last_name' => trim(strstr($providerUser->name, ' ')), |
||
41 | ]; |
||
42 | |||
43 | switch ($provider) { |
||
44 | case 'twitter': |
||
45 | $attributes['bio'] = $providerUser->user['description']; |
||
46 | $attributes['profile_picture'] = $providerUser->avatar_original; |
||
47 | break; |
||
48 | case 'github': |
||
49 | $attributes['bio'] = $providerUser->user['bio']; |
||
50 | $attributes['profile_picture'] = $providerUser->avatar; |
||
51 | break; |
||
52 | case 'facebook': |
||
53 | $attributes['profile_picture'] = $providerUser->avatar_original; |
||
54 | break; |
||
55 | case 'linkedin': |
||
56 | $attributes['bio'] = $providerUser->headline; |
||
57 | $attributes['profile_picture'] = $providerUser->avatar_original; |
||
58 | break; |
||
59 | case 'google': |
||
60 | $attributes['bio'] = $providerUser->tagline; |
||
61 | $attributes['profile_picture'] = $providerUser->avatar_original; |
||
62 | break; |
||
63 | } |
||
64 | |||
65 | if (! ($localUser = $this->getLocalUser($provider, $providerUser->id))) { |
||
66 | $localUser = $this->createLocalUser($provider, $attributes); |
||
67 | } |
||
68 | |||
69 | $loginResult = auth()->guard($this->getGuard())->attempt([ |
||
70 | 'is_active' => $localUser->is_active, |
||
71 | 'email' => $localUser->email, |
||
72 | 'social' => true, |
||
73 | ], true); |
||
74 | |||
75 | return $this->getLoginResponse(request(), $loginResult); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Get local user for the given provider. |
||
125 |
This check looks for classes that have been defined more than once.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.