Last active
March 9, 2017 19:20
-
-
Save tphdev/40500251d6b60d589c1183621071d0de to your computer and use it in GitHub Desktop.
a user model for authentication w/ backbone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Backbone from 'backbone' | |
import $ from 'jquery' | |
export const UserModel = Backbone.Model.extend({ | |
initialize: function(){ | |
}, | |
urlRoot: '/api/users', | |
idAttribute: '_id' | |
}) | |
UserModel.logIn = function(username, password){ | |
if(typeof username !== 'string' || typeof password !== 'string' ){ throw new Error(`UserModel.login() must receive string 2 string paramaters for username and password`) } | |
return $.ajax({ | |
method: 'POST', | |
data: JSON.stringify({ username: username, password: password}), | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
url: '/auth/login' | |
}) | |
} | |
UserModel.register = function(dataObj){ | |
if(typeof dataObj !== 'object' ){ throw new Error(`UserModel.register() must receive an object`) } | |
if(typeof dataObj.username === 'undefined' || typeof dataObj.password === 'undefined' ){ throw new Error(`UserModel.register() must receive an object w/ username + password`) } | |
return $.ajax({ | |
method: 'POST', | |
data: JSON.stringify(dataObj), | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
url: '/auth/register' | |
}) | |
} | |
UserModel.getCurrentUser = function(){ | |
return $.ajax({ | |
method: 'GET', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
url: '/auth/current' | |
}) | |
} | |
UserModel.logOut = function(){ | |
console.log('logging in!') | |
return $.ajax({ | |
method: 'GET', | |
url: '/auth/logout' | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment