1 | <?php |
||||||
2 | |||||||
3 | namespace Mckenziearts\LaravelOAuth\Traits; |
||||||
4 | |||||||
5 | use Carbon\Carbon; |
||||||
6 | use Illuminate\Support\Facades\DB; |
||||||
7 | use Illuminate\Support\Facades\Auth; |
||||||
8 | use Laravel\Socialite\Facades\Socialite; |
||||||
9 | |||||||
10 | trait OAuthSocialite |
||||||
11 | { |
||||||
12 | /** |
||||||
13 | * Redirect the user to the Provider authentication page. |
||||||
14 | * |
||||||
15 | * @param $provider |
||||||
16 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||||||
17 | */ |
||||||
18 | public function redirectToProvider($provider) |
||||||
19 | { |
||||||
20 | $provider = strtolower($provider); |
||||||
21 | |||||||
22 | if ($provider === 'facebook') { |
||||||
23 | return Socialite::driver('facebook')->with(['auth_type' => 'rerequest'])->redirect(); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
24 | } |
||||||
25 | |||||||
26 | return Socialite::driver($provider)->redirect(); |
||||||
27 | } |
||||||
28 | |||||||
29 | /** |
||||||
30 | * Obtain the user information from provider. |
||||||
31 | * |
||||||
32 | * @param $provider |
||||||
33 | * @return \Illuminate\Http\Response |
||||||
34 | */ |
||||||
35 | public function handleProviderCallback($provider) |
||||||
36 | { |
||||||
37 | $providerUser = Socialite::driver($provider)->user(); |
||||||
38 | |||||||
39 | // Check if user email is null |
||||||
40 | // Get user from the database with the good provider_id or email |
||||||
41 | if (is_null($providerUser->getEmail())) { |
||||||
0 ignored issues
–
show
|
|||||||
42 | $this->redirectToProvider($provider); |
||||||
43 | } |
||||||
44 | |||||||
45 | $user = DB::table(config('laravel-oauth.users.table')) |
||||||
0 ignored issues
–
show
The function
config was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
46 | ->where($provider.'_id', '=', $providerUser->getId()) |
||||||
47 | ->orWhere('email', '=', $providerUser->getEmail()) |
||||||
48 | ->first(); |
||||||
49 | |||||||
50 | if (is_null($user)) { |
||||||
51 | // Save user to the database |
||||||
52 | $userId = $this->registerUser($provider, $providerUser); |
||||||
53 | Auth::loginUsingId($userId); |
||||||
54 | |||||||
55 | return redirect()->intended($this->redirectPath()); |
||||||
0 ignored issues
–
show
It seems like
redirectPath() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() The function
redirect was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
56 | } |
||||||
57 | |||||||
58 | // Login user |
||||||
59 | Auth::loginUsingId($user->id); |
||||||
60 | |||||||
61 | return redirect()->intended($this->redirectPath()); |
||||||
62 | } |
||||||
63 | |||||||
64 | /** |
||||||
65 | * Resgiter user to the database and return ID. |
||||||
66 | * |
||||||
67 | * @param $provider |
||||||
68 | * @param $user |
||||||
69 | * @return int |
||||||
70 | */ |
||||||
71 | public function registerUser($provider, $user) |
||||||
72 | { |
||||||
73 | $userId = DB::table(config('laravel-oauth.users.table'))->insertGetId([ |
||||||
0 ignored issues
–
show
The function
config was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
74 | 'name' => $user->getName(), |
||||||
75 | 'email' => $user->getEmail(), |
||||||
76 | 'password' => bcrypt('password'), |
||||||
0 ignored issues
–
show
The function
bcrypt was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
77 | $provider.'_id' => $user->getId(), |
||||||
78 | 'email_verified_at' => Carbon::now(), |
||||||
79 | 'created_at' => Carbon::now(), |
||||||
80 | 'updated_at' => Carbon::now(), |
||||||
81 | ]); |
||||||
82 | |||||||
83 | return $userId; |
||||||
84 | } |
||||||
85 | } |
||||||
86 |