|
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 |
|
|
|
|
|
|
29
|
|
|
*/ |
|
30
|
|
|
public function handleProviderCallback($provider) |
|
31
|
|
|
{ |
|
32
|
|
|
$user = null; |
|
33
|
|
|
$err = null; |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
if($provider == 'google') { |
|
36
|
|
|
$user = Socialite::driver($provider)->stateless()->user(); |
|
|
|
|
|
|
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([ |
|
|
|
|
|
|
46
|
|
|
'email' => $authUser->email, |
|
47
|
|
|
]); |
|
48
|
|
|
|
|
49
|
|
|
if($attempt) { |
|
50
|
|
|
$msg = "Selamat Datang ".$authUser->name." !"; |
|
51
|
|
|
return redirect()->intended(route('home'))->with('info', $msg); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
// $this->sendFailedLoginResponse($req); |
|
54
|
|
|
return redirect(route('login')); |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
|
|
throw ValidationException::withMessages([ |
|
87
|
|
|
'email' => [trans('auth.failed')], |
|
88
|
|
|
]); |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|