Javascript Firebase Auth+;函数|使用displayName创建用户
我试图用displayName创建一个用户。如果我在创建后更新用户配置文件,它实际上是有效的。但我也在需要显示名的地方使用了一个云函数auth.onCreate。但是event.data没有给我显示名称。我猜这是因为当云函数被触发时,配置文件没有更新。你知道如何在我的云函数中访问displayName吗 我之所以尝试这样做,是因为我想确保显示名是唯一的。因此,当人们注册时,他们必须留下一个用户名。如果它已经存在于我的数据库中,他们就必须使用另一个 如何使用Javascript创建用户:Javascript Firebase Auth+;函数|使用displayName创建用户,javascript,firebase,firebase-authentication,google-cloud-functions,Javascript,Firebase,Firebase Authentication,Google Cloud Functions,我试图用displayName创建一个用户。如果我在创建后更新用户配置文件,它实际上是有效的。但我也在需要显示名的地方使用了一个云函数auth.onCreate。但是event.data没有给我显示名称。我猜这是因为当云函数被触发时,配置文件没有更新。你知道如何在我的云函数中访问displayName吗 我之所以尝试这样做,是因为我想确保显示名是唯一的。因此,当人们注册时,他们必须留下一个用户名。如果它已经存在于我的数据库中,他们就必须使用另一个 如何使用Javascript创建用户: fire
firebase.auth().createUserWithEmailAndPassword(this.email, this.password).then(
(user) => {
user.updateProfile({
displayName: username
}).then(() => {
user.sendEmailVerification().then(
() => {
firebase.auth().signOut()
this.complete = true
}).catch(function(err) {
console.log(err.message)
})
})
}
)
exports.createUser = functions.auth.user().onCreate(event => {
let user = event.data;
var userObject = {
displayName: user.displayName, // undefined
wins: 0
}
admin.database().ref('/users/' + user.uid).set(userObject).then(() => {
return true
})
});
我的云功能:
firebase.auth().createUserWithEmailAndPassword(this.email, this.password).then(
(user) => {
user.updateProfile({
displayName: username
}).then(() => {
user.sendEmailVerification().then(
() => {
firebase.auth().signOut()
this.complete = true
}).catch(function(err) {
console.log(err.message)
})
})
}
)
exports.createUser = functions.auth.user().onCreate(event => {
let user = event.data;
var userObject = {
displayName: user.displayName, // undefined
wins: 0
}
admin.database().ref('/users/' + user.uid).set(userObject).then(() => {
return true
})
});
几周前我也有同样的问题。我认为在
onCreate
事件期间无法使用displayName
(文档中的代码不起作用)。以下是我到目前为止的表现
创建一个函数来处理从客户端更新用户的访问令牌
updateUserProfile(name: string, photoURL: string) {
const data = {
displayName: name,
photoURL: photoURL
};
return this.afAuth.auth.currentUser.updateProfile(data)
.then(() => {
console.log('Successfully updated default user profile');
// IMPORTANT: Force refresh regardless of token expiration
return this.afAuth.auth.currentUser.getIdToken(true);
})
.then(newToken => {
console.log('Token refreshed!', newToken);
return newToken;
})
.catch((err) => console.log(err));
}
使用更新的令牌生成HTTP触发器
const data = {
displayName: this.displayName,
photoURL: this.photoURL,
};
this.userService.updateUserProfile(this.displayName, this.photoURL).then(accessToken => {
// Better: Store token in local storage
const url = 'https://cloud function endpoint';
this.http.post(url, JSON.stringify(data), {
headers: {'Authorization': accessToken, 'Content-Type': 'application/json; charset=utf-8'}
}).subscribe((res) => {
// Went well
}, (err) => {
// Went wrong
});
});
创建一个云函数,用于将用户的displayName
更新到服务器
请查看Firebase提供的
const app = express();
app.use(cors({ origin: true }));
app.use((req, res, next) => {
if (!req.headers.authorization) return res.status(403).json({ message: 'Missing Authorization Header' });
... handle JWT
});
app.post('/', (req, res) => {
const batch = admin.firestore().batch();
const data = req.body;
const userState = {
displayName: data.displayName,
photoURL: data.photoURL,
};
... commit batch update
});
这完全取决于您,可能有更好的方法来处理更新用户的displayName
。
请注意,用户经常更改其显示名称和个人资料照片。每次用户更新其默认配置文件时,您都应该更新令牌并将其存储在本地存储中
注意:如果令牌过期,Firebase将刷新令牌并为您返回一个新令牌
如果您确实想在onCreate事件期间初始化用户的displayName
,则可以尝试以下操作
exports.createUser = functions.auth.user().onCreate(event => {
let user = event.data;
const displayName = user.displayName || 'Anonymous';
...Update operation code goes below
});
我希望这对您有所帮助。您可以通过将所有逻辑移到云函数来简化此解决方案,然后使用firebase admin
API更新用户名或任何其他身份验证数据admin.auth().updateUser(uid,data)
,还可以执行任何数据库更新。比如说,嘿,杰夫。user.displayName
属性的值将在创建帐户时传递给云函数。由于对于电子邮件+密码帐户,displayName
是在第二个API调用中设置的,因此该值在云函数触发器中不可用。您使用辅助调用的方法确实是一个很好的解决方法。