Skip to content

blavetstudio/simple-jwt-login-client-php

 
 

Repository files navigation

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
                )

        )

)

Packages

 
 
 

Languages

  • PHP 100.0%