OAuthController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 29
dl 0
loc 75
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handleProviderCallback() 0 25 3
A sendFailedLoginResponse() 0 4 1
A redirectToProvider() 0 3 1
A findOrCreateUser() 0 16 2
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\User;
6
use Illuminate\Http\Request;
7
use App\Http\Controllers\Controller;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Hash;
10
use Illuminate\Validation\ValidationException;
11
use Laravel\Socialite\Facades\Socialite;
12
13
class OAuthController extends Controller
14
{
15
16
    public function redirectToProvider($provider)
17
    {
18
        return Socialite::driver($provider)->redirect();
19
    }
20
21
22
    /**
23
     * Obtain the user information from provider.  Check if the user already exists in our
24
     * database by looking up their provider_id in the database.
25
     * If the user exists, log them in. Otherwise, create a new user then log them in. After that
26
     * redirect them to the authenticated users homepage.
27
     *
28
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Auth\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
29
     */
30
    public function handleProviderCallback($provider)
31
    {
32
        $user = null;
33
        $err = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $err is dead and can be removed.
Loading history...
34
35
        if($provider == 'google') {
36
            $user = Socialite::driver($provider)->stateless()->user();
0 ignored issues
show
Bug introduced by
The method stateless() 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

36
            $user = Socialite::driver($provider)->/** @scrutinizer ignore-call */ stateless()->user();
Loading history...
37
        } else {
38
            $user = Socialite::driver($provider)->user();
39
        }
40
41
        $authUser = $this->findOrCreateUser($user, $provider);
42
43
        $attempt = Auth::guard('web')->attempt(['email' => $authUser->email, 'password' => $authUser->provider_id]);
44
45
        $req = new Request([
0 ignored issues
show
Unused Code introduced by
The assignment to $req is dead and can be removed.
Loading history...
46
            'email' => $authUser->email,
47
        ]);
48
49
        if($attempt) {
50
            $msg = "Selamat Datang ".$authUser->name." !";
51
            return redirect()->intended(route('home'))->with('info', $msg);
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->inten...'))->with('info', $msg) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Auth\Response.
Loading history...
52
        }
53
//        $this->sendFailedLoginResponse($req);
54
        return redirect(route('login'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('login')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Auth\Response.
Loading history...
55
    }
56
57
58
    /**
59
     * If a user has registered before using social auth, return the user
60
     * else, create a new user object.
61
     * @param  $user Socialite user object
62
     * @param $provider Social auth provider
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Auth\Social was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
63
     * @return  User
64
     */
65
    private function findOrCreateUser($user, $provider)
66
    {
67
        $authUser = User::where('email', $user->getEmail())->first();
68
        if($authUser) {
69
            return $authUser;
70
        }
71
72
        $newUser = new User;
73
        $newUser->email = $user->getEmail();
74
        $newUser->name = $user->getName();
75
        $newUser->password = Hash::make($user->id);
76
        $newUser->provider = $provider;
77
        $newUser->provider_id = $user->id;
78
        $newUser->save();
79
80
        return $newUser;
81
    }
82
83
84
    private function sendFailedLoginResponse(Request $request)
0 ignored issues
show
Unused Code introduced by
The method sendFailedLoginResponse() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

84
    private function sendFailedLoginResponse(/** @scrutinizer ignore-unused */ Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85
    {
86
        throw ValidationException::withMessages([
87
            'email' => [trans('auth.failed')],
88
        ]);
89
90
    }
91
92
}
93