3
3
namespace JamesMills \LaravelTimezone \Listeners \Auth ;
4
4
5
5
use Illuminate \Auth \Events \Login ;
6
+ use Illuminate \Support \Facades \Auth ;
7
+ use Laravel \Passport \Events \AccessTokenCreated ;
8
+ use Torann \GeoIP \Location ;
6
9
7
10
class UpdateUsersTimezone
8
11
{
9
12
10
13
/**
11
14
* Handle the event.
12
15
*
13
- * @param Login $event
14
16
* @return void
15
17
*/
16
- public function handle (Login $ event )
18
+ public function handle ($ event )
17
19
{
20
+ $ user = null ;
21
+
22
+ /**
23
+ * If the event is AccessTokenCreated,
24
+ * we logged the user and return,
25
+ * stopping the execution.
26
+ *
27
+ * The Auth::loginUsingId dispatches a Login event,
28
+ * making this listener be called again.
29
+ */
30
+ if ($ event instanceof AccessTokenCreated) {
31
+ Auth::loginUsingId ($ event ->userId );
32
+
33
+ return ;
34
+ }
35
+
36
+ /**
37
+ * If the event is Login, we get the user from the web guard.
38
+ */
39
+ if ($ event instanceof Login) {
40
+ $ user = Auth::user ();
41
+ }
42
+
43
+ /**
44
+ * If no user is found, we just return. Nothing to do here.
45
+ */
46
+ if (is_null ($ user )) {
47
+ return ;
48
+ }
49
+
18
50
$ ip = $ this ->getFromLookup ();
19
51
$ geoip_info = geoip ()->getLocation ($ ip );
20
52
21
- $ user = auth ()->user ();
22
53
if ($ user ->timezone != $ geoip_info ['timezone ' ]) {
23
54
if (config ('timezone.overwrite ' ) == true || $ user ->timezone == null ) {
24
55
$ user ->timezone = $ geoip_info ['timezone ' ];
@@ -30,9 +61,9 @@ public function handle(Login $event)
30
61
}
31
62
32
63
/**
33
- * @param \Torann\GeoIP\ Location $geoip_info
64
+ * @param Location $geoip_info
34
65
*/
35
- public function notify (\ Torann \ GeoIP \ Location $ geoip_info )
66
+ private function notify (Location $ geoip_info )
36
67
{
37
68
if (config ('timezone.flash ' ) == 'off ' ) {
38
69
return ;
@@ -74,7 +105,7 @@ public function notify(\Torann\GeoIP\Location $geoip_info)
74
105
/**
75
106
* @return mixed
76
107
*/
77
- public function getFromLookup ()
108
+ private function getFromLookup ()
78
109
{
79
110
$ result = null ;
80
111
@@ -98,7 +129,7 @@ public function getFromLookup()
98
129
* @param $keys
99
130
* @return string|null
100
131
*/
101
- public function lookup ($ type , $ keys )
132
+ private function lookup ($ type , $ keys )
102
133
{
103
134
$ value = null ;
104
135
0 commit comments