Skip to content

Commit 65da698

Browse files
authored
Auth service refactor (#174)
* wip * Apply fixes from StyleCI (#145) * Refactoring authentication service * Apply fixes from StyleCI (#173)
1 parent eaf0697 commit 65da698

20 files changed

+565
-530
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
"require": {
2121
"php": "^7.4",
2222
"ext-json": "*",
23+
"doctrine/dbal": "^2.10",
2324
"illuminate/support": "^6.0|^7.0",
24-
"doctrine/dbal": "^2.10"
25+
"laravel/ui": "^2.0"
2526
},
2627
"require-dev": {
2728
"mockery/mockery": "^1.3",

config/config.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,24 @@
2525
| this will be used for the verification of the authenticatable model and provide the
2626
| authorizable functionality
2727
|
28-
| Supported: "passport", "airlock"
28+
| Supported: "passport", "sanctum"
2929
*/
3030

31-
'provider' => 'airlock',
31+
'provider' => 'sanctum',
32+
33+
/*
34+
|--------------------------------------------------------------------------
35+
| Auth frontend app url
36+
|--------------------------------------------------------------------------
37+
|
38+
|URL used for reset password URL generating.
39+
|
40+
|
41+
*/
42+
43+
'frontend_app_url' => env('FRONTEND_APP_URL', env('APP_URL')),
44+
45+
'password_reset_url' => env('FRONTEND_APP_URL').'/password/reset?token={token}&email={email}',
3246
],
3347

3448
/*

src/Commands/CheckPassport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function hasPassportClient(): bool
148148
} catch (\RuntimeException $e) {
149149
$this->warn($e->getMessage());
150150
$this->warn('Hint: php artisan passport:client --personal');
151-
$this->warn('See: https://laravel.com/docs/6.x/passport#creating-a-personal-access-client');
151+
$this->warn('See: https://laravel.com/docs/7.x/passport#creating-a-personal-access-client');
152152

153153
return false;
154154
}

src/Contracts/Sanctumable.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Contracts;
4+
5+
interface Sanctumable
6+
{
7+
}

src/Controllers/AuthController.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Controllers;
4+
5+
use Binaryk\LaravelRestify\Services\AuthService;
6+
use Illuminate\Http\Request;
7+
8+
class AuthController extends RestController
9+
{
10+
private AuthService $authService;
11+
12+
public function __construct(AuthService $authService)
13+
{
14+
$this->authService = $authService;
15+
}
16+
17+
public function login(Request $request)
18+
{
19+
return $this->authService->login($request);
20+
}
21+
22+
public function register(Request $request)
23+
{
24+
return $this->authService->register($request);
25+
}
26+
27+
public function verify(Request $request)
28+
{
29+
return $this->authService->verify($request);
30+
}
31+
32+
public function forgotPassword(Request $request)
33+
{
34+
return $this->authService->forgotPassword($request);
35+
}
36+
37+
public function resetPassword(Request $request)
38+
{
39+
return $this->authService->resetPassword($request);
40+
}
41+
}

src/Exceptions/AirlockUserException.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Exceptions/AuthenticatableUserException.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@
99
*/
1010
class AuthenticatableUserException extends Exception
1111
{
12+
public static function wrongInstance(): self
13+
{
14+
$message = __("Repository model should be an instance of \Illuminate\Contracts\Auth\Authenticatable");
15+
16+
return new static($message);
17+
}
1218
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Exceptions;
4+
5+
use Exception;
6+
7+
/**
8+
* @author Eduard Lupacescu <[email protected]>
9+
*/
10+
class SanctumUserException extends Exception
11+
{
12+
public static function wrongConfiguration()
13+
{
14+
return new static('Auth provider should be [sanctum] in the configuration [restify.auth.provider].');
15+
}
16+
}

src/Notifications/VerifyEmail.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Notifications;
4+
5+
use Illuminate\Auth\Notifications\VerifyEmail as VerifyEmailLaravel;
6+
use Illuminate\Notifications\Messages\MailMessage;
7+
use Illuminate\Notifications\Notification;
8+
use Illuminate\Support\Carbon;
9+
use Illuminate\Support\Facades\Config;
10+
use Illuminate\Support\Facades\Lang;
11+
use Illuminate\Support\Facades\URL;
12+
13+
class VerifyEmail extends VerifyEmailLaravel
14+
{
15+
public function toMail($notifiable)
16+
{
17+
$verificationUrl = $this->verificationUrl($notifiable);
18+
19+
if (static::$toMailCallback) {
20+
return call_user_func(static::$toMailCallback, $notifiable, $verificationUrl);
21+
}
22+
23+
return (new MailMessage)
24+
->subject(Lang::get('Verify Email Address'))
25+
->line(Lang::get('Please click the button below to verify your email address.'))
26+
->action(Lang::get('Verify Email Address'), $verificationUrl)
27+
->line(Lang::get('If you did not create an account, no further action is required.'));
28+
}
29+
30+
/**
31+
* Get the verification URL for the given notifiable.
32+
*
33+
* @param mixed $notifiable
34+
* @return string
35+
*/
36+
protected function verificationUrl($notifiable)
37+
{
38+
return URL::temporarySignedRoute(
39+
'restify.verification.verify',
40+
Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
41+
[
42+
'id' => $notifiable->getKey(),
43+
'hash' => sha1($notifiable->getEmailForVerification()),
44+
]
45+
);
46+
}
47+
48+
/**
49+
* Set a callback that should be used when building the notification mail message.
50+
*
51+
* @param \Closure $callback
52+
* @return void
53+
*/
54+
public static function toMailUsing($callback)
55+
{
56+
static::$toMailCallback = $callback;
57+
}
58+
}

0 commit comments

Comments
 (0)