What is the best way to allow a user to use a node.js app using their user account from a WordPress page?
I have tried storing session information in Redis, but I am not very familiar with PHP and have run into a dead end where the session isn’t being stored. I have used this guide.
I have also tried using node-phpass but it is not very well documented. I have successfully connected to the WordPress database, but the password hashes I generate with node-phpass do not match the hashes in the wp_users table.
This is what I’m using to test node-phpass:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'wordpress',
password : 'jPWrwvGbYXMADd8F',
database : 'wordpress',
});
connection.query('SELECT * FROM wp_users', function(err, rows) {
if (err) {
console.log("Database error: " + err);
} else {
for (var i = 0; i < rows.length; i++){
console.log("Rows[" + i + "] : " + rows[i].user_login + " " + passwordHash.checkPassword('secret',rows[i].user_pass));
}
}
});
Aside from sharing the session, are there any other better ways to do this? OAuth?
I didn’t quite understand if you want to the user authenticate primarily on your node.js application and then creates the cookie for WordPress, or the opposite, but once you understand how WordPress are doing it, you’ll be able to accomplish both.
First of all, WordPress use lots of hashes and salts to security its cookies, so you need to know where these hashes and salts are to be able to define the same values for those on you nodejs application.
On
wp-config.php
you will find the constants of the hashes and salts we’re looking for, I think you just need to transport to your nodejs application the constant values forLOGGED_IN_KEY
,LOGGED_IN_SALT
,NONCE_KEY
andNONCE_SALT
. You might give a better look on the filewp-includes/default-constants.php
where WordPress keep its default constant values, pay attention to the if(defined(..)) because if the constant is defined in somewhere else prior to this file (probably on thewp-config.php
) the value will be from that not from thewp-includes/default-constants.php
file.Now, you need to understand how WordPress is creating its cookies.
This is an example of logged in cookie:
You need to make your nodejs application understand this cookie, being able to break it just like WordPress.
The functions to create authentication cookies on WordPress resides on
wp-includes/pluggable.php
Note that the constant
LOGGED_IN_COOKIE
is default set onwp-includes/default-constants.php
, on the same file you have theCOOKIEHASH
constant, you need to transport the value of that to your nodejs application to be able to read and validate the cookie name too, theCOOKIEHASH
is just the website URL hashed with md5.if you don’t want to transport all that, you can just set on wp-config.php the
LOGGED_IN_COOKIE
constant with the logged in cookie name that you want for WordPress and portrait just this constant to your nodejs, like this:And finally you’ll need to portrait the the functions
wp_generate_auth_cookie
,wp_hash
and hash_hmacto your nodejs application if you want to create the cookie there, the first two functions reside on the file
wp-inclues/pluggable.phpthe
hash_hmacresides on
wp-includes/compat.php`.Once you understand what
wp_generate_auth_cookie
function is doing, you’ll also be able to decode the cookie and authenticate the WordPress user on your nodejs application.Note that what WordPress is doing for the the logged in cookie value, is simply slicing the hashed password for the user on your database, merging with the user login name and the expiration time (unix time) for the cookie, and hashing all together. Then he creates the cookie value with user login name, the expiration time for the cookie and the hash that he just created, with the first two values he can verify the “token hash” with the hashed password from the user.
You might want to know that to logout an user on WordPress you need to pass the nonce key for the user on the logout url, so if you want to logout an user from WordPress by your nodejs application, you might want to transport the nonce key functions.
If your application will be in a subdomain, you might want to declare on
wp-config.php
, or WordPress will just use the full domain on the cookie, and the cookie will not be available for subdomains.Hope this give you better directions. Good luck.
Have a look at this npm package: https://www.npmjs.com/package/wp-auth
This is a cookie-based authentication package that lets you authenticate users in nodejs based on the cookies.
I am using it myself for authentication users in my node app using wordpress cookies.
I hope this helps.
To extend the already given answer there are a couple of node.js libraries to authenticate a user with wordpress via cookies, exactly like wordpress does by default
https://github.com/j3k0/node-wordpress-auth (fork of https://github.com/Nightgunner5/node-wordpress-auth)
https://github.com/jhurliman/node-phpass (a port of https://github.com/rchouinard/phpass which is same library WP is using to hash passwords, to node.js)
The wordpress-auth version installed using the npm install method is outdated. Download and use the version from the modules website.