1

I'm developing a library in order to get access to a REST ednpoint.

I've generated a Injectable class foreach REST endpoint (you can take a look on here. Now, I need to add OAuth dance to this callings, and I've thought to create a OAuthService class:

import { Injectable } from '@angular/core';
import { Http, Headers, URLSearchParams } from '@angular/http';

import { OAuthSettings } from './OAuthSettings';

@Injectable()
export class OAuthService
{
    constructor(private http: Http, private settings: OAuthSettings) { }

    grantAuthorization() {}

    getAccessToken() {}

    refreshAccessToken() {}
}

, where OAuthSettings is:

export class OAuthSettings {
    baseAuthzURI: string;
    baseCESTURI: string;

    client: string;
    user: string;
    passwd: string;

    authzCode: string;
    accessToken: string;
    refreshToken: string;
    authCodeThreshold: Date;
    accessTokenThreshold: Date;
    refreshTokenThreshold: Date;
}

I'm able to figure out that I need to add an OAuthService on UserApi as dependency. But, what about OAuthSettings, how is this provided, or initialized?

This library it's used by an application which's going to need to provide an user and a password. So, these fields have to be provided on OAuthSettings by someway.

3
  • Add a method to OAuthService which returns the settings for that instance. Commented Dec 2, 2016 at 12:26
  • Sorry, but I'm not able to figure out what you are trying to mean. I've edited the post and I've added user and password are unknown until an app (which's using my library) read it from any form. Commented Dec 2, 2016 at 12:33
  • @Jordi: edit your title (anGular, not anDular) Commented Dec 2, 2016 at 12:34

1 Answer 1

2
@NgModule({
  providers: [{ provide: OAuthSettings, useValue: {...}}]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Günter. Please, could you expand a bit your answer taking these gists: LoginModule.ts, and LoginComponent.ts and LoginTemplate? I'm really a newbie with angular2 and it really would help me a bit better.
Sorry, I don't get how these gists are related. You need to register a provider then DI can inject it to other services, components, ...
Where do I need to add this annotation: on root module or on login module?
On any module that is imported somewhere and that is not lazy loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.