Advertisement
Guest User

Untitled

a guest
Mar 9th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. import Backbone from 'backbone'
  2. import $ from 'jquery'
  3.  
  4. export const UserModel = Backbone.Model.extend({
  5. initialize: function(){
  6.  
  7. },
  8. urlRoot: '/api/users',
  9. idAttribute: '_id'
  10. })
  11.  
  12. UserModel.logIn = function(username, password){
  13. if(typeof username !== 'string' || typeof password !== 'string' ){ throw new Error(`UserModel.login() must receive string 2 string paramaters for username and password`) }
  14.  
  15. return $.ajax({
  16. method: 'POST',
  17. data: JSON.stringify({ username: username, password: password}),
  18. headers: {
  19. 'Content-Type': 'application/json'
  20. },
  21. url: '/auth/login'
  22. })
  23. }
  24.  
  25. UserModel.register = function(dataObj){
  26. if(typeof dataObj !== 'object' ){ throw new Error(`UserModel.register() must receive an object`) }
  27. if(typeof dataObj.username === 'undefined' || typeof dataObj.password === 'undefined' ){ throw new Error(`UserModel.register() must receive an object w/ username + password`) }
  28.  
  29. return $.ajax({
  30. method: 'POST',
  31. data: JSON.stringify(dataObj),
  32. headers: {
  33. 'Content-Type': 'application/json'
  34. },
  35. url: '/auth/register'
  36. })
  37. }
  38.  
  39. UserModel.getCurrentUser = function(){
  40. return $.ajax({
  41. method: 'GET',
  42. data: JSON.stringify({ username: username, password: pw}),
  43. headers: {
  44. 'Content-Type': 'application/json'
  45. },
  46. url: '/auth/current'
  47. })
  48. }
  49.  
  50. UserModel.logOut = function(){
  51. console.log('logging in!')
  52. return $.ajax({
  53. method: 'GET',
  54. url: '/auth/logout'
  55. })
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement