Skip to content

Instantly share code, notes, and snippets.

@Pirenko
Last active July 16, 2019 10:29
Show Gist options
  • Save Pirenko/5c8c04ca79a70a4f22e71a6a38c27d92 to your computer and use it in GitHub Desktop.
Save Pirenko/5c8c04ca79a70a4f22e71a6a38c27d92 to your computer and use it in GitHub Desktop.
// Here's an example of the route we are creating
// https://www.yourwebsite.com/wp-json/sand_login/[email protected]&secret=6Uy11Uyl
// Register the rest route
add_action( 'rest_api_init', function () {
register_rest_route(
'sand_login',
'prk_route',
array(
'methods' => 'GET',
'callback' => 'pirenko_login_user',
)
);
});
// Function to handle the request
function pirenko_login_user() {
// Validate input
if (isset($_GET['secret']) && isset($_GET['username']) && $_GET['username']!="") {
$username = $_GET['username'];
$user = get_user_by('email', $username );
if ($user) {
//Check if secret key matches
$check_user = get_user_by( 'id', $user->ID );
if ($_GET['secret']==$check_user->secret_key) {
//Login info is correct - let's proceed
// Manage login cookies
wp_clear_auth_cookie();
wp_set_current_user ( $user->ID );
wp_set_auth_cookie ( $user->ID );
// Finally redirect user
$redirect_to = get_dashboard_url($user->ID);
wp_safe_redirect( $redirect_to );
exit();
}
else {
echo "Secret key mismatch!";
exit();
}
}
else {
echo "Could not find user!";
exit();
}
}
else {
echo "Parameters are missing!";
exit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment