|
1 | | -# node-wp-authenticator |
2 | | -Authenticate users through node using WordPress cookies |
| 1 | +This repo is forked from Nightgunner5/node-wordpress-auth. Since The original project is not working on new node versions and is not maintained by the original author anymore I decided to update the code to make it work and publish a npm package based on that. |
| 2 | + |
| 3 | +TO INSTALL: |
| 4 | +=========== |
| 5 | + npm install wp-authenticator |
| 6 | + |
| 7 | +TO USE: |
| 8 | +======= |
| 9 | +In your init: |
| 10 | + var wp_auth = require('wp-authenticator').create({ |
| 11 | + wpurl: 'http://my-blog.example', |
| 12 | + logged_in_key: 'LOGGED_IN_KEY from wp-config.php', |
| 13 | + logged_in_salt: 'LOGGED_IN_SALT from wp-config.php', |
| 14 | + mysql_host: 'MySQL host', |
| 15 | + mysql_user: 'MySQL username', |
| 16 | + mysql_pass: 'MySQL password', |
| 17 | + mysql_db: 'MySQL database', |
| 18 | + wp_table_prefix: 'WordPress table prefix (eg. wp_)' }); |
| 19 | + |
| 20 | +When you get a HTTP request and you need to verify auth: |
| 21 | + |
| 22 | + wp_auth.checkAuth( req ).on( 'auth', function( auth_is_valid, user_id ) { |
| 23 | + auth_is_valid; // true if the user is logged in, false if they are not |
| 24 | + user_id; // the ID number of the user or 0 if the user is not logged in |
| 25 | + } ); |
| 26 | + |
| 27 | +To get a usermeta value: |
| 28 | + |
| 29 | + wp_auth.getUserMeta( user_id, 'meta_key (eg. occupation)', function( data ) { |
| 30 | + // data is null if the user doesn't exist or doesn't have the key. |
| 31 | + // Otherwise, it is the value you would get in WordPress (unserialized intelligently) |
| 32 | + } ); |
| 33 | + |
| 34 | +To set a usermeta value: |
| 35 | + |
| 36 | + wp_auth.setUserMeta( user_id, 'meta_key (eg. occupation)', 'meta_value (eg. Eating spaghetti)' ); |
| 37 | + |
| 38 | +To get a user_id from a usermeta value: |
| 39 | + |
| 40 | + wp_auth.reverseUserMeta( 'meta_key (eg. occupation)', 'meta_value (eg. Eating spaghetti)', function( id ) { |
| 41 | + // id is null if no user could be found with the given meta key value pair |
| 42 | + // Otherwise, it is the ID of the user |
| 43 | + // If multiple users matched, only one will be given |
| 44 | + } ); |
0 commit comments