angular-token icon indicating copy to clipboard operation
angular-token copied to clipboard

Adding properties to UserData

Open davidjconnolly opened this issue 8 years ago • 7 comments

I'm trying to add a 'role' property to my users, however I'm getting typescript compilation errors as role isn't defined on UserData. As a workaround, I'm using lodash _.get to trick the compiler but this is obviously not an ideal solution :P

Short of forking the repo and changing the UserData definition, is there a better way to do this?

Thanks!

davidjconnolly avatar May 06 '17 16:05 davidjconnolly

@davidjconnolly Or you could fork the repo, modify it for the use of an extended UserData (which is managed by Devise), and create an PR when your done :smile:. Its on my ToDo-Liste, but Im quite busy recently.

neroniaky avatar May 07 '17 11:05 neroniaky

@davidjconnolly Could you explain further how you solved this issue? I have been at this for hours on end. No luck getting this done at my end!

When I use _.get(_tokenService.currentUserData, 'firstname'); and assign the value of this to the variable, I still get the error

v-osazemeu avatar Jun 02 '17 09:06 v-osazemeu

I was able to temporarily bypass this issue by using this aot false flag for the production build. ng build --prod --aot=false

This is only a temporal solution.

v-osazemeu avatar Jun 05 '17 08:06 v-osazemeu

I found another solution to this problem, unfortunately it requires a fair bit of coding:

  1. Create a User object class with all the attributes you want in it. For example:

export class User {
  constructor(
    public id: string,
    public uid: string,
    public account_id: string,
    public email: string,
    public name: string,
    public nickname: string,
    public provider: string,
    public image: string, // this will be a url
    public is_onboarded: boolean,
    public created_at: string,
    public updated_at: string,
    public last_sign_in_at: string,
  ) {}

}

  1. Create a user service that contains a public object of type User, eg:

public current_user: User

  1. In the constructor of this service, inject the Angular2TokenService and subscribe to the validateToken method:
constructor (
   private _authService: Angular2TokenService
) {
  this._authService.validateToken().subscribe(
     result => {
          if (result.status == 200)
              this.current_user = result.json().data;
     }
   )
}
  1. Inject this service into any other service that you need. For example, I have a template service that creates templates, and since I am using the methods exposed by Angular2TokenService to call my API, every request will automatically fire the validateToken. Example from my \services\template.service.ts:
// this simply creates a new Template, given a name, 
// but it needs to send the users account_id to the api 
// [the account has_many users, user belongs_to account in Rails)

  private newTemplate(name: string): Template {
    let template = new Template();
    template.name = name;
    template.account_id = this._userService.current_user.account_id;
    return template;
  }

  postTemplate(name: string): Observable<any> {
    return this._authService.post('templates', this.newTemplate(name))
      .map((response) => {
        return response.json()
      });
  }

rmcsharry avatar Oct 02 '17 13:10 rmcsharry

I found a much easier solution. Just extend the base UserData interface:

import { UserData } from 'angular2-token;

export MyUserData extends UserData {
    role: string;
}

You then use the MyUserData type in your components.

izambard avatar Dec 09 '17 21:12 izambard

@izambard that should be the correct solution here, and no edit to the repo should be required to allow users to modify the userData type for their scenario.

If this could be added to the readme or be documented in some way, that would be great.

donsisco avatar Dec 19 '17 14:12 donsisco

What should I do if I'm using tokenService.currentUserData.role?

2624789 avatar Aug 09 '19 11:08 2624789