|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
/*
|
|
4
|
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
5
|
|
|
* To change this template file, choose Tools | Templates
|
|
6
|
|
|
* and open the template in the editor.
|
|
7
|
|
|
*/
|
|
8
|
|
|
|
|
9
|
|
|
namespace App;
|
|
10
|
|
|
|
|
11
|
|
|
use Socialite;
|
|
12
|
|
|
use Illuminate\Support\Facades\Session;
|
|
13
|
|
|
|
|
14
|
|
|
class LoginUser {
|
|
15
|
|
|
|
|
16
|
1 |
|
public function authenticate($provider) {
|
|
17
|
1 |
|
return Socialite::driver($provider)
|
|
18
|
1 |
|
->with(["access_type" => "offline", "prompt" => "consent select_account"])
|
|
19
|
1 |
|
->redirect();
|
|
20
|
|
|
}
|
|
21
|
|
|
|
|
22
|
1 |
|
public function login($provider = 'google') {
|
|
23
|
1 |
|
$provider_user = Socialite::driver($provider)->user();
|
|
24
|
|
|
|
|
25
|
|
|
//setup the oauth identity
|
|
26
|
1 |
|
$oauth_identity = OauthIdentity::firstOrNew(['provider' => $provider, 'provider_user_id' => $provider_user->id]);
|
|
27
|
|
|
|
|
28
|
|
|
//setup the email
|
|
29
|
1 |
|
$email = Email::firstOrNew(['email' => $provider_user->email]);
|
|
30
|
|
|
|
|
31
|
|
|
// if we have an oauth user.. then grab that user.. if we don't then try and find the user via email (or just make a new user)
|
|
32
|
1 |
|
if ($oauth_identity->user_id) {
|
|
33
|
|
|
$user = User::find($oauth_identity->user_id);
|
|
34
|
|
|
} else {
|
|
35
|
1 |
|
$user = User::firstOrNew(['id' => $email->user_id]);
|
|
36
|
|
|
}
|
|
37
|
|
|
|
|
38
|
|
|
// save tokens
|
|
39
|
1 |
|
$oauth_identity->access_token = $provider_user->token;
|
|
40
|
1 |
|
$oauth_identity->refresh_token = $provider_user->refreshToken;
|
|
41
|
|
|
|
|
42
|
|
|
// if we don't have a user id, then it is a new user.. fill from the provider..
|
|
43
|
1 |
|
if (!$user->id) {
|
|
44
|
1 |
|
$user->name = $provider_user->name;
|
|
45
|
1 |
|
$user->avatar = $provider_user->avatar;
|
|
46
|
1 |
|
$user->confirmed = 0;
|
|
47
|
1 |
|
$email->is_primary = 1;
|
|
48
|
|
|
}
|
|
49
|
|
|
|
|
50
|
|
|
// if the user exists already, but isn't active then activate and grab the avatar.
|
|
51
|
|
|
// (this is for the scenario where someone registers their email address, but never activates their account using
|
|
52
|
|
|
// normal registration process.. we want to activate the user and clear the activation token etc.
|
|
53
|
1 |
|
if ($user->id && !$user->confirmed) {
|
|
54
|
|
|
$user->confirmed = 1;
|
|
55
|
|
|
$user->activation_token = null;
|
|
56
|
|
|
$user->avatar = $provider_user->avatar;
|
|
57
|
|
|
}
|
|
58
|
|
|
|
|
59
|
|
|
// Verify if user is new. Prepare user to second stage of register if so.
|
|
60
|
1 |
|
if(!$user->exists) {
|
|
61
|
1 |
|
Session::flash('warning', 'New User: Register a password for your local account.');
|
|
62
|
1 |
|
$retorno = view('auth.register',['name' => $provider_user->name, 'email' => $provider_user->email]);
|
|
63
|
|
|
} else {
|
|
64
|
|
|
$retorno = redirect()->action('HomeController@showDashboard');
|
|
65
|
|
|
}
|
|
66
|
|
|
|
|
67
|
|
|
// these should only actually do anything if there is something to update.. but maybe wrap these up..
|
|
68
|
1 |
|
$user->save();
|
|
69
|
1 |
|
$user->emails()->save($email);
|
|
70
|
1 |
|
$user->oauthIdentities()->save($oauth_identity);
|
|
71
|
|
|
|
|
72
|
|
|
// log the user in with whatever you've chosen for authentication (I use JWT tokens) but you can do something like this
|
|
73
|
1 |
|
auth()->login($user);
|
|
74
|
|
|
|
|
75
|
|
|
// I like to throw in an event to handle some stuff like sending a welcome email etc...
|
|
76
|
|
|
//event(new UserRegisteredThroughOauth($user));
|
|
77
|
|
|
|
|
78
|
1 |
|
return $retorno;
|
|
79
|
|
|
}
|
|
80
|
|
|
|
|
81
|
|
|
}
|
|
82
|
|
|
|