Can create a new user
This commit is contained in:
		
							parent
							
								
									d4bd0e915c
								
							
						
					
					
						commit
						6f6faee122
					
				
					 7 changed files with 53 additions and 3 deletions
				
			
		| 
						 | 
				
			
			@ -6,5 +6,11 @@ export default Ember.Route.extend(ApplicationRouteMixin, {
 | 
			
		|||
    invalidateSession: function() {
 | 
			
		||||
      this.get('session').invalidate();
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    didTransition: function() {
 | 
			
		||||
      this.get('flashMessages').clearMessages();
 | 
			
		||||
      return true;
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
});
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,6 +7,7 @@ export default Ember.Controller.extend({
 | 
			
		|||
      let authenticator = 'simple-auth-authenticator:token';
 | 
			
		||||
 | 
			
		||||
      this.set('loading', true);
 | 
			
		||||
      // Manually clean up because there might not be a transition
 | 
			
		||||
      this.get('flashMessages').clearMessages();
 | 
			
		||||
      this.get('session').authenticate(authenticator, credentials).then(()=>{
 | 
			
		||||
        this.set('loading', false);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,3 +1,4 @@
 | 
			
		|||
import Ember from 'ember';
 | 
			
		||||
import DS from 'ember-data';
 | 
			
		||||
 | 
			
		||||
export default DS.RESTAdapter.extend({
 | 
			
		||||
| 
						 | 
				
			
			@ -6,4 +7,21 @@ export default DS.RESTAdapter.extend({
 | 
			
		|||
    return this.get('globals.apiURL');
 | 
			
		||||
  }.property(),
 | 
			
		||||
  coalesceFindRequests: true,
 | 
			
		||||
  ajaxError: function(jqXHR) {
 | 
			
		||||
    // http://stackoverflow.com/a/24027443
 | 
			
		||||
    var error = this._super(jqXHR);
 | 
			
		||||
    if (jqXHR && jqXHR.status === 422) {
 | 
			
		||||
      var response = Ember.$.parseJSON(jqXHR.responseText),
 | 
			
		||||
        errors = {};
 | 
			
		||||
      if (response.errors !== undefined) {
 | 
			
		||||
        var jsonErrors = response.errors;
 | 
			
		||||
        Ember.EnumerableUtils.forEach(Ember.keys(jsonErrors), function(key) {
 | 
			
		||||
          errors[Ember.String.camelize(key)] = jsonErrors[key];
 | 
			
		||||
        });
 | 
			
		||||
      }
 | 
			
		||||
      return new DS.InvalidError(errors);
 | 
			
		||||
    } else {
 | 
			
		||||
      return error;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
});
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,7 +9,15 @@ export default Ember.Component.extend({
 | 
			
		|||
      var user = this.get('user');
 | 
			
		||||
 | 
			
		||||
      if (user.get('isDirty')) {
 | 
			
		||||
        user.save();
 | 
			
		||||
        user.save().then(() => {
 | 
			
		||||
          this.sendAction();
 | 
			
		||||
        }).catch(() => {
 | 
			
		||||
          // Manually clean up messages because there is no transition
 | 
			
		||||
          this.get('flashMessages').clearMessages();
 | 
			
		||||
          user.get('errors').forEach((error) => {
 | 
			
		||||
            this.get('flashMessages').error(`${error.attribute.capitalize()} - ${error.message}`);
 | 
			
		||||
          });
 | 
			
		||||
        });
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
  },
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,4 +11,12 @@ export default Ember.Route.extend(UnauthenticatedRouteMixin, {
 | 
			
		|||
  setupController: function(controller, model) {
 | 
			
		||||
    controller.setProperties(model);
 | 
			
		||||
  },
 | 
			
		||||
 | 
			
		||||
  actions: {
 | 
			
		||||
    success: function() {
 | 
			
		||||
      this.transitionTo('login');
 | 
			
		||||
      this.get('flashMessages').information(`You have successfully signed up.
 | 
			
		||||
        Please check your email for further instructions.`);
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
});
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1 +1 @@
 | 
			
		|||
{{users/new/new-user-form user=user}}
 | 
			
		||||
{{users/new/new-user-form user=user action="success"}}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -47,7 +47,16 @@ module.exports = function(app) {
 | 
			
		|||
  });
 | 
			
		||||
 | 
			
		||||
  usersRouter.post('/', function(req, res) {
 | 
			
		||||
    res.status(201).end();
 | 
			
		||||
    // req.body.user.id = Math.max.apply(Math, USERS.map(function(o){return o.id;})) + 1;
 | 
			
		||||
    // res.status(201).send(req.body);
 | 
			
		||||
    // NOTE - use the following for testing errors
 | 
			
		||||
    res.status(422).send({
 | 
			
		||||
      'errors':{
 | 
			
		||||
        "name": ["Must provide a value"],
 | 
			
		||||
        "email": ["Must provide a value"],
 | 
			
		||||
        "password": ["Must provide a value"],
 | 
			
		||||
      }
 | 
			
		||||
    }).end();
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  usersRouter.get('/:id', function(req, res) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Reference in a new issue