login & registration now functional

This commit is contained in:
Edin Dazdarevic
2015-03-03 19:11:38 +01:00
parent 4befef5bf4
commit 028bf79617
8 changed files with 189 additions and 71 deletions

View File

@@ -1,64 +1,105 @@
var AppDispatcher = require('../dispatcher/appDispatcher');
var UserConstants = require('../constants/userConstants');
var superagent = require('superagent');
var globals = require('../globals');
var NavigationActions = require('./navigationActions');
// Define action methods
var UserActions = {
registerUser: function(user) {
AppDispatcher.handleAction({
actionType: UserConstants.REGISTER_USER,
user: user
});
registerUser: function(user) {
AppDispatcher.handleAction({
actionType: UserConstants.REGISTER_USER,
user: user
});
user.save(null, {
success: function() {
UserActions.registrationSuccess();
},
error: function(model, response, options) {
UserActions.registrationFailure(response);
}
});
},
registrationSuccess: function() {
AppDispatcher.handleAction({
actionType: UserConstants.REGISTRATION_SUCCESS
});
},
registrationFailure: function(error) {
AppDispatcher.handleAction({
actionType: UserConstants.REGISTRATION_FAILURE,
error: error
});
},
userLogin: function(loginDetails) {
AppDispatcher.handleAction({
actionType: UserConstants.USER_LOGIN,
loginDetails: loginDetails
});
user.save(null, {
success: function() {
UserActions.registrationSuccess();
},
error: function(model, response, options) {
UserActions.registrationFailure(response);
}
});
},
registrationSuccess: function() {
AppDispatcher.handleAction({
actionType: UserConstants.REGISTRATION_SUCCESS
});
},
registrationFailure: function(error) {
AppDispatcher.handleAction({
actionType: UserConstants.REGISTRATION_FAILURE,
error: error
});
},
userLogout:function() {
AppDispatcher.handleAction({
actionType: UserConstants.USER_LOGOUT
});
loginDetails.save(null, {
success: function(){
alert('ok')
UserActions.loginSuccess();
},
superagent
.post(globals.ApiUrl + '/user/logout')
.withCredentials()
.end(function(response){
UserActions.logoutDone();
});
},
logoutDone: function() {
AppDispatcher.handleAction({
actionType: UserConstants.USER_LOGOUT_DONE
});
},
userLogin: function(loginDetails) {
AppDispatcher.handleAction({
actionType: UserConstants.USER_LOGIN
});
error: function(model, response, options){
alert('error!');
UserActions.loginFailure(response);
}
});
},
loginSuccess: function() {
AppDispatcher.handleAction({
actionType: UserConstants.LOGIN_SUCCESS
});
},
loginFailure: function(error) {
AppDispatcher.handleAction({
actionType: UserConstants.LOGIN_FAILURE,
error: error
});
}
superagent.post(globals.ApiUrl + '/user/login')
.withCredentials()
.send(loginDetails)
.end(function(response) {
if(response.unauthorized){
UserActions.loginFailure(response.body);
} else {
UserActions.loginSuccess(response.body);
}
});
},
loginSuccess: function(user) {
AppDispatcher.handleAction({
actionType: UserConstants.LOGIN_SUCCESS,
user: user
});
},
loginFailure: function(error) {
AppDispatcher.handleAction({
actionType: UserConstants.LOGIN_FAILURE,
error: error
});
},
checkLogin: function() {
AppDispatcher.handleAction({
actionType: UserConstants.CHECK_LOGIN
});
superagent
.get(globals.ApiUrl + '/user')
.withCredentials()
.end(function(response){
if(response.unauthorized){
UserActions.checkLoginArrived(null, true);
} else {
UserActions.checkLoginArrived(response.body);
}
});
},
checkLoginArrived: function(user, error) {
AppDispatcher.handleAction({
actionType: UserConstants.CHECK_LOGIN_ARRIVED,
user: user,
error: error
});
}
};
module.exports = UserActions;