Skip to content

Commit 06b9a7c

Browse files
amandiobmAmandio Magalhaes
and
Amandio Magalhaes
authored
Laravel Passport support (jamesmills#29)
Co-authored-by: Amandio Magalhaes <[email protected]>
1 parent 91c1b1d commit 06b9a7c

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

src/LaravelTimezoneServiceProvider.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function boot()
3636
AliasLoader::getInstance()->alias('Timezone', \JamesMills\LaravelTimezone\Facades\Timezone::class);
3737

3838
// Register an event listener
39-
Event::listen(\Illuminate\Auth\Events\Login::class, UpdateUsersTimezone::class);
39+
$this->registerEventListener();
4040

4141
// Allow config publish
4242
$this->publishes([
@@ -76,4 +76,17 @@ public function register()
7676
'timezone'
7777
);
7878
}
79+
80+
/**
81+
*
82+
*/
83+
private function registerEventListener(): void
84+
{
85+
$events = [
86+
\Illuminate\Auth\Events\Login::class,
87+
\Laravel\Passport\Events\AccessTokenCreated::class,
88+
];
89+
90+
Event::listen($events, UpdateUsersTimezone::class);
91+
}
7992
}

src/Listeners/Auth/UpdateUsersTimezone.php

+38-7
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,53 @@
33
namespace JamesMills\LaravelTimezone\Listeners\Auth;
44

55
use Illuminate\Auth\Events\Login;
6+
use Illuminate\Support\Facades\Auth;
7+
use Laravel\Passport\Events\AccessTokenCreated;
8+
use Torann\GeoIP\Location;
69

710
class 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

src/Timezone.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function convertFromLocal($date) : Carbon
4848
* @param Carbon $date
4949
* @return string
5050
*/
51-
public function formatTimezone(Carbon $date) : string
51+
private function formatTimezone(Carbon $date) : string
5252
{
5353
$timezone = $date->format('e');
5454
$parts = explode('/', $timezone);

0 commit comments

Comments
 (0)