Authentication 尝试注销时无法识别Auth2对象(Angular2)

Authentication 尝试注销时无法识别Auth2对象(Angular2),authentication,angular,typescript,oauth,Authentication,Angular,Typescript,Oauth,你好 我正在尝试注销auth2客户端。在我升级路由器以适应新的RC要求之前,这个过程运行良好。现在,auth2对象似乎在从登录到注销的过程中被清除或丢失 这是我的注销标签: <a role="button" (click)="signOut()" style="padding-left: 30px;">Log out</a> 以下是它正在调用的导航服务代码: import { Injectable } from '@angular/core'; @Injectabl

你好

我正在尝试注销auth2客户端。在我升级路由器以适应新的RC要求之前,这个过程运行良好。现在,auth2对象似乎在从登录到注销的过程中被清除或丢失

这是我的注销标签:

 <a role="button" (click)="signOut()" style="padding-left: 30px;">Log out</a>
以下是它正在调用的导航服务代码:

import { Injectable } from '@angular/core';

@Injectable()
export class NavigationService {
onEditMode:boolean;
auth2:any;
constructor() {
    this.onEditMode=true;
}

getEditMode(){
    return this.onEditMode;
}

setEditMode(editMode:boolean){
    this.onEditMode=editMode;
}

setAuth2(auth2:any){       
    this.auth2=auth2;

}

getAuth2(){
    return this.auth2;

}

}
下面是我的login.component.ts,它设置在navigationService.ts中看到的auth2对象:

onGoogleLoginSuccess = (loggedInUser) => {

    this.isLoading=true;
    console.log(loggedInUser)
    this._navigationService.setAuth2(gapi.auth2.getAuthInstance());

    console.log("Google gapi" + gapi.auth2.getAuthInstance());
    sessionStorage.setItem('gapi',gapi.auth2.getAuthInstance());

    this._zone.run(() => {

        this.userAuthToken = loggedInUser.hg.access_token;
        this.userDisplayName = loggedInUser.getBasicProfile().getName();
        var strClientID = document.getElementsByTagName('meta')['google-signin-client_id'].getAttribute('content')
        this.objTrimbleAuthentication.ClientID = document.getElementsByTagName('meta')['google-signin-client_id'].getAttribute('content');
        this.objTrimbleAuthentication.IDToken = loggedInUser.getAuthResponse().id_token;

        this._trimbleAuthenticationService.sendAndVerify(this.objTrimbleAuthentication).subscribe(data=>{
            if(data.tokenIsValid==true){

                sessionStorage.setItem('S_USER_EMAIL',loggedInUser.getBasicProfile().getEmail());
                sessionStorage.setItem('S_USER_NAME',loggedInUser.getBasicProfile().getName());
                sessionStorage.setItem('S_ID_TOKEN',this.userAuthToken);

                this.objExternalBindingModel.ExternalAccessToken=this.userAuthToken;
                this.objExternalBindingModel.Provider="Google";
                this.objExternalBindingModel.UserName = loggedInUser.getBasicProfile().getName();

                this._LoginService.obtainLocalAccessToken(this.objExternalBindingModel).subscribe(data=>{

                    // console.log(data);
                    this.isLoading=false;
                    this._router.navigate(['/Home']);
                    sessionStorage.setItem("access_token",data.access_token);

                },error=>{
                    console.log(error);
                })

            }else{
                this.isLoading= false;
                this.showModal('#trimbleAuthError');

            }
        }, error=>{

        })
    });
}
从login.component.html调用OnGoogleLoginsAccess:

 <div style="margin-left:8% !important" id="{{googleLoginButtonId}}"></div>
如果您需要任何其他信息或组件,请询问我希望我提供了足够的信息。正如我所说的,它正在发挥作用,所以请记住这一点

更新 正在等待其他信息


在下面的代码中,
auth2:any
未声明的
setAuth2
是否在
signOut()之前的任何位置调用


基于发布的有限信息和代码,我猜测注销过程中存在逻辑缺陷

signOut()
中,
窗口.location.reload()
在当前url处重新加载页面,同时清除所有变量/对象。但是,重新加载后,您的应用程序会正确尝试再次注销(由于url?)

navbar.component
中,您可能需要在
ngInit()
中添加更多逻辑来处理这种情况

或者,如果没有
window.location.reload()
,您的代码是否可以工作?在angular2中使用该选项似乎有些奇怪,尤其是在路由方面。

Update 正在等待其他信息


在下面的代码中,
auth2:any
未声明的
setAuth2
是否在
signOut()之前的任何位置调用


基于发布的有限信息和代码,我猜测注销过程中存在逻辑缺陷

signOut()
中,
窗口.location.reload()
在当前url处重新加载页面,同时清除所有变量/对象。但是,重新加载后,您的应用程序会正确尝试再次注销(由于url?)

navbar.component
中,您可能需要在
ngInit()
中添加更多逻辑来处理这种情况


或者,如果没有
window.location.reload()
,您的代码是否可以工作?在angular2中使用这一点似乎很奇怪,尤其是在路由方面。

对,我发现上述问题的解决方案是使用localhost注销将不起作用。所以我只是在部署网站时使用了这段代码,并在localhost上运行网站时保留注释

这是在navbar.component.ts中找到的my signOut()函数:

signOut() {

    //////////////////////////////////////// Uncomment block for live deployment //////////////////////////////


    // var auth2 = gapi.auth2.getAuthInstance();
    // auth2.signOut().then(function () {
    //     console.log('User signed out.');
    // });


    //////////////////////////////////////////////////////////////////////////////////////////////////////////

    sessionStorage.clear();
    localStorage.clear();
    this.router.navigate(['/']);
    window.location.reload();

}

尽管getAuthInstance在尝试在localhost中运行时出错,但将web应用程序部署到服务器似乎可以正常工作。

对,我发现上述问题的解决方案是使用localhost注销将无法工作。所以我只是在部署网站时使用了这段代码,并在localhost上运行网站时保留注释

这是在navbar.component.ts中找到的my signOut()函数:

signOut() {

    //////////////////////////////////////// Uncomment block for live deployment //////////////////////////////


    // var auth2 = gapi.auth2.getAuthInstance();
    // auth2.signOut().then(function () {
    //     console.log('User signed out.');
    // });


    //////////////////////////////////////////////////////////////////////////////////////////////////////////

    sessionStorage.clear();
    localStorage.clear();
    this.router.navigate(['/']);
    window.location.reload();

}

尽管getAuthInstance在尝试在localhost中运行时出现错误,但将web应用程序部署到服务器似乎工作正常。

您好,感谢您的响应,我取出了window.location.reload(),代码仍然可以工作。但问题发生在执行该行之前。错误发生在这里:var auth2=this.\u navigationService.getAuth2();你所说的有限信息是什么意思?您需要我添加什么,我会这样做。@AndreSmith是否可以将您的代码上载到github?@PaulR我的原始答案无法解决问题,因此我删除了内容。感谢您的回复,我取出了window.location.reload(),代码仍然可以工作。但问题发生在执行该行之前。错误发生在这里:var auth2=this.\u navigationService.getAuth2();你所说的有限信息是什么意思?您需要我添加什么,我会添加。@AndreSmith是否可以将您的代码上载到github?@PaulR我的原始答案无法解决问题,因此我删除了同一类中的内容是
OnGoogleLoginsAccess()
signOut()
?如果没有,请发布两个类的
@Component({})
部分。还需要
module.ts
文件。
OnGoogleLoginsAccess()
signOut()
是否在同一个类中?如果没有,请发布两个类的
@Component({})
部分。还需要
module.ts
文件。
import { Injectable } from '@angular/core';

@Injectable()
export class NavigationService {

    onEditMode:boolean;
    auth2:any;
    constructor() {
        this.onEditMode=true;
    }

    getEditMode(){
        return this.onEditMode;
    }

    setEditMode(editMode:boolean){
        this.onEditMode=editMode;
    }

    setAuth2(auth2:any){       
        this.auth2=auth2;

    }

    getAuth2(){
        return this.auth2;

    }

}
signOut() {

    //////////////////////////////////////// Uncomment block for live deployment //////////////////////////////


    // var auth2 = gapi.auth2.getAuthInstance();
    // auth2.signOut().then(function () {
    //     console.log('User signed out.');
    // });


    //////////////////////////////////////////////////////////////////////////////////////////////////////////

    sessionStorage.clear();
    localStorage.clear();
    this.router.navigate(['/']);
    window.location.reload();

}