Guest User

Untitled

a guest
Dec 14th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. const WPAPI = require('wpapi'),
  2. wp = require('./wp');
  3.  
  4. module.exports = function(){
  5.  
  6. return {
  7. findById: function(id, cb) {
  8. process.nextTick(function() {
  9. wp.users().id(id).get(function( err, data ) {
  10. if ( err || !data.id ) {
  11. cb(new Error('User ' + id + ' does not exist'));
  12. }
  13. return cb(null, data);
  14. });
  15. });
  16. },
  17.  
  18. findByUsername: function(username, cb) {
  19. process.nextTick(function() {
  20. wp.users().slug(username).get(function( err, data ) {
  21. user = data[0];
  22. if ( err || !user.id ) {
  23. cb(new Error('User ' + username + ' does not exist'));
  24. }
  25. return cb(null, user);
  26. });
  27. });
  28. },
  29.  
  30. authUser: function(username, password, cb) {
  31. // JUST checks if the user CAN login to WP and has a user Profile, but DOESN'T authenticate them towards it
  32. var testAuth;
  33. process.nextTick(function() {
  34. testAuth = new WPAPI({
  35. endpoint: config.auth.endpoint,
  36. username: username,
  37. password: password,
  38. auth: false
  39. });
  40. return testAuth.users().me().get(function( err, data ) {
  41. if ( err ) {
  42. // console.log('oh no',err.code )
  43. cb(new Error('Cannot authenticate ' + username + ''));
  44. // cb(new Error(err.code));
  45. }
  46. return cb(null, data); //the ME user data
  47. });
  48. });
  49. }
  50. }
  51.  
  52. }();
Add Comment
Please, Sign In to add comment