nicumicle/simple-jwt-login-client-php

Simple JWT Login Client composer package

1.0.0 2021-12-04 12:09 UTC

This package is auto-updated.

Last update: 2022-12-04 16:16:23 UTC


README

68747470733a2f2f70732e772e6f72672f73696d706c652d6a77742d6c6f67696e2f6173736574732f62616e6e65722d373732783235302e706e673f7265763d32313036303937

badge.svg 68747470733a2f2f636f6465636f762e696f2f67682f6e6963756d69636c652f73696d706c652d6a77742d6c6f67696e2d636c69656e742d7068702f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d4236326b545866503630

Latest Stable Version Total Downloads License PHP Version Require

The simple-jwt-login PHP client

This client will help you integrate your PHP Application with a WordPress website that is using the simple-jwt-login WordPress plugin.

Requirements

  • PHP : >=5.6
  • curl extension

Installation

    composer require nicumicle/simple-jwt-login-client-php

Simple Example

    $simpleJwtLogin = new SimpleJwtLoginClient'https://mydomain.com');
    
    $result = $simpleJwtLogin->registerUser('email@test.com', 'My-password');
    
    //var_dump($result); 

The output of result will be the actual API call result.

How to use it

Login User

In order to autologin, you will need to redirect to the WordPress website, with the generated URL:

    $simpleJwtLogin = new SimpleJwtLoginClient'https://mydomain.com', '/simple-jwt-login/v1');
    header('Location: ' . $simpleJwtLogin->getAutologinUrl('My JWT', 'AUTH CODE', 'https://test.com'));

The Auth Code parameter is optional. You can set it to null If you don't want to use it.

Register User

This will register a new user.

    $simpleJwtLogin = new SimpleJwtLoginClient'https://mydomain.com', '/simple-jwt-login/v1');
    $result = $simpleJwtLogin->registerUser('email@simplejwtlogin.com', 'password', 'AUTH CODE');

The Auth Code parameter is optional. You can set it to null If you don't want to use it.

The $result value is:

Array
(
    [success] => true
    [id] => 1
    [message] => User was successfully created.
    [user] => Array
        (
            [ID] => 1
            [user_login] => test@simplejwtlogin.com
            [user_nicename] => test@simplejwtlogin.com
            [user_email] => test@simplejwtlogin.com
            [user_url] => 
            [user_registered] => 2021-28-01 15:30:37
            [user_activation_key] => 
            [user_status] => 0
            [display_name] => test@simplejwtlogin.com
            [user_level] => 10
        )

    [jwt] => eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
)

Delete User

This will delete a user.

    $simpleJwtLogin = new SimpleJwtLoginClient'https://mydomain.com', '/simple-jwt-login/v1');
    $result = $simpleJwtLogin->deleteUser('Your JWT here', 'AUTH CODE');

The Auth Code parameter is optional. You can set it to null If you don't want to use it.

The $result value is:

Array
(
    [message] => User was successfully deleted.
    [id] => 1
)

Reset Password

This will send the reset password email.

    $simpleJwtLogin = new SimpleJwtLoginClient'https://mydomain.com', '/simple-jwt-login/v1');
    $result = $simpleJwtLogin->resetPassword('email@simplejwtlogin.com', 'AUTH CODE');

The Auth Code parameter is optional. You can set it to null If you don't want to use it.

The $result value is:

Array
(
    [success] => true
    [message] => Reset password email has been sent.
)

Change password

This will send the reset password email. The Auth Code parameter is optional. You can set it to null If you don't want to use it.

The following code part, will change the user password, using the reset password code:

    $simpleJwtLogin = new SimpleJwtLoginClient'https://mydomain.com', '/simple-jwt-login/v1');
    $result = $simpleJwtLogin->changePassword('email@simplejwtlogin.com', 'new password', 'code', null, 'AUTH CODE');

The following code part, will change the user password, using a JWT:

    $simpleJwtLogin = new SimpleJwtLoginClient'https://mydomain.com', '/simple-jwt-login/v1');
    $result = $simpleJwtLogin->changePassword('email@simplejwtlogin.com', 'new password', null, 'Your JWT here', 'AUTH CODE');

The $result value is:

Array
(
    [success] => true
    [message] => User Password has been changed.
)

Authenticate User

The Auth Code parameter is optional. You can set it to null If you don't want to use it.

Authenticate

This will generate a JWT based on user credentials.

    $simpleJwtLogin = new SimpleJwtLoginClient'https://mydomain.com', '/simple-jwt-login/v1');
    $result = $simpleJwtLogin->authenticate('email@simplejwtlogin.com', 'your password', 'AUTH CODE');

The $result value is:

Array
(
    [success] => true
    [data] => Array
        (
            [jwt] => eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
        )

)

Refresh token

+The following code will refresh an expired token:

    $simpleJwtLogin = new SimpleJwtLoginClient'https://mydomain.com', '/simple-jwt-login/v1');
    $result = $simpleJwtLogin->refreshToken('your JWT here', 'AUTH CODE');

The $result value is:

Array
(
    [success] => true
    [data] => Array
        (
            [jwt] => eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
        )

)

Validate token

The following code will check if your JWT is valid or not:

   $simpleJwtLogin = new SimpleJwtLoginClient'https://mydomain.com', '/simple-jwt-login/v1');
    $result = $simpleJwtLogin->validateToken('your JWT here', 'AUTH CODE');

The $result value is:

Array
(
    [success] => true
    [data] => Array
        (
            [user] => Array
                (
                    [ID] => 1
                    [user_login] => test@simplejwtlogin.com
                    [user_nicename] => test@simplejwtlogin.com
                    [user_email] => test@simplejwtlogin.com
                    [user_url] => https://simplejwtlogin.com
                    [user_registered] => 2021-28-01 15:30:37
                    [user_activation_key] => 
                    [user_status] => 0
                    [display_name] => test@simplejwtlogin.com
                )

            [jwt] => Array
                (
                    [0] => Array
                        (
                            [token] => eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
                            [header] => Array
                                (
                                    [typ] => JWT
                                    [alg] => HS256
                                )

                            [payload] => Array
                                (
                                    [iat] => 1638459037
                                    [email] => test@simplejwtlogin.com
                                    [id] => 1
                                    [site] => http://localhost
                                    [username] => test@simplejwtlogin.com
                                )

                        )

                )

        )

)

Revoke token

The following code will invalidate a JWT:

   $simpleJwtLogin = new SimpleJwtLoginClient'https://mydomain.com', '/simple-jwt-login/v1'); 
   $result = $simpleJwtLogin->revokeToken('your JWT here', 'AUTH CODE');

The $result value is:

Array
(
    [success] => true
    [message] => Token was revoked.
    [data] => Array
        (
            [jwt] => Array
                (
                    [0] => eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
                )

        )

)