普通网友 2016-04-16 17:17
浏览 37

如何使用Angular2访问请求标头?

ON my golang backend after a success oauth2 request for facebook I redirect whe user to my app's dashboard like so:

w.Header().Set("Authorization", fmt.Sprintf("Bearer %s", tokenString))
http.Redirect(w, r, "http://" + r.Host + "/dashboard?jwt=" + tokenString, http.StatusFound)

Then on the dashboard initialization I do somenthing like:

params:RouteParams;


constructor(private _router:Router, private _jwt:JWTService, private _params:RouteParams, private location:Location) {
    this.params = _params;


}


consol() {
    var redirect_url = encodeURIComponent("http://localhost:9000/api/facebook/");
    var url = "https://www.facebook.com/dialog/oauth?client_id=xxxx&redirect_uri="+ redirect_url + "&response_type=code&scope=email+user_location+user_about_me"
    window.location.href=url;
}

ngOnInit() {
    this.token = '';
    console.log(this.params);
    if (this.params.params['jwt'] != null) {
        console.log(this.params);
        localStorage.setItem('jwt', this.params.params['jwt']);
        this.location.replaceState('/dashboard')
    }
    this.Bouncer();
}

I want to avoid making my url dirty, not even for a few seconds. I wish I could inspect the request headers, because I am sending the jwt through that as well.

Updated

The original request is done through a angular2-material button

 <div md-raised-button color="warn" (click)="consol()">Login to FACEBOOK</div>
  • 写回答

1条回答 默认 最新

  • dongyoudi1342 2016-04-17 07:16
    关注

    First I create a pop up window/tab.

        var url = "https://accounts.google.com/o/oauth2/auth?client_id="
                        + clientid + "&redirect_uri="
                        + redirect_url + "&response_type=code&scope="
                        + scope;
        window.open(url);
    

    This goes to google and hit my server on the way back,at the redirect url. Which serves the script tag below inside this popup. It actually just run a command on the windows that created this popup , in this case my SPA, with my application token as a parameter and then closes it.

    w.Write([]byte(`
                        <script>
                            var token="` + token + `";
                            function CloseMySelf() {
                                try {
                                    window.opener.angularComponentRef.runThisFunctionFromOutside(token);
                                }
                                catch (err) {}
                                window.close();
                                return false;
                            }
                            CloseMySelf();
                        </script>
    
        `))
    

    This is the function that it call. This method needs to be made public like this question shows.

    runThisFunctionFromOutside(token) {
        localStorage.setItem('jwt', token);
        location.href = "../dashboard";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集