Last active
January 3, 2016 14:34
-
-
Save cad/4865558a7f22baced60b to your computer and use it in GitHub Desktop.
services
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 request from 'reqwest'; | |
import when from 'when'; | |
var AUTH_BASE_URL = "http://localhost:5000" | |
var AUTHENTICATE_URL = AUTH_BASE_URL + "/auth/" | |
var TASK_BASE_URL = "http://localhost:5001" | |
var AuthActions = { | |
authenticateUser: (accessToken) => { | |
var savedAccessToken = localStorage.getItem('access_token'); | |
// Dispatch action login here | |
if (savedAccessToken !== accessToken) { | |
// do your transitions here | |
localStorage.setItem('acces_token', accessToken); | |
} | |
}, | |
deauthenticateUser: () => { | |
// do your deauthenticate transitions | |
localStorage.removeItem('access_token'); | |
// Dispatch here deauth | |
} | |
} | |
class AuthService { | |
authenticate(email, password) { | |
return this.handleAuth(when(request({ | |
url: AUTHENTICATE_URL, | |
method: 'POST', | |
crossOrigin: true, | |
type: 'json', | |
data: { | |
email: email, | |
password: password | |
} | |
}))); | |
} | |
deauthenticate() { | |
AuthActions.deauthenticateUser(); | |
} | |
handleAuth(authPromise) { | |
return authPromise | |
.then(function(response) { | |
var accessToken = response.access_token; | |
AuthActions.authenticateUser(accessToken); | |
return true; | |
}); | |
} | |
} | |
class TaskService { | |
createTask(body, title) { | |
request({ | |
url: TASK_BASE_URL+ '/task/', | |
method: 'POST', | |
crossOrigin: true, | |
headers: { | |
'Authorization': 'Bearer ' + AuthStore.accessToken | |
}, | |
data: { | |
body: body, | |
title: title | |
} | |
}) | |
.then(function(response) { | |
// do your actions here | |
// TaskActions.taskCreated(response); | |
}); | |
} | |
completeTask(taskId) { | |
request({ | |
url: TASK_BASE_URL + '/task/'+ taskID, | |
method: 'POST', | |
crossOrigin: true, | |
headers: { | |
'Authorization': 'Bearer ' + AuthStore.accessToken | |
} | |
}) | |
.then(function(response) { | |
// do your actions here | |
// TaskActions.taskCompleted(response); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment