Completed
Pull Request — master (#16)
by ARCANEDEV
05:25
created

SocialAuthController::findOrCreateUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
1
<?php namespace Arcanesoft\Auth\Http\Controllers\Front;
2
3
use App\Models\User;
4
use Arcanedev\LaravelAuth\Services\SocialAuthenticator;
5
use Illuminate\Support\Facades\Auth;
6
use Laravel\Socialite\Facades\Socialite;
7
8
/**
9
 * Class     SocialAuthController
10
 *
11
 * @package  Arcanesoft\Auth\Http\Controllers\Front
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class SocialAuthController extends Controller
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Properties
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /**
21
     * Where to redirect users after login / registration.
22
     *
23
     * @var string
24
     */
25
    protected $redirectTo = '/home';
26
27
    /* ------------------------------------------------------------------------------------------------
28
     |  Main Functions
29
     | ------------------------------------------------------------------------------------------------
30
     */
31
    /**
32
     * Redirect the user to the OAuth Provider.
33
     *
34
     * @param  string  $socialProvider
35
     *
36
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
37
     */
38
    public function redirectToProvider($socialProvider)
39
    {
40
        return $this->getSocialProvider($socialProvider)->redirect();
41
    }
42
43
    /**
44
     * Obtain the user information from provider.
45
     *
46
     * @param  string  $socialProvider
47
     *
48
     * @return \Illuminate\Http\RedirectResponse
49
     */
50
    public function handleCallback($socialProvider)
51
    {
52
        /** @var  \Laravel\Socialite\One\User|\Laravel\Socialite\Two\User  $user */
53
        $user     = $this->getSocialProvider($socialProvider)->user();
54
        $authUser = $this->findOrCreateUser($user, $socialProvider);
55
56
        Auth::login($authUser, true);
57
58
        return redirect()->to($this->redirectTo);
59
    }
60
61
    /* ------------------------------------------------------------------------------------------------
62
     |  Other Functions
63
     | ------------------------------------------------------------------------------------------------
64
     */
65
    /**
66
     * Find or create a user.
67
     *
68
     * @param  \Laravel\Socialite\One\User|\Laravel\Socialite\Two\User  $user
69
     * @param  string                                                   $provider
70
     *
71
     * @return \Arcanesoft\Contracts\Auth\Models\User
72
     */
73
    private function findOrCreateUser($user, $provider)
74
    {
75
        if ($authUser = User::where('social_provider_id', $user->id)->first()) {
76
            return $authUser;
77
        }
78
79
        return User::create([
80
            'name'               => $user->name,
81
            'email'              => $user->email,
82
            'social_provider'    => $provider,
83
            'social_provider_id' => $user->id,
84
        ]);
85
    }
86
87
    /**
88
     * Get the social provider.
89
     *
90
     * @param  string  $driver
91
     *
92
     * @return \Laravel\Socialite\One\AbstractProvider|\Laravel\Socialite\Two\AbstractProvider
93
     */
94
    private function getSocialProvider($driver)
95
    {
96
        if ( ! SocialAuthenticator::isSupported($driver))
97
            static::pageNotFound();
98
99
        return Socialite::driver($driver);
100
    }
101
}
102