Skip to content

Instantly share code, notes, and snippets.

@mohamedsalehamin
Created July 13, 2018 18:30
Show Gist options
  • Save mohamedsalehamin/d79fd28150095b5a828db81c76c42900 to your computer and use it in GitHub Desktop.
Save mohamedsalehamin/d79fd28150095b5a828db81c76c42900 to your computer and use it in GitHub Desktop.
wordpress get token by email JWT
<?php
use \Firebase\JWT\JWT;
function social_token($request)
{
$user_name = $request['email'];
if( email_exists( $user_name ) == true )
{
$secret_key = defined('JWT_AUTH_SECRET_KEY') ? JWT_AUTH_SECRET_KEY : false;
/** First thing, check the secret key if not exist return a error*/
if (!$secret_key) {
return new WP_Error(
'jwt_auth_bad_config',
__('JWT is not configurated properly, please contact the admin', 'wp-api-jwt-auth'),
array(
'status' => 403,
)
);
}
/** Try to authenticate the user with the passed credentials*/
$user = get_user_by( 'email', $user_name );
/** If the authentication fails return a error*/
if (is_wp_error($user)) {
$error_code = $user->get_error_code();
return new WP_Error(
'[jwt_auth] '.$error_code,
$user->get_error_message($error_code),
array(
'status' => 403,
)
);
}
/** Valid credentials, the user exists create the according Token */
$issuedAt = time();
$notBefore = apply_filters('jwt_auth_not_before', $issuedAt, $issuedAt);
$expire = apply_filters('jwt_auth_expire', $issuedAt + (DAY_IN_SECONDS * 7), $issuedAt);
$token = array(
'iss' => get_bloginfo('url'),
'iat' => $issuedAt,
'nbf' => $notBefore,
'exp' => $expire,
'data' => array(
'user' => array(
'id' => $user->data->ID,
),
),
);
/** Let the user modify the token data before the sign. */
$token = JWT::encode(apply_filters('jwt_auth_token_before_sign', $token, $user), $secret_key);
/** The token is signed, now create the object with no sensible user data to the client*/
$data = array(
'token' => $token,
'user_email' => $user->data->user_email,
'user_nicename' => $user->data->user_nicename,
'user_display_name' => $user->data->display_name,
);
/** Let the user modify the data before send it back */
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment