Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(363)

Side by Side Diff: appengine/swarming/elements/res/imp/common/auth-signin.html

Issue 2408743002: Move elements/ to ui/ (Closed)
Patch Set: rebase again Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 <!--
2 Copyright 2016 The LUCI Authors. All rights reserved.
3 Use of this source code is governed under the Apache License, Version 2.0
4 that can be found in the LICENSE file.
5
6 The `auth-signin` element displays sign-in/sign-out button, user email and
7 avatar.
8 It has a google-signin/google-signin-aware element under the hood that handles
9 the actual OAuth logic.
10
11 Usage:
12
13 <auth-signin></auth-signin>
14
15 Properties:
16 auth_headers: Object, Use this as an argument to sk.request to set oauth2 he aders.
17 auth_response: Object, The raw gapi.auth2.AuthResponse object.
18 client_id: String, The client id to authenticate
19 profile: Object, Read Only, The email address and imageurl of the logged in user.
20 signed_in: Boolean, Read Only, if the user is logged in.
21
22 Methods:
23 signIn(): Signs the user in by popping up the authorization dialog.
24 signOut(): Signs the user out.
25
26 Events:
27 auth-signin: Fired when the oauth handshake has completed and a user has log ged in.
28 -->
29
30 <link rel="import" href="/res/imp/bower_components/google-signin/google-signin-a ware.html">
31 <link rel="import" href="/res/imp/bower_components/polymer/polymer.html">
32
33
34 <dom-module id="auth-signin">
35 <template>
36 <style>
37 #avatar {
38 border-radius: 5px;
39 }
40 a {
41 color: white;
42 }
43 .center {
44 vertical-align: middle;
45 }
46 </style>
47
48 <google-signin-aware id="aware"
49 client-id="[[client_id]]"
50 offline
51 scopes="email"
52 on-google-signin-aware-success="_onSignin"
53 on-google-signin-aware-signed-out="_onSignout">
54 </google-signin-aware>
55
56 <template is="dom-if" if="[[!signed_in]]">
57 <div id="signinContainer">
58 <a on-tap="signIn" href="#">Sign in</a>
59 </div>
60 </template>
61
62 <template is="dom-if" if="[[signed_in]]">
63 <img class="center" id="avatar" src="[[profile.imageUrl]]" width="30" heig ht="30">
64 <span class="center" >[[profile.email]]</span>
65 <span class="center" >|</span>
66 <a class="center" on-tap="signOut" href="#">Sign out</a>
67 </template>
68 </template>
69 <script>
70 'use strict';
71 Polymer({
72 is: 'auth-signin',
73 properties: {
74 auth_headers: {
75 type: Object,
76 computed: "_makeHeader(auth_response)",
77 notify: true,
78 },
79 auth_response: {
80 type: Object,
81 notify: true,
82 },
83 client_id: {
84 type: String,
85 },
86 profile: {
87 type: Object,
88 readOnly: true,
89 notify: true,
90 },
91 signed_in: {
92 type: Boolean,
93 readOnly: true,
94 value: false,
95 notify: true,
96 },
97 },
98
99 ready: function() {
100 if (!this.client_id) {
101 return;
102 }
103 // If a page is opened in a new tab, we are (likely) already logged in
104 // so we wait for the gapi and auth2 to be loaded and re-extract our
105 // access_token.
106 window.setTimeout(function(){
107 // The 'gapi' checks are the same that signin-aware does. We do them
108 // to avoid extraneous errors in the console.
109 if (!this.signed_in && !this._signingIn){
110 if (('gapi' in window) && ('auth2' in window.gapi)) {
111 var user = gapi.auth2.getAuthInstance().currentUser.get();
112 if (user && user.getAuthResponse().access_token) {
113 // User is already logged in, can use the access_token.
114 this._onSignin();
115 } else {
116 window.setTimeout(this.ready.bind(this), 50);
117 }
118 } else {
119 window.setTimeout(this.ready.bind(this), 50);
120 }
121 }
122 }.bind(this), 50);
123 },
124
125 _onSignin: function() {
126 this._signingIn = true;
127 var user = gapi.auth2.getAuthInstance().currentUser.get();
128 var profile = user.getBasicProfile();
129 this._setProfile({
130 email: profile.getEmail(),
131 imageUrl: profile.getImageUrl()
132 });
133 this.set("auth_response", user.getAuthResponse());
134 this._setSigned_in(true);
135 this.fire("auth-signin");
136 // The credential will expire after a while (usually an hour)
137 // so we need to reload it.
138 this.async(function(){
139 console.log("reloading credentials");
140 user.reloadAuthResponse();
141 this._onSignin();
142 }, this.auth_response.expires_in * 1000); // convert seconds to ms
143 this._signingIn = false;
144 },
145
146 _onSignout: function(e) {
147 this._setSigned_in(false);
148 this._setProfile(null);
149 },
150
151 _makeHeader: function(auth_response) {
152 if (!auth_response) {
153 return {};
154 }
155 return {
156 "authorization": auth_response.token_type + " " + auth_response.access _token
157 };
158 },
159
160 signIn: function() {
161 this.$.aware.signIn();
162 },
163
164 signOut: function() {
165 this.$.aware.signOut();
166 }
167 });
168 </script>
169 </dom-module>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698