Auth0 SSO Angular返回WebpackOk

时间:2017-07-17 11:14:17

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

我正在开发使用Auth0来处理用户的Angular App。我期待着同时使用两个客户端的方法。如果我有两个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}

1 个答案:

答案 0 :(得分:0)

它像这样工作

NAs