File tree 8 files changed +43
-26
lines changed
8 files changed +43
-26
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ final class TokenKeyDal
8
8
{
9
9
public const TABLE_NAME = 'secretkeys ' ;
10
10
11
- public static function saveSecretKey (string $ jwtKey )
11
+ public static function saveSecretKey (string $ jwtKey ): void
12
12
{
13
13
$ tokenBean = R::dispense (self ::TABLE_NAME );
14
14
$ tokenBean ->secretKey = $ jwtKey ;
Original file line number Diff line number Diff line change @@ -89,6 +89,9 @@ public static function getByEmail(string $email): UserEntity
89
89
return (new UserEntity ())->unserialize ($ userBean ?->export());
90
90
}
91
91
92
+ /**
93
+ * @throws \RedBeanPHP\RedException\SQL
94
+ */
92
95
public static function setToken (string $ jwtToken , string $ userUuid ): void
93
96
{
94
97
$ bindings = ['userUuid ' => $ userUuid ];
Original file line number Diff line number Diff line change 2
2
namespace PH7 \ApiSimpleMenu \Route ;
3
3
4
4
use PH7 \ApiSimpleMenu \Service \FoodItem ;
5
- use PH7 \ApiSimpleMenu \Validation \Exception \InvalidValidationException ;
6
-
7
- use PH7 \JustHttp \StatusCode ;
8
- use PH7 \PhpHttpResponseHeader \Http ;
9
5
10
6
enum FoodItemAction: string
11
7
{
@@ -21,22 +17,10 @@ public function getResponse(): string
21
17
$ itemId = $ _REQUEST ['id ' ] ?? '' ; // using the null coalescing operator
22
18
23
19
$ item = new FoodItem ();
24
- try {
25
- $ response = match ($ this ) {
26
- self ::RETRIEVE_ALL => $ item ->retrieveAll (),
27
- self ::RETRIEVE => $ item ->retrieve ($ itemId ),
28
- };
29
- } catch (InvalidValidationException $ e ) {
30
- // Send 400 http status code
31
- Http::setHeadersByCode (StatusCode::BAD_REQUEST );
32
-
33
- $ response = [
34
- 'errors ' => [
35
- 'message ' => $ e ->getMessage (),
36
- 'code ' => $ e ->getCode ()
37
- ]
38
- ];
39
- }
20
+ $ response = match ($ this ) {
21
+ self ::RETRIEVE_ALL => $ item ->retrieveAll (),
22
+ self ::RETRIEVE => $ item ->retrieve ($ itemId ),
23
+ };
40
24
41
25
return json_encode ($ response );
42
26
}
Original file line number Diff line number Diff line change 4
4
use PH7 \JustHttp \StatusCode ;
5
5
use PH7 \PhpHttpResponseHeader \Http ;
6
6
7
+ // PHP 7.4 anonymous arrow function
7
8
$ getResponse = fn (): string => json_encode (['error ' => 'Request not found ' ]);
8
9
9
10
// Send HTTP 404 Not Found
Original file line number Diff line number Diff line change 3
3
4
4
use PH7 \ApiSimpleMenu \Route \Exception \NotFoundException ;
5
5
use PH7 \ApiSimpleMenu \Service \Exception \CredentialsInvalidException ;
6
+ use PH7 \ApiSimpleMenu \Validation \Exception \InvalidValidationException ;
7
+ use PH7 \JustHttp \StatusCode ;
8
+ use PH7 \PhpHttpResponseHeader \Http as HttpResponse ;
6
9
7
10
$ resource = $ _REQUEST ['resource ' ] ?? null ;
8
11
14
17
};
15
18
} catch (CredentialsInvalidException $ e ) {
16
19
response ([
17
- 'message ' => $ e ->getMessage ()
20
+ 'errors ' => [
21
+ 'message ' => $ e ->getMessage ()
22
+ ]
23
+ ]);
24
+ } catch (InvalidValidationException $ e ) {
25
+ // Send 400 http status code
26
+ HttpResponse::setHeadersByCode (StatusCode::BAD_REQUEST );
27
+
28
+ response ([
29
+ 'errors ' => [
30
+ 'message ' => $ e ->getMessage (),
31
+ 'code ' => $ e ->getCode ()
32
+ ]
18
33
]);
19
34
} catch (NotFoundException $ e ) {
20
35
// FYI, not-found.Route already sends a 404 Not Found HTTP code
Original file line number Diff line number Diff line change 2
2
namespace PH7 \ApiSimpleMenu \Route ;
3
3
4
4
use PH7 \ApiSimpleMenu \Route \Exception \NotFoundException ;
5
+ use PH7 \ApiSimpleMenu \Service \Exception \CannotLoginUserException ;
5
6
use PH7 \ApiSimpleMenu \Service \Exception \EmailExistsException ;
6
7
use PH7 \ApiSimpleMenu \Service \SecretKey ;
7
8
use PH7 \ApiSimpleMenu \Service \User ;
8
- use PH7 \ApiSimpleMenu \Validation \Exception \InvalidValidationException ;
9
9
10
10
use PH7 \JustHttp \StatusCode ;
11
11
use PH7 \PhpHttpResponseHeader \Http as HttpResponse ;
@@ -57,14 +57,13 @@ public function getResponse(): string
57
57
self ::RETRIEVE => $ user ->retrieve ($ userId ),
58
58
self ::REMOVE => $ user ->remove ($ postBody ),
59
59
};
60
- } catch (InvalidValidationException $ e ) {
60
+ } catch (CannotLoginUserException $ e ) {
61
61
// Send 400 http status code
62
62
HttpResponse::setHeadersByCode (StatusCode::BAD_REQUEST );
63
63
64
64
$ response = [
65
65
'errors ' => [
66
66
'message ' => $ e ->getMessage (),
67
- 'code ' => $ e ->getCode ()
68
67
]
69
68
];
70
69
} catch (EmailExistsException $ e ) {
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace PH7 \ApiSimpleMenu \Service \Exception ;
4
+
5
+ use RuntimeException ;
6
+
7
+ class CannotLoginUserException extends RuntimeException
8
+ {
9
+ }
Original file line number Diff line number Diff line change 1
1
<?php
2
2
namespace PH7 \ApiSimpleMenu \Service ;
3
3
4
+ use Exception ;
4
5
use Firebase \JWT \JWT ;
5
6
use PH7 \ApiSimpleMenu \Dal \UserDal ;
7
+ use PH7 \ApiSimpleMenu \Service \Exception \CannotLoginUserException ;
6
8
use PH7 \ApiSimpleMenu \Service \Exception \EmailExistsException ;
7
9
use PH7 \ApiSimpleMenu \Service \Exception \CredentialsInvalidException ;
8
10
use PH7 \ApiSimpleMenu \Validation \Exception \InvalidValidationException ;
@@ -47,7 +49,11 @@ public function login(mixed $data): array
47
49
$ _ENV ['JWT_ALGO_ENCRYPTION ' ]
48
50
);
49
51
50
- UserDal::setToken ($ jwtToken , $ user ->getUserUuid ());
52
+ try {
53
+ UserDal::setToken ($ jwtToken , $ user ->getUserUuid ());
54
+ } catch (Exception $ e ) {
55
+ throw new CannotLoginUserException ('Cannot set token to user ' );
56
+ }
51
57
52
58
return [
53
59
'message ' => sprintf ('%s successfully logged in ' , $ userName ),
You can’t perform that action at this time.
0 commit comments