仅在--prod build

时间:2018-05-04 13:37:03

标签: angular okta

我们在Angular 5应用程序中使用okta-angular(v 1.0.1)和okta-signin-widget(v 2.7.0)。

我们在environments/environment.tsenvironments/environment.prod.ts文件中有一些配置,基本上是这样,但每个env的值都不同:

oidc: {
    clientId: '{Okta_ClientId}',
    issuer: '{Okta_Issuer}',
    redirectUri: '{Okta_RedirectUri',
    scope: 'openid profile email'
},

以上是来自文件的prod版本,而值被Octopus取代,非prod版本具有相同的密钥,但有一些硬编码值。

在我们引入的app.module.ts文件中,创建配置:

import { OktaAuthModule, OktaAuthService } from '@okta/okta-angular';
import { environment } from '../environments/environment';

const oktaConfig = Object.assign({
  onAuthRequired: ({oktaAuth, router}) => {
  // Redirect the user to your custom login page
    router.navigate(['/login']);
  }
}, environment.oidc);

...

imports: [
  ...
  OktaAuthModule.initAuth(oktaConfig),
],
providers: [
  ...
  OktaAuthService,
],

我们已将authguard和callback组件添加到我们的app.routes.ts文件中:

import { OktaAuthGuard, OktaCallbackComponent } from '@okta/okta-angular';
...

export const appRoutes: Routes = [
  {
    path: 'implicit/callback',
    component: OktaCallbackComponent,
  },
  {
    path: 'login',
    component: LoginComponent
  },
  {
    path: 'dashboard',
    component: DashboardComponent,
    canActivate: [ OktaAuthGuard ]
  },
  ...

我们添加了一个登录组件,我们在其中创建了一个okta signin:

...
import { OktaAuthService } from '@okta/okta-angular';
import * as OktaSignIn from '@okta/okta-signin-widget';

import { environment } from '../../environments/environment';

export class LoginComponent implements OnInit {
  public signIn: OktaSignIn;

  constructor(
    public oktaAuth: OktaAuthService,
    private router: Router
  ) {}

  async ngOnInit(): Promise<void> {
    this.signIn = new OktaSignIn({
      baseUrl: environment.oidc.issuer.split('/oauth2')[0],
      clientId: environment.oidc.clientId,
      redirectUri: environment.oidc.redirectUri,
      i18n: {
        en: {
          'primaryauth.title': 'Please log in',
        },
     },
     authParams: {
       responseType: ['id_token', 'token'],
       issuer: environment.oidc.issuer,
       display: 'page',
       scopes: environment.oidc.scope.split(' '),
    },
  });

  const isAuthenticated = await this.oktaAuth.isAuthenticated();

  if (isAuthenticated) {
    this.router.navigate(['dashboard']);
  } else {
    this.signIn.renderEl(
      { el: '#sign-in-widget' },
      () => {
        // the success handler will not be called because we redirect to the Okta org for authentication
      },
      (err) => {
        throw err;
      },
    );
  }
}

}

我们添加了user.service.ts来封装一些okta内容,例如检查用户是否经过身份验证以及存储/检索访问令牌:

import { Injectable } from '@angular/core';
import { OktaAuthService } from '@okta/okta-angular';

@Injectable()
export class UserService {
  public isAuthenticated: boolean;
  private _accessToken: string;

  constructor(private oktaAuth: OktaAuthService) {}

  async initAuth(): Promise<void> {
    this.isAuthenticated = await this.oktaAuth.isAuthenticated();
    this._accessToken = await this.oktaAuth.getAccessToken();
  }

  get accessToken(): string {
    if (this.isAuthenticated) {
      return this._accessToken;
    }

    return '';
  }
}

最后,我们更新了header.component.ts文件,以在应用的标题中显示已登录用户的电子邮件和退出按钮:

...
import { OktaAuthService } from '@okta/okta-angular';

import { UserService } from '../_services/user.service';

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.scss']
})
export class HeaderComponent implements OnInit {
  public userName: string;

  constructor(
    public oktaAuth: OktaAuthService,
    public userService: UserService,
    public router: Router
  ) {}

  async ngOnInit(): Promise<void> {

    this.userService.initAuth().then(() => {
    this.setUserName();
  });
}

private async setUserName(): Promise<void> {
  if (this.userService.isAuthenticated) {
    const userClaims = await this.oktaAuth.getUser();
    this.userName = userClaims.name;
  }
}

logout(): void {
  this.oktaAuth.logout('/');
}

}

这些是我们目前使用Okta的所有地方,而且它仍然在进行中。

问题是,当使用常规ng serve命令生成的普通开发版本在本地运行时,这看起来效果很好,但是在运行ng build --prod时它会失败,在这种情况下应用程序不会甚至引导程序,我们在浏览器中看不到任何内容,在控制台中我们看到:

  

错误类型错误:无法读取属性&#39;发行者&#39;未定义的

使用为prod版本启用的源映射进行调试后,此错误来自okta.service.j内的node_modules s文件。这个服务的构造函数需要一个auth参数,这就是代码试图从中获取issuer属性的内容,但这是Angular在初始化时应该传递给服务的东西。内部准备好了DI。

真的不知道甚至尝试什么,因为它在开发过程中没有任何问题。

1 个答案:

答案 0 :(得分:1)

问题可能在于您的授权服务器。可以在预览(dev)org中免费访问API Access管理。但你必须为生产组织中的功能付费。

检查:

转到您的制作组织。 Okta仪表板&gt;安全&gt; API&gt;检查您是否看到&#34;授权服务器&#34;选项卡以及#34; Tokens&#34;和&#34; Trusted Origins&#34;翼片

如果您没有&#34;授权服务器&#34;上市。将颁发者URL更改为您的Okta URL。所以issuer = https:// {org name} .okta.com