Completed
Push — master ( 03b333...573f2e )
by Abdelrahman
02:45
created

handleGithubCallback()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 19
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 31
rs 8.8571
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Fort\Http\Controllers\Frontend;
17
18
use Exception;
19
use Illuminate\Http\Request;
20
use Illuminate\Support\Facades\Auth;
21
use Laravel\Socialite\Facades\Socialite;
22
23
class SocialAuthenticationController extends AuthenticationController
24
{
25
    /**
26
     * Redirect to Github for authentication.
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function redirectToGithub()
31
    {
32
        return Socialite::driver('github')->redirect();
33
    }
34
35
    /**
36
     * Handle Github authentication callback.
37
     *
38
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
39
     */
40
    public function handleGithubCallback(Request $request)
41
    {
42
        try {
43
            $githubUser = Socialite::driver('github')->user();
44
        } catch (Exception $e) {
45
            return intend([
46
                'intended' => route('rinvex.fort.auth.social.github'),
47
            ]);
48
        }
49
50
        $user = app('rinvex.fort.user')->whereHas('socialites', function ($query) use ($githubUser) {
51
            $query->where('provider', 'github')->where('provider_uid', $githubUser->id);
52
        })->first();
53
54
        if (! $user) {
55
            $user = Auth::guard($this->getGuard())->registerSocialite([
56
                'email'    => $githubUser->email,
57
                'username' => $githubUser->username,
58
            ]);
59
60
            $user->socialites()->create([
61
                'user_id'      => 'github',
62
                'provider'     => 'github',
63
                'provider_uid' => $githubUser->id,
64
            ]);
65
        }
66
67
        $result = Auth::guard($this->getGuard())->login($user, true);
68
69
        return $this->getLoginResponse($request, $result);
70
    }
71
}
72