33namespace JamesMills \LaravelTimezone \Listeners \Auth ;
44
55use Illuminate \Auth \Events \Login ;
6+ use Illuminate \Support \Facades \Auth ;
7+ use Laravel \Passport \Events \AccessTokenCreated ;
8+ use Torann \GeoIP \Location ;
69
710class UpdateUsersTimezone
811{
912
1013 /**
1114 * Handle the event.
1215 *
13- * @param Login $event
1416 * @return void
1517 */
16- public function handle (Login $ event )
18+ public function handle ($ event )
1719 {
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+
1850 $ ip = $ this ->getFromLookup ();
1951 $ geoip_info = geoip ()->getLocation ($ ip );
2052
21- $ user = auth ()->user ();
2253 if ($ user ->timezone != $ geoip_info ['timezone ' ]) {
2354 if (config ('timezone.overwrite ' ) == true || $ user ->timezone == null ) {
2455 $ user ->timezone = $ geoip_info ['timezone ' ];
@@ -30,9 +61,9 @@ public function handle(Login $event)
3061 }
3162
3263 /**
33- * @param \Torann\GeoIP\ Location $geoip_info
64+ * @param Location $geoip_info
3465 */
35- public function notify (\ Torann \ GeoIP \ Location $ geoip_info )
66+ private function notify (Location $ geoip_info )
3667 {
3768 if (config ('timezone.flash ' ) == 'off ' ) {
3869 return ;
@@ -74,7 +105,7 @@ public function notify(\Torann\GeoIP\Location $geoip_info)
74105 /**
75106 * @return mixed
76107 */
77- public function getFromLookup ()
108+ private function getFromLookup ()
78109 {
79110 $ result = null ;
80111
@@ -98,7 +129,7 @@ public function getFromLookup()
98129 * @param $keys
99130 * @return string|null
100131 */
101- public function lookup ($ type , $ keys )
132+ private function lookup ($ type , $ keys )
102133 {
103134 $ value = null ;
104135
0 commit comments