| 1 |
<?php |
| 2 |
namespace Depicter\Services; |
| 3 |
|
| 4 |
use Averta\WordPress\Utility\JSON; |
| 5 |
use Depicter\GuzzleHttp\Exception\GuzzleException; |
| 6 |
|
| 7 |
class UserAPIService { |
| 8 |
|
| 9 |
/** |
| 10 |
* User Login |
| 11 |
* |
| 12 |
* @var string $email |
| 13 |
* @var string $password |
| 14 |
* |
| 15 |
* @throws \Depicter\GuzzleHttp\Exception\GuzzleException |
| 16 |
* @throws \Exception |
| 17 |
* |
| 18 |
* @return bool |
| 19 |
*/ |
| 20 |
public static function login( $email, $password ) { |
| 21 |
$response = \Depicter::remote()->post( 'v1/remote/user/login', [ |
| 22 |
'form_params' => [ |
| 23 |
'email' => $email, |
| 24 |
'password' => $password |
| 25 |
] |
| 26 |
]); |
| 27 |
|
| 28 |
$response = JSON::decode( $response->getBody(), true ); |
| 29 |
if ( !empty( $response['errors'] ) ) { |
| 30 |
throw new \Exception( $response['errors'][0] ); |
| 31 |
} |
| 32 |
|
| 33 |
\Depicter::cache('base')->set( 'access_token', $response['accessToken'], DAY_IN_SECONDS ); |
| 34 |
\Depicter::cache('base')->set( 'id_token', $response['idToken'], MONTH_IN_SECONDS ); |
| 35 |
|
| 36 |
return $response; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* User Logout |
| 41 |
* |
| 42 |
* @return bool |
| 43 |
*/ |
| 44 |
public static function logout() { |
| 45 |
return \Depicter::cache('base')->delete( 'id_token' ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* User Register |
| 50 |
* |
| 51 |
* @param $email |
| 52 |
* @param $password |
| 53 |
* @param $fields |
| 54 |
* |
| 55 |
* @return mixed |
| 56 |
* |
| 57 |
* @throws GuzzleException |
| 58 |
* @throws \Exception |
| 59 |
*/ |
| 60 |
public static function register( $email, $password, $fields = [] ) { |
| 61 |
|
| 62 |
$options = array_merge( [ 'email' => $email, 'password' => $password ], $fields ); |
| 63 |
$response = \Depicter::remote()->post( 'v1/remote/user/register', [ 'form_params' => $options ] ); |
| 64 |
|
| 65 |
$response = JSON::decode( $response->getBody(), true ); |
| 66 |
|
| 67 |
if ( !empty( $response['errors'] ) ) { |
| 68 |
throw new \Exception( $response['errors'][0] ); |
| 69 |
} |
| 70 |
|
| 71 |
\Depicter::cache('base')->set( 'access_token', $response['accessToken'], DAY_IN_SECONDS ); |
| 72 |
\Depicter::cache('base')->set( 'id_token', $response['idToken'], MONTH_IN_SECONDS ); |
| 73 |
|
| 74 |
return $response; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Renew access and refresh tokens |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public static function renewTokens() { |
| 83 |
\Depicter::client()->getAccessToken(); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get google client id |
| 88 |
* |
| 89 |
* @return mixed |
| 90 |
* @throws GuzzleException |
| 91 |
* @throws \Exception |
| 92 |
*/ |
| 93 |
public static function googleClientID() { |
| 94 |
|
| 95 |
if ( false !== $response = \Depicter::cache('base')->get( 'googleClientId') ) { |
| 96 |
return $response; |
| 97 |
} |
| 98 |
|
| 99 |
$response = \Depicter::remote()->get( 'v1/remote/auth/google/id' ); |
| 100 |
$response = JSON::decode( $response->getBody(), true ); |
| 101 |
|
| 102 |
if ( !empty( $response['errors'] ) ) { |
| 103 |
throw new \Exception( $response['errors'][0] ); |
| 104 |
} |
| 105 |
|
| 106 |
if ( empty( $response[0] ) ) { |
| 107 |
throw new \Exception( "Cannot get a valid auth id" ); |
| 108 |
} |
| 109 |
|
| 110 |
\Depicter::cache('base')->set( 'googleClientId', $response[0], 2 * DAY_IN_SECONDS ); |
| 111 |
return $response[0]; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Login by google |
| 116 |
* |
| 117 |
* @param $accessToken |
| 118 |
* |
| 119 |
* @return mixed |
| 120 |
* @throws GuzzleException |
| 121 |
* @throws \Exception |
| 122 |
*/ |
| 123 |
public static function googleLogin( $accessToken ) { |
| 124 |
|
| 125 |
$response = \Depicter::remote()->post( 'v1/remote/user/login/google', [ |
| 126 |
'form_params' => [ |
| 127 |
'accessToken' => $accessToken |
| 128 |
] |
| 129 |
] ); |
| 130 |
|
| 131 |
$response = JSON::decode( $response->getBody(), true ); |
| 132 |
|
| 133 |
if ( !empty( $response['errors'] ) ) { |
| 134 |
throw new \Exception( $response['errors'][0] ); |
| 135 |
} |
| 136 |
|
| 137 |
\Depicter::cache('base')->set( 'access_token', $response['accessToken'], DAY_IN_SECONDS ); |
| 138 |
//\Depicter::cache('base')->set( 'refresh_token', $response['refreshToken'], DAY_IN_SECONDS ); |
| 139 |
\Depicter::cache('base')->set( 'id_token', $response['idToken'], MONTH_IN_SECONDS ); |
| 140 |
|
| 141 |
return $response; |
| 142 |
} |
| 143 |
} |
| 144 |
|