Skip to content

Instantly share code, notes, and snippets.

@mbmaciel
Last active March 2, 2023 18:53
Show Gist options
  • Save mbmaciel/388931262ca720250250e4c5a8e11d05 to your computer and use it in GitHub Desktop.
Save mbmaciel/388931262ca720250250e4c5a8e11d05 to your computer and use it in GitHub Desktop.
Authenticate wp . With plan id 1 only
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding");
$referer = parse_url($_SERVER['HTTP_REFERER']);
$allowedDomain = 'playbotpro.com';
/* if ($referer['host'] != $allowedDomain){
die ('Domain not allowed!');
} */
require_once('wp-load.php');
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
$response = array(
'data' => array(),
'msg' => 'Invalid email or password',
'status' => false
);
/* Sanitize all received posts */
foreach($_POST as $k => $value){
$_POST[$k] = sanitize_text_field($value);
}
/**
* Login Method
*
*/
if( isset( $_POST['type'] ) && $_POST['type'] == 'login' ){
/* Get user data */
$user = get_user_by( 'email', $_POST['email'] );
$email = $_POST['email'];
if ( $user ){
$password_check = wp_check_password( $_POST['password'], $user->user_pass, $user->ID );
if ( $password_check ){
$status = get_user_meta($user->ID, 'arm_user_plan_ids', true);
if ($status != ["1"])
{
die("{ 'nao permitido': null }");
}
/* Generate a unique auth token */
$token = generateRandomString( 30 );
/* Store / Update auth token in the database */
if( update_user_meta( $user->ID, 'auth_token', $token ) ){
$image = get_user_meta ( $user->ID, 'file_jyga', true);
$image = "<img src='http:".$image."' height='95' width='95' />";
/* Return generated token and user ID*/
$profile_avatar = get_avatar($user->ID,95);
$response['status'] = true;
$response['data'] = array(
'auth_token' => $token,
'user_id' => $user->ID,
'user_login' => $user->user_login,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'nickname' => $user->nickname,
'email' => $email,
'avatar' => $image,
'status' => get_user_meta($user->ID, 'arm_user_plan_ids', true),
'password' => $_POST['password']
);
$response['msg'] = 'Successfully Authenticated';
print_r(json_encode($response['data']));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment