IdentityModel / oidc-client-js

OpenID Connect (OIDC) and OAuth2 protocol support for browser-based JavaScript applications
Apache License 2.0
2.43k stars 842 forks source link

TypeScript can't find Oidc.UserManager #108

Closed Martin-Andersen closed 8 years ago

Martin-Andersen commented 8 years ago

I am trying to create a new instance of: "var mgr = new Oidc.UserManager(config);" But VSCode are unable to find the namespace Oidc.

I am using JSPM and have made this adjustments. The index.d.ts in my wwwroot looks like this

/// <reference path="../typings/index.d.ts" />
/// <reference path="jspm_packages/github/IdentityModel/oidc-client-js@1.1.0/oidc-client.d.ts" />

I have replaced callback:(ev:null) with callback:(ev:any) and included the oidc-client.d.ts in package.json

What am I missing?

brockallen commented 8 years ago

Not sure -- I'm not a TS user. If you learn anything, please report back here so I/we can fix whatever is needed. Thanks.

nikki603 commented 8 years ago

@Martin-Andersen I am also using VS Code, Typescript 2. Here is what works for me if it will help you:

/// <reference path="../../config/oidc-client.d.ts" />
import { Injectable } from '@angular/core';

const settings: any = {
  client_id: 'client1',
  authority: 'http://localhost:5000/',
  response_type: 'id_token token',
  scope: 'openid email profile',
  loadUserInfo: true,
  redirect_uri: 'http://localhost:3000/signInCallback.html',
  post_logout_redirect_uri: 'http://localhost:3000/',
  monitorSession: true
};

@Injectable()
export class AuthService {
  mgr: any = new UserManager(settings);
  private loggedIn: boolean = false;

  constructor() {
    this.mgr.getUser()
      .then((user) => {
        if (user) {
          this.loggedIn = true;
        } else {
          this.loggedIn = false;
          this.signIn();
        }
      })
      .catch((err) => {
        this.loggedIn = false;
      });

    this.mgr.events.addUserSignedOut((e) => {
      this.signOut();
    });
  }

  getLoggedInState() {
    return this.loggedIn;
  }

  signIn() {
    this.mgr.signinRedirect().then(function () {
    }).catch(function (err) {
      console.log(err);
    });
  }

  signOut() {
    this.mgr.signoutRedirect().then(function () {
    }).catch(function (err) {
      console.log(err);
    });
    this.loggedIn = false;
    this.mgr.clearStaleState();
  };
}
robbaman commented 8 years ago

I'm now also running into this issue and I think I've figured out what's wrong here. The type definitions file should tell TypeScript what the non-TypeScript library looks like. Unfortunately as it stands now this does not seem to be the case.

The most notable difference is that in the actual oidc library the UserManager is supposed to be used as a class (and should thus be 'newed up'). As it stands now, the declaration file describes it as an interface, which makes it impossible to create a 'new' Oidc.UserManager.

For now I've solved it by simply changing the declarations of UserManager and OidcClient from interface to class like so:

class OidcClient {
    constructor (settings: OidcClientCtor);
    ... 
}

class UserManager extends OidcClient {
    constructor (settings: UserManagerCtor);
    ...
}

A few disclaimers:

I can create (another) PR for a changed oidc-client.d.ts file that does work for me if you want. Just know that while I have quite a bit of experience with TypeScript in general, this whole module loading thing is still fairly new to me.

brockallen commented 8 years ago

As I said:

Not sure -- I'm not a TS user.

And I don't have time right now to learn and make sure it's all correct. There are some other issues and PRs related. Maybe you can ping those folks to help and provide input.

brockallen commented 8 years ago

Can we close this now?