0

Trying to find a complete example of extending (adding functionality) Parse.User. There is alot out there for extending other objects, but it seems Parse.User (and also Parse.Role and Parse.Installation) are special exceptions.

There are a few SO posts out there with answers, but unfortunately the links that they point to are the old parse.com site, which don't map up to the new site.

Let's say we have an extra field in the User table/class called IsAdmin and I want to add an extra method to check for that field (Yes can use Parse.User.getCurrent().get('IsAdmin), but this is just an example). As far as I can tell, albeit with snippets of examples from here and there, I "think" it sort of looks like this:


/**
 * I "think" you need this. Docs don't explain how its fully used
 */
Parse.User.allowCustomUserClass(true);

export class User extends Parse.User {

  constructor() {
    super('User');
  }

  isThisUserAnAdmin(): boolean {
    return !(!this.get('IsAdmin));
  }
}

Parse.Object.registerSubclass('User', User);

/**
 * Then I want to use it and call specific 'Parse.User' superclass
 * functions + get the actual `User` model and NOT the `Parse.User`
 */
const makeAnAdmin = async (email: string, password: string): Promise<User> => {
  return User.signUp(email, password,
    {
      email: email,
      isAdmin: false
    }
  );
}

Again, looks ugly, but this is a mish-mash of what I could find. Also feel free to tell me off if there is an obvious answer in the docs as I've been bashing my head on this all day.

DrunkenBeetle
  • 267
  • 4
  • 2

1 Answers1

0

I use the code below to manage my users. I'm able to see the JSON with the result after using the login or sign up method. I hope that the code below work for you.

Parse.User.allowCustomUserClass(true);

export class User extends Parse.User {
  email: string;
  password: string;
  isAdmin: string;

  constructor() {
    super('_User');
  }

  doLogin(email: string, password: string) {
    Parse.User.logIn(email,  password).then(object => {
      console.log(object);
      this.isAdmin = object.get('isAdmin');
    }).catch(console.error);
  }

  doSignUp(username: string, email: string, password: string) {
    return makeAnAdmin(email, password);
  }

}

Parse.Object.registerSubclass('User', User);

const makeAnAdmin = async (email: string, password: string): Promise<User> => {
  return Parse.User.signUp(email, password,
    {
      email: email,
      isAdmin: false
    }
  );
}

If it still doesn't work, paste here the message that you're facing :)

nataliec
  • 502
  • 4
  • 14
  • Hi @nataliec, wouldn't calling `doSignUp` return you an instance of `Parse.User` and not `User`? Same with the `doLogin` method as `object` would be of type `Parse.User` and not `User`. I'm getting tslint errors with the `makeAnAdmin` function because of it – DrunkenBeetle Jan 31 '19 at 05:34