neroniaky / angular-token

:key: Token based authentication service for Angular with interceptor and multi-user support. Works best with devise token auth for Rails. Example:
https://stackblitz.com/github/neroniaky/angular-token
MIT License
370 stars 190 forks source link

Adding properties to UserData #231

Open davidjconnolly opened 7 years ago

davidjconnolly commented 7 years ago

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!

neroniaky commented 7 years ago

@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.

osazemeu commented 7 years ago

@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

osazemeu commented 7 years ago

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.

rmcsharry commented 6 years ago

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, ) {}

}


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

`public current_user: User`

3. 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; } ) }

4. 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 { return this._authService.post('templates', this.newTemplate(name)) .map((response) => { return response.json() }); }

izambard commented 6 years ago

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.

donsisco commented 6 years ago

@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.

2624789 commented 5 years ago

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