-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
674 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Class list | ||
|
||
- [SimonDevelop\Strapi\Setup](https://github.com/SimonDevelop/strapi-api/blob/master/docs/setup.md) | ||
- [SimonDevelop\Strapi\Auth](https://github.com/SimonDevelop/strapi-api/blob/master/docs/auth.md) | ||
- SimonDevelop\Strapi\CollectionTypes | ||
- SimonDevelop\Strapi\SingleTypes | ||
- SimonDevelop\Strapi\Upload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# SimonDevelop\Strapi\Auth references | ||
|
||
## __constructor(SimonDevelop\Strapi\Setup $setup) | ||
```php | ||
$auth = new Auth($setup); | ||
``` | ||
|
||
## getSetup(): SimonDevelop\Strapi\Setup | ||
### Get Setup object | ||
```php | ||
$setup = $auth->getSetup(); | ||
``` | ||
|
||
## setSetup(SimonDevelop\Strapi\Setup $setup): self | ||
### Define Setup object | ||
```php | ||
$auth->setSetup($setup); | ||
``` | ||
|
||
## authentication(string $identifier, string $password): array|string | ||
### User data with JWT token (response strapi) if the request is passed, else information in string | ||
```php | ||
$response = $auth->authentication($identifier, $password); | ||
``` | ||
|
||
## register(string $username, string $email, string $password): array|string | ||
### User data (response strapi) | ||
```php | ||
$response = $auth->register($username, $email, $password); | ||
``` | ||
|
||
## forgotPassword(string $email): bool|string | ||
### Returns true if the request is passed, else information in string | ||
```php | ||
$response = $auth->forgotPassword($email); | ||
``` | ||
|
||
## resetPassword(string $code, string $newPassword, string $newPasswordConfirm): array|string | ||
### User data with JWT token (response strapi) if the request is passed, else information in string | ||
```php | ||
$response = $auth->resetPassword($code, $newPassword, $newPasswordConfirm); | ||
``` | ||
|
||
## sendEmailConfirmation(string $email): bool|string | ||
### Returns true if the request is passed, else information in string | ||
```php | ||
$response = $auth->sendEmailConfirmation($email); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# SimonDevelop\Strapi\Setup references | ||
|
||
## __constructor(string $url, string $token = null) | ||
```php | ||
$setup = new Setup('https://strapi.subdomain.fr/api'); | ||
``` | ||
|
||
## getUrl(): string | ||
### Get url strapi api defined | ||
```php | ||
$url = $setup->getUrl(); | ||
``` | ||
|
||
## setUrl(string $url): self | ||
### Define url of strapi api | ||
```php | ||
$setup->setUrl('https://strapi.subdomain.com/api'); | ||
``` | ||
|
||
## getToken(): ?string | ||
### Get jwt token strapi api defined | ||
```php | ||
$token = $setup->getToken(); | ||
``` | ||
|
||
## setToken(?string $token): self | ||
### Define jwt token of strapi api | ||
```php | ||
$setup->setToken('jwt'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
<?php | ||
|
||
/* | ||
* This file is the strapi-api package. | ||
* | ||
* (c) Simon Micheneau <contact@simon-micheneau.fr> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace SimonDevelop\Strapi; | ||
|
||
use SimonDevelop\Strapi\Setup; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\ClientException; | ||
use GuzzleHttp\Exception\ConnectException; | ||
|
||
/** | ||
* Class Auth | ||
* Authentication, register and more function. | ||
*/ | ||
class Auth | ||
{ | ||
/** | ||
* @const url for authentication | ||
*/ | ||
const AUTH_URL = '/auth/local'; | ||
|
||
/** | ||
* @const url for register | ||
*/ | ||
const REGISTER_URL = '/auth/local/register'; | ||
|
||
/** | ||
* @const url for forgot password | ||
*/ | ||
const FORGOT_PASSWORD_URL = '/auth/forgot-password'; | ||
|
||
/** | ||
* @const url for reset password | ||
*/ | ||
const RESET_PASSWORD_URL = '/auth/reset-password'; | ||
|
||
/** | ||
* @const url for send email confirmation | ||
*/ | ||
const SEND_CONFIRMATION_URL = '/auth/send-email-confirmation'; | ||
|
||
/** | ||
* @var Setup | ||
*/ | ||
private $setup; | ||
|
||
/** | ||
* @var Client | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* @param Setup $setup | ||
*/ | ||
public function __construct(Setup $setup) | ||
{ | ||
$this->setup = $setup; | ||
$this->client = new Client(); | ||
} | ||
|
||
/** | ||
* @param string $identifier | ||
* @param string $password | ||
* @return array|string | ||
*/ | ||
public function authentication(string $identifier, string $password) | ||
{ | ||
// Function tested with mock | ||
try { | ||
// @codeCoverageIgnoreStart | ||
$response = $this->client->request('POST', $this->setup->getUrl() . self::AUTH_URL, [ | ||
'json' => [ | ||
'identifier' => $identifier, | ||
'password' => $password | ||
] | ||
]); | ||
|
||
return json_decode($response->getBody()->getContents(), true); | ||
// @codeCoverageIgnoreEnd | ||
} catch (ClientException $e) { | ||
// @codeCoverageIgnoreStart | ||
$json = json_decode($e->getResponse()->getBody()->getContents(), true); | ||
|
||
return $json['error']['message']; | ||
// @codeCoverageIgnoreEnd | ||
} catch (ConnectException $e) { | ||
return $e->getMessage(); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $username | ||
* @param string $email | ||
* @param string $password | ||
* @return array|string | ||
*/ | ||
public function register(string $username, string $email, string $password) | ||
{ | ||
// Function tested with mock | ||
try { | ||
// @codeCoverageIgnoreStart | ||
$response = $this->client->request('POST', $this->setup->getUrl() . self::REGISTER_URL, [ | ||
'json' => [ | ||
'username' => $username, | ||
'email' => $email, | ||
'password' => $password | ||
] | ||
]); | ||
|
||
return json_decode($response->getBody()->getContents(), true); | ||
// @codeCoverageIgnoreEnd | ||
} catch (ClientException $e) { | ||
// @codeCoverageIgnoreStart | ||
$json = json_decode($e->getResponse()->getBody()->getContents(), true); | ||
|
||
return $json['error']['message']; | ||
// @codeCoverageIgnoreEnd | ||
} catch (ConnectException $e) { | ||
return $e->getMessage(); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $email | ||
* @return bool|string | ||
*/ | ||
public function forgotPassword(string $email) | ||
{ | ||
// Function tested with mock | ||
try { | ||
// @codeCoverageIgnoreStart | ||
$this->client->request('POST', $this->setup->getUrl() . self::FORGOT_PASSWORD_URL, [ | ||
'json' => [ | ||
'email' => $email | ||
] | ||
]); | ||
|
||
return true; | ||
// @codeCoverageIgnoreEnd | ||
} catch (ClientException $e) { | ||
// @codeCoverageIgnoreStart | ||
$json = json_decode($e->getResponse()->getBody()->getContents(), true); | ||
|
||
return $json['error']['message']; | ||
// @codeCoverageIgnoreEnd | ||
} catch (ConnectException $e) { | ||
return $e->getMessage(); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $code | ||
* @param string $newPassword | ||
* @param string $newPasswordConfirm | ||
* @return array|string | ||
*/ | ||
public function resetPassword(string $code, string $newPassword, string $newPasswordConfirm) | ||
{ | ||
// Function tested with mock | ||
if ($newPassword !== $newPasswordConfirm) { | ||
throw new \InvalidArgumentException( | ||
'Unable to "resetPassword": $newPassword and $newPasswordConfirm are not equals' | ||
); | ||
} | ||
|
||
try { | ||
// @codeCoverageIgnoreStart | ||
$response = $this->client->request('POST', $this->setup->getUrl() . self::RESET_PASSWORD_URL, [ | ||
'json' => [ | ||
'code' => $code, | ||
'password' => $newPassword, | ||
'passwordConfirmation' => $newPasswordConfirm | ||
] | ||
]); | ||
|
||
return json_decode($response->getBody()->getContents(), true); | ||
// @codeCoverageIgnoreEnd | ||
} catch (ClientException $e) { | ||
// @codeCoverageIgnoreStart | ||
$json = json_decode($e->getResponse()->getBody()->getContents(), true); | ||
|
||
return $json['error']['message']; | ||
// @codeCoverageIgnoreEnd | ||
} catch (ConnectException $e) { | ||
return $e->getMessage(); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $email | ||
* @return bool|string | ||
*/ | ||
public function sendEmailConfirmation(string $email) | ||
{ | ||
// Function tested with mock | ||
try { | ||
// @codeCoverageIgnoreStart | ||
$this->client->request('POST', $this->setup->getUrl() . self::SEND_CONFIRMATION_URL, [ | ||
'json' => [ | ||
'email' => $email | ||
] | ||
]); | ||
|
||
return true; | ||
// @codeCoverageIgnoreEnd | ||
} catch (ClientException $e) { | ||
// @codeCoverageIgnoreStart | ||
$json = json_decode($e->getResponse()->getBody()->getContents(), true); | ||
|
||
return $json['error']['message']; | ||
// @codeCoverageIgnoreEnd | ||
} catch (ConnectException $e) { | ||
return $e->getMessage(); | ||
} | ||
} | ||
|
||
/** | ||
* @return Setup Current setup | ||
*/ | ||
public function getSetup(): Setup | ||
{ | ||
return $this->setup; | ||
} | ||
|
||
/** | ||
* @param Setup $setup | ||
* @return Auth | ||
*/ | ||
public function setSetup(Setup $setup): self | ||
{ | ||
$this->setup = $setup; | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.