Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UserApi problem with confirm #147

Closed
spid3r opened this issue Oct 11, 2016 · 3 comments
Closed

UserApi problem with confirm #147

spid3r opened this issue Oct 11, 2016 · 3 comments
Assignees
Milestone

Comments

@spid3r
Copy link

spid3r commented Oct 11, 2016

Hi guys,

first of all a big shout out to you all for building such a nice SDK for angular2! I love it!
You guys rock!

I found a problem on the UserApi confirm method:
Loopback documentation defines the GET confirm request with 3 query params:

  • uid
  • token
  • redirect

but the generated confirm method in Class UserApi only puts the token and redirect as urlParam and the uid as a route param:

  /**
   * Confirm a user registration with email verification token.
   *
   * @param string uid 
   *
   * @param string token 
   *
   * @param string redirect 
   *
   * @returns object An empty reference that will be
   *   populated with the actual data once the response is returned
   *   from the server.
   *
   * This method returns no data.
   */
  public confirm(uid: any, token: any, redirect: any = undefined): Observable<any> {
    let method: string = "GET";
    let url: string = LoopBackConfig.getPath() + "/" + LoopBackConfig.getApiVersion() +
    "/users/confirm";
    let routeParams: any = {
      uid: uid
    };
    let postBody: any = {};
    let urlParams: any = {};
    if (token) urlParams.token = token;
    if (redirect) urlParams.redirect = redirect;
    let result = this.request(method, url, routeParams, urlParams, postBody);
    return result;
  }

This leads to an error which gets returned by loopback framework when calling the generated confirm method of UserApi:

{
"error": {
"name": "Error",
"status": 400,
"message": "uid is a required argument",
"statusCode": 400,
"stack": "Error: uid is a required argument\n...
}

I think the problem can be found and solved
here in loopback-sdk-builder/lib/angular2/index.js#526: where you want to filter unallowed query params :
urlParams = urlParams.filter(param => (param.arg && !param.arg.match(/(id|fk|data|options|credentials)/g)));
so the filter is being applied to the function argument uid as well which leads to this problem.
Here is the Loopback Model definition of the confirm method:

/**
   * Confirm the user's identity.
   *
   * @param {Any} userId
   * @param {String} token The validation token
   * @param {String} redirect URL to redirect the user to once confirmed
   * @callback {Function} callback
   * @param {Error} err
   */
  User.confirm = function(uid, token, redirect, fn) {
    fn = fn || utils.createPromiseCallback();
    this.findById(uid, function(err, user) {
      if (err) {
        fn(err);
      } else {
        if (user && user.verificationToken === token) {
          user.verificationToken = null;
          user.emailVerified = true;
          user.save(function(err) {
            if (err) {
              fn(err);
            } else {
              fn();
            }
          });
        } else {
          if (user) {
            err = new Error(g.f('Invalid token: %s', token));
            err.statusCode = 400;
            err.code = 'INVALID_TOKEN';
          } else {
            err = new Error(g.f('User not found: %s', uid));
            err.statusCode = 404;
            err.code = 'USER_NOT_FOUND';
          }
          fn(err);
        }
      }
    });
    return fn.promise;
  };
@jonathan-casarrubias
Copy link
Collaborator

Hi @spid3r thanks for reaching out!!

Hey so, yes it seems that I will need to improve the regex to avoid other words containing id, like uid.

Thanks for contributing with this issue.

Cheers!!
Jon

@jonathan-casarrubias jonathan-casarrubias added this to the 2.1.0-beta.11 milestone Oct 18, 2016
jonathan-casarrubias pushed a commit that referenced this issue Oct 18, 2016
- Fix: #157
- Fix: #155
- Fix: #154
- Fix: #153
- Fix: #151
- Fix: #147
jonathan-casarrubias pushed a commit that referenced this issue Oct 18, 2016
- Fix: #157
- Fix: #155
- Fix: #154
- Fix: #153
- Fix: #151
- Fix: #147
jonathan-casarrubias pushed a commit that referenced this issue Oct 18, 2016
- Fix: #157
- Fix: #155
- Fix: #154
- Fix: #153
- Fix: #151
- Fix: #147
jonathan-casarrubias pushed a commit that referenced this issue Oct 18, 2016
- Fix: #157
- Fix: #155
- Fix: #154
- Fix: #153
- Fix: #151
- Fix: #147
jonathan-casarrubias pushed a commit that referenced this issue Oct 18, 2016
- Fix: #157
- Fix: #155
- Fix: #154
- Fix: #153
- Fix: #152
- Fix: #151
- Fix: #148
- Fix: #147
@jonathan-casarrubias
Copy link
Collaborator

Fixed

@isrmicha
Copy link

still getting error in userApi.confirm, invalid token, anyone can help ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants