【问题标题】:Auth0 SSO Angular returns WebpackOkAuth0 SSO Angular 返回 WebpackOk
【发布时间】:2017-12-21 21:42:40
【问题描述】:

我正在开发一个使用 Auth0 来处理用户的 Angular 应用程序。我期待一种同时使用两个客户端的方法。如果我有两个 JWT 没关系。我在 Auth0 中看到了有关单点登录 (SSO) 的信息。我已经实现了它,但它不起作用。

@Injectable()
export class AuthService {
    auth0js = new auth0.WebAuth({
        domain: 'domain.com',
        clientID: 'clientID',
        scope: 'openid name picture groups permissions roles',
        responseType: 'token id_token',
        redirectUri: 'http://localhost:4200'
    });

    auth0js2 = new auth0.WebAuth({
        domain: 'domain2.com',
        clientID: 'clientID2',
        scope: 'openid name picture groups permissions roles',
        responseType: 'token id_token',
        redirectUri: 'http://localhost:4200'
    });

    profile: any;
    profile$ = new BehaviorSubject<any>(this.profile);

    constructor(
        private notificationService: NotificationService,
        private router: Router,
        private _ngZone: NgZone){
    }

    public login(): void {
        this.auth0js.authorize();
    }

    public handleAuthentication(): void {
        const self = this;
        self.auth0js.parseHash({ _idTokenVerification: false }, (err, authResult) => {
            if (authResult && authResult.accessToken && authResult.idToken) {
                window.location.hash = '';
                self.setSession(authResult);
                self._ngZone.runOutsideAngular(() => {
                    self.renew();
                });
            } else if (err) {
                self.router.navigate(['/home']);
                console.log(err);
            }
        });
    }

    renew() {
        const self = this;
        self.auth0js2.renewAuth({
        audience: 'domain.com/api/v2/',
        scope: 'openid name picture groups permissions roles',
        redirectUri: 'localhost:4200',
        usePostMessage: true}, function (err, authResult) {
            if (err) {
                alert(`Could not get a new token using silent authentication (${err.error}). Redirecting to login page...`);
                self.auth0js2.authorize();
            } else {
                console.log('AUTH', authResult);
                self.setSession2(authResult);
            }
        });
    }

    private setSession(authResult): void {
        const expiresAt = JSON.stringify((authResult.expiresIn * 1000) + new Date().getTime());
        localStorage.setItem('access_token', authResult.accessToken);
        localStorage.setItem('id_token', authResult.idToken);
        localStorage.setItem('expires_at', expiresAt);
    }

    private setSession2(authResult): void {
        // Here I've got {type: "webpackOk", data: undefined} as authResult
        // Set the time that the access token will expire at
        const expiresAt = JSON.stringify((authResult.expiresIn * 1000) + new Date().getTime());
        localStorage.setItem('access_token2', authResult.accessToken);
        localStorage.setItem('id_token2', authResult.idToken);
        localStorage.setItem('expires_at2', expiresAt);
        }
    }

当我进行静默身份验证时,我会收到以下响应:

{type: "webpackOk": data: undefined}

【问题讨论】:

  • 你解决了吗?

标签: angular oauth-2.0 single-sign-on auth0


【解决方案1】:

它是这样工作的

   setTimeout(function () {
          iframe.contentWindow.postMessage(data, '*')
        }, 1000)

【讨论】:

  • 但是为什么当我们这样做时它会起作用?这有助于我解决我的问题。添加更多详细信息
猜你喜欢
  • 2016-08-04
  • 2018-07-21
  • 2021-07-13
  • 2022-01-09
  • 2015-08-14
  • 2019-11-14
  • 2018-03-01
  • 2018-02-11
  • 2020-09-24
相关资源
最近更新 更多