Passed
Push — develop ( fdb96a...bba4e3 )
by Niclas Leon
04:22
created

AuthController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 20
c 2
b 0
f 0
dl 0
loc 65
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findOrCreateUser() 0 20 3
A handleProviderCallback() 0 8 1
A signOut() 0 5 1
A redirectToProvider() 0 3 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Socialite;
6
use Auth;
7
use App\User;
8
use Exception;
9
10
class AuthController extends Controller
11
{
12
    /**
13
     * Redirect the user to the GitHub authentication page.
14
     *
15
     * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
16
     */
17
    public function redirectToProvider()
18
    {
19
        return Socialite::driver('github')->scopes([])->redirect();
0 ignored issues
show
Bug introduced by
The method scopes() does not exist on Laravel\Socialite\Contracts\Provider. It seems like you code against a sub-type of Laravel\Socialite\Contracts\Provider such as Laravel\Socialite\Two\AbstractProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

19
        return Socialite::driver('github')->/** @scrutinizer ignore-call */ scopes([])->redirect();
Loading history...
20
    }
21
22
    /**
23
     * Obtain the user information from GitHub.
24
     *
25
     * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
26
     */
27
    public function handleProviderCallback()
28
    {
29
        $user = Socialite::driver('github')->user();
30
        $authUser = $this->findOrCreateUser($user);
31
32
        Auth::login($authUser, true);
33
34
        return redirect('/');
35
    }
36
37
    /**
38
     * Log the current user out.
39
     *
40
     * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
41
     */
42
    public function signOut()
43
    {
44
        Auth::logout();
45
46
        return redirect('/');
47
    }
48
49
    /**
50
     * Return user if exists; create and return if doesn't
51
     *
52
     * @param $githubUser
53
     * @return User
54
     */
55
    private function findOrCreateUser($user)
56
    {
57
        $authUser = [
58
            'name' => $user->name ? $user->name : $user->nickname,
59
            'github_username' => $user->nickname,
60
            'github_id' => $user->id,
61
            'github_token' => $user->token,
62
            'github_avatar' => $user->avatar,
63
        ];
64
65
        $databaseUser = User::where('github_id', $user->id)->first();
66
67
        if ($databaseUser) {
68
            $databaseUser->fill($authUser);
69
            $databaseUser->save();
70
71
            return $databaseUser;
72
        }
73
74
        return User::create($authUser);
75
    }
76
}
77