Skip to content

Instantly share code, notes, and snippets.

@cad
Last active January 3, 2016 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cad/4865558a7f22baced60b to your computer and use it in GitHub Desktop.
Save cad/4865558a7f22baced60b to your computer and use it in GitHub Desktop.
services
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