helloxielan 2016-06-09 18:41 采纳率: 0%
浏览 30

Polymer.js + OAuth2

Is there an example out there which describes how to implement an OAuth2 authentication in the frontend based on Polymer.js?

I just found some examples that describe the procedure for AJAX, which is also part of the Polymer element set. So how to go on ahead?

  • 写回答

1条回答 默认 最新

  • ??yy 2016-08-15 07:33
    关注

    I found a git repo here which have demo. So you can take a look.

      <oauth-user></oauth-user>
    
      <polymer-element name="oauth-user">
        <template>
            <oauth-authenticator id="authenticator" client_id="282331888208-06mufq54k942624lv803nlm6kvlq76fr.apps.googleusercontent.com" scopes="https://www.googleapis.com/auth/userinfo.profile" on-authenticated="{{authenticated}}"></oauth-authenticator>
            <core-ajax auto id="ajax" url="{{url}}" headers="{{headers}}" handleAs="json" on-core-response="{{ajaxResponse}}"></core-ajax>
            <p>{{greeting}}</p>
        </template>
        <script>
            Polymer({
                url: '',
                greeting: '...',
    
                authenticated: function(event){
                  var token = event.detail.token;
                  this.headers = {'Authorization': 'Bearer ' + token};
                  this.url = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json";
                },
    
                ajaxResponse: function(event, response) {
                  this.greeting = 'Hello ' + response.response.name + '!';
                }
            });
        </script>
      </polymer-element>
    

    You can see there're event handler so you can redirect user or update screen. There're also a url to get user info.

    评论

报告相同问题?