Skip to content

Commit

Permalink
Merge branch 'develop' minor
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDevelop committed Apr 24, 2022
2 parents 9d629af + 8106cf6 commit b2e3a43
Show file tree
Hide file tree
Showing 8 changed files with 674 additions and 16 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
[![version](https://img.shields.io/badge/Version-0.0.0-brightgreen.svg)](https://github.com/SimonDevelop/strapi-api/releases/tag/0.0.0)
[![version](https://img.shields.io/badge/Version-0.1.0-brightgreen.svg)](https://github.com/SimonDevelop/strapi-api/releases/tag/0.1.0)
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.4-8892BF.svg)](https://php.net/)
[![Github Actions PHP](https://github.com/SimonDevelop/strapi-api/workflows/PHP/badge.svg)](https://github.com/SimonDevelop/strapi-api/actions)
[![codecov](https://codecov.io/gh/SimonDevelop/strapi-api/branch/master/graph/badge.svg?token=D30AKR07T2)](https://codecov.io/gh/SimonDevelop/strapi-api)
[![GitHub license](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/SimonDevelop/strapi-api/blob/master/LICENSE)

# strapi-api
Librairie php vous permettant d'utiliser l'API de [Strapi](https://strapi.io/) via diverses fonctions pour facilité les requêtes.
PHP library allowing you to use the [Strapi](https://strapi.io/) API via various functions to facilitate requests.

## TODO
- [ ] Authentification
- [x] User system (without external provider)
- [ ] Collection types
- [ ] Single types
- [ ] Upload

Check this [docs](https://github.com/SimonDevelop/strapi-api/blob/master/docs) for more.

#### Go to contribute !
- Check the [Code of Conduct](https://github.com/SimonDevelop/strapi-api/blob/master/.github/CODE_OF_CONDUCT.md)
- Check the [Contributing file](https://github.com/SimonDevelop/strapi-api/blob/master/.github/CONTRIBUTING.md)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simondevelop/strapi-api",
"description": "Librairie facilitant l'utilisation de l'API strapi",
"description": "PHP library allowing you to use the Strapi API via various functions to facilitate requests.",
"type": "library",
"keywords": [
"strapi",
Expand Down
7 changes: 7 additions & 0 deletions docs/README.md
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
48 changes: 48 additions & 0 deletions docs/auth.md
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);
```
30 changes: 30 additions & 0 deletions docs/setup.md
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');
```
243 changes: 243 additions & 0 deletions src/Auth.php
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;
}
}
Loading

0 comments on commit b2e3a43

Please sign in to comment.