我正在使用Firebase Polymer Component让用户使用Google Oauth登录。我似乎无法找到获取电子邮件地址的方法。我似乎无法向范围添加电子邮件。
在Github中似乎存在一个与此相关的元素的问题。
https://github.com/GoogleWebComponents/firebase-element/issues/39
我使用了firebase-auth聚合物元素并添加了电子邮件范围。我还创建了一个名为hi9-login的元素。
https://github.com/HackITtoday/hi9-login
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../firebase-element/firebase-auth.html">
<link rel="import" href="../firebase-element/firebase-document.html">
<link rel="import" href="../paper-button/paper-button.html">
<dom-module id="hi9-login">
<template>
<firebase-auth id="firebaseLogin" user="{{user}}" status-known="{{statusKnown}}" location="https://hi9site.firebaseio.com" provider="google" on-error="errorHandler" on-user-created="userSuccessHandler" on-password-changed="userSuccessHandler" on-password-reset="userSuccessHandler" on-user-removed="userSuccessHandler"></firebase-auth>
<!-- <firebase-document location="https://hi9site.firebaseio.com/data" id="firebaseDocument"></firebase-document> -->
<firebase-document location="{{userDataUrl}}" data="{{userData}}" id="firebaseUser"></firebase-document>
<firebase-document location="{{ownerDataUrl}}" data="{{ownerData}}" id="firebaseOwner"></firebase-document>
<paper-button on-tap="logout" hidden$="{{computeLogoutHidden(statusKnown, user)}}"> Logout </paper-button>
<paper-button on-tap="login" hidden$="{{computeLoginHidden(statusKnown, user)}}"> Login </paper-button>
</template>
</dom-module>
<script>
var that = {};
Polymer({
is: 'hi9-login',
properties: {
params: {
scope: "email"
},
provider: {
type: String, value: 'anonymous'
},
message: {
type: String, value: ''
},
email: {
type: String, value: ''
},
password: {
type: String, value: ''
},
user: {
type: Object, value: null,
notify: true
},
uid: {
computed:'returnVal(user.uid)'
},
owner: {
computed:'returnVal(user.owner)'
},
userDataUrl:{
computed:'getUserDataUrl(uid)'
},
ownerDataUrl:{
computed:'getOwnerDataUrl(owner, uid)'
},
statusKnown: {
type: Boolean
},
show_model: {
type: Boolean, notify: true,
computed: "computeLogoutHidden(statusKnown, user)"
},
role: {
computed: 'getRole(user,userData)',
notify: true
},
admin: {
computed: 'isAdmin(role)',
notify: true
}
},
returnVal: function(val) {
if (val !== undefined && val !== null) {
return val;
} else {
return undefined;
}
},
getOwnerDataUrl: function(owner,uid) {
return "https://hi9site.firebaseio.com/owner/" + owner + "/users/" + uid;
},
getUserDataUrl: function(uid) {
that = this;
setTimeout(function(){ that.whenReady(); }, 7000);
return "https://hi9site.firebaseio.com/users/" + uid;
},
login: function() {
var params;
try {
params = JSON.parse(this.params);
} catch (e) {
params = null;
}
if (this.provider == 'password') {
params = params || {};
params.email = this.email;
params.password = this.password;
}
this.$.firebaseLogin.login(params);
this.log("Login");
},
logout: function() {
this.log("Logout");
this.$.firebaseLogin.logout();
},
errorHandler: function(e) {
this.log("Login Status");
this.message = 'Error: ' + e.detail.message;
},
userSuccessHandler: function(e) {
this.log("Login Status");
this.message = e.type + ' success!';
},
createUserHandler: function(e) {
this.log("createUserHandler");
this.$.firebaseLogin.createUser(this.email, this.password);
},
changePasswordHandler: function(e) {
this.log("changePasswordHandler");
this.$.firebaseLogin.changePassword(this.email, this.password, this.newPassword);
},
resetPasswordHandler: function(e) {
this.log("resetPasswordHandler");
this.$.firebaseLogin.sendPasswordResetEmail(this.email);
},
computePasswordHidden: function(provider) {
this.log("Login Status");
return provider !== 'password';
},
computeCreateUserDisabled: function(email, password) {
this.log("computeCreateUserDisabled");
return !email || !password;
},
computeChangePasswordDisabled: function(email, password, newPassword) {
this.log("computeChangePasswordDisabled");
return !email || !password || !newPassword;
},
computeResetPasswordDisabled: function(email, password) {
this.log("computeResetPasswordDisabled");
return !email || !password;
},
computeRemoveUserDisabled: function(email, password) {
this.log("computeRemoveUserDisabled");
return !email || !password;
},
computeLoginHidden: function(statusKnown, user) {
this.log("computeLoginHidden");
return !statusKnown || !!user;
},
computeLogoutHidden: function(statusKnown, user) {
this.log("computeLogoutHidden");
return !statusKnown || !user;
},
computeLoginStatus: function(statusKnown, user) {
var d = new Date();
var n = d.getTime();
this.log("Login Status");
if (statusKnown && user) {
return 'Logged in';
}
if (statusKnown) {
return 'Logged out';
}
return 'Unknown (checking status...)';
},
log: function(log) {
// var d = new Date();
// var n = d.getTime();
// this.$.firebaseDocument.query.ref().push({log: log,time:n,user:this.user});
},
getRole: function(user, userData) {
if (userData !== undefined && userData !== null && user !== null) {
if (this.userSet === undefined && userData.owner === undefined) { // Stops Looping.
this.userSet = true;
if (userData.num === undefined ) {
userData.num = prompt("Hi "+user.google.displayName+"\nPlease tell me your mobile phone number?", "07");
}
if (userData.owner === undefined) {
userData.owner = prompt("What is your housing association?", "Yarlington");
}
if (userData.email === undefined) {
userData.email = prompt("and lastly you email address?", "");
}
if (userData.log === undefined) {
var d = new Date();
var n = d.getTime();
userData.log = [{first: n}]
}
userData.user = user;
this.userData = clone(userData);
this.ownerData = clone(userData);
}
// user
if (!userData.hasOwnProperty("role")) {
userData.role = "User"
}
return userData.role;
} else {
return 'no data'
}
},
isAdmin: function(role) {
return role = 'admin';
},
whenReady: function() {
var userDataTest = clone(this.$.firebaseUser.data);
delete(this.$.firebaseUser.data);
if (userDataTest === undefined || userDataTest === null){
userDataTest = {placeholder:true}
};
this.$.firebaseUser.data = userDataTest;
var ownerDataTest = clone(this.$.firebaseOwner.data);
delete(this.$.firebaseOwner.data);
if (ownerDataTest === undefined || ownerDataTest === null){
ownerDataTest = {placeholder:true}
};
if (this.ownerDataUrl) {
this.$.firebaseOwner.data = ownerDataTest;
}
}
});
function clone(obj) {
var copy;
// Handle the 3 simple types, and null or undefined
if (null == obj || "object" != typeof obj) return obj;
// Handle Date
if (obj instanceof Date) {
copy = new Date();
copy.setTime(obj.getTime());
return copy;
}
// Handle Array
if (obj instanceof Array) {
copy = [];
for (var i = 0, len = obj.length; i < len; i++) {
copy[i] = clone(obj[i]);
}
return copy;
}
// Handle Object
if (obj instanceof Object) {
copy = {};
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
}
return copy;
}
throw new Error("Unable to copy obj! Its type isn't supported.");
}
</script>
这里也是JSFiddle的屏幕截图,显示它无法正常工作
答案 0 :(得分:3)
您可以向authWithOAuthRedirect API添加范围。
例如,
ref.authWithOAuthRedirect('google', errorHandler, {
scope: "email"
});
然后,在onAuth处理程序中,您可以获得&#34; authData.google.email&#34;属性:
ref.onAuth(function(authData) {
if (authData) {
console.log("Authenticated with email: ", authData.google.email);
} else {
console.log("Client unauthenticated.")
}
});