Skip to content

New Authentication Middleware using Wordpress #982

Closed
@nkappler

Description

@nkappler

Hi @mevdschee,

I'm currently trying to marry your api to the wordpress user session.
Unfortunately it is not as easy as pointing the api to the 'users' table, because WP uses neither the standard $_SESSION variable, nor standard password encryption (it uses some custom salt and appends an ID or something...)

So far I was able to write a custom script which checks if a wordpress session is available and which can verify a password from the database:

<?php
// Load the WordPress environment
define('WP_USE_THEMES', false);
require('path_to_wordpress_installation/wp-load.php'); // <- the base path needs to be configurable

// Function to check user's password
function verify_user_password($email, $plain_text_password)
{
    // Check if the user is logged in
    if (is_user_logged_in()) {
        // User is logged in, retrieve user ID and email
        $user = wp_get_current_user();
    
        // Do something useful here
        echo "User ID: $user->ID<br>";
        echo "User Email: $user->user_email<br>";
        echo "User Display Name: " . $user->display_name . "<br>";
        return;
    }

    // Retrieve the user data based on the provided email
    $user_data = get_user_by('email', $email);

    if (!$user_data) {
        echo "User not found with the provided email.";
        exit;
    }

    if (wp_check_password($plain_text_password, $user_data->user_pass)) {
        // do something useful here instead of echo...

        // Password is correct, display all user data
        echo "User ID: " . $user_data->ID . "<br>";
        echo "User Display Name: " . $user_data->display_name . "<br>";
        echo "User Email: " . $user_data->user_email . "<br>";
        // Add more fields as needed
    } else {
        echo "Password is incorrect.";
    }
}

// just for testing:
// Get user email and unhashed password from GET parameters
$email = isset($_GET['email']) ? $_GET['email'] : '';
$plain_text_password = isset($_GET['password']) ? $_GET['password'] : '';
verify_user_password($email, $plain_text_password);

I'd like to write a middleware similar to DBAuth and I would be happy to file a PR but I am not very good at php and I don't know where to start.

Any help is appreciated 🙂

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions