Skip to content

Instantly share code, notes, and snippets.

@dpastoor
Created January 10, 2016 17:49
Show Gist options
  • Select an option

  • Save dpastoor/7af22aada21119dc1b08 to your computer and use it in GitHub Desktop.

Select an option

Save dpastoor/7af22aada21119dc1b08 to your computer and use it in GitHub Desktop.
import { domain } from 'config'
export default {
signup: async function (body, cb) {
let response = await fetch(`${domain}:8080/signup`, {
method: `POST`,
headers: {
'Content-Type': `application/json`
},
body: JSON.stringify(body)
})
let { success, message } = await response.json()
if (success) this.login(body, cb)
else cb({ success, message })
},
login: async function (body, cb) {
let response = await fetch(`${domain}:8080/api/authenticate`, {
method: `POST`,
headers: { 'Content-Type': `application/json` },
body: JSON.stringify(body)
})
let { success, message, token, user } = await response.json()
if (success) {
localStorage.token = token
localStorage.userId = user._id
localStorage.userEmail = user.email
cb({ success, message, user })
}
else {
cb({ message })
}
},
logout (cb) {
delete localStorage.token
if (cb) cb()
},
loggedIn() {
return !!localStorage.token
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment