Advertisement
Guest User

Untitled

a guest
Feb 7th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. const userSchema = Schema({
  2. local: {
  3. email: {
  4. type: String,
  5. unique: true,
  6. },
  7. password: String,
  8. name: String
  9. },
  10. facebook: {
  11. id: String,
  12. token: String,
  13. email: String,
  14. name: String
  15. },
  16. ...
  17.  
  18. const tokenForUser = (user) => {
  19. const timestamp = new Date().getTime();
  20. return jwt.encode({ sub: user._id, iat: timestamp }, SECRET);
  21. };
  22.  
  23. const facebookSignIn = (model, cb) => {
  24. User.findOne({ 'facebook.id': model.id }, (err, docs) => {
  25. if (err) {
  26. return cb({ 'status': HTTP_INTERNAL_SERVER_ERROR,
  27. 'message': { error: 'Unable to search for an user due to an unknown error' } });
  28. } else if (docs) {
  29. return cb(null, { token: tokenForUser(docs) });
  30. }
  31.  
  32. const user = new User({
  33. facebook: {
  34. id: model.id,
  35. token: model.accessToken,
  36. email: model.email,
  37. name: model.name
  38. }
  39. });
  40.  
  41. user.save((err) => {
  42. if (err) {
  43. if (err.name === VALIDATION_ERROR) {
  44. return cb({ 'status': HTTP_PRECONDITION_FAILED,
  45. 'message': { error: Object.keys(err.errors).map(e => `${err.errors[e].message} `) } });
  46. }
  47. return cb({ 'status': HTTP_INTERNAL_SERVER_ERROR,
  48. 'message': { error: 'Unable to save user due to an unknown error' } });
  49. }
  50.  
  51. return cb(null, { token: tokenForUser(user) });
  52. });
  53. });
  54. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement