0
votes

I have a link in an email for resetting a user password. This link takes the user to a password-reset template with a parameter containing the reset token:

http://localhost:8000/password-reset?token=asldfasjdfalsdf

I need to get the token in the PasswordResetController on transition from the email link. I have tried using queryParams but haven't had any luck. I tried the code below and also using window.location.href but the problem is getting the param on transition to the page before the controller is loaded. Thank you in advance for the help.

App.NewPasswordController = Ember.Controller.extend
  queryParams: ['token']
  token: null
  actions:
    submit: ->
      token = @get 'token' # Here lies the culprit.
      password = @get 'password'
      passwordConfirmation = @get 'passwordConfirmation'
      data = JSON.stringify password: password, password_confirmation: passwordConfirmation, token: token
      ic.ajax.request(type: 'PUT', url: 'api/v1/password_resets/update', data: data).then (result)=>
        console.log result
        if result.status == 'ok'
          console.log 'ok'
          # Log user in
        else
          console.log 'Error: ForgotPasswordController#submit'

SOLUTION

I had tried query params before but just saw on the docs today that you have to be using one of the newer beta versions of Ember for it to work. I switched to 1.7.0-beta.4 and query params is now working great.

App.NewPasswordController = Ember.Controller.extend
  queryParams: ['token']
  token: null
  actions:
    submit: ->
      token = @get 'token'
2
Are you attempting to link directly to the NewPassword Controller from a link in the email? - user1225352
@user1225352 Yes I am. - sturoid

2 Answers

1
votes

You can do this by using Query Params. You'll need to activate the feature with a feature flag like this:

Ember.FEATURES['query-params-new'] = true;

Then in your controller, just do:

App.NewPasswordController= Ember.ObjectController.extend({
  queryParams: ['token'],
  token: null
});

Then you can just get the token property as normal.

You'll need a more recent version of Ember.js for this to work, like 1.6.

1
votes

If you don't want to use feature flags to active query params in a beta build of Ember, you can capture the query string before the application initializes. This is very hacky but would allow you to capture the token to use in your Ember app. Place something like the following before Em.Application.create():

window.queryStr = (function(name) {
  var urlSplit = window.location.href.split('?');

  return urlSplit.length > 1 ? urlSplit[1] : null;
})();

Then you can access window.queryStr in your Ember app.

It goes without saying that as soon as Query Params are in a stable release of Ember that you feel comfortable using you should use Ember's core query params feature instead of the above method.