bon*_*low 5 javascript firebase firebase-authentication google-cloud-functions
我试图用displayName创建一个用户。如果我在创建后更新用户个人资料,它实际上可以工作。但是我还需要一个displayName的地方使用了一个云函数auth.onCreate。但是event.data没有给我displayName。我猜这是因为触发云功能时,配置文件没有更新。知道如何在我的云函数中访问displayName吗?
我尝试执行此操作的原因是因为我想确保displayNames是唯一的。因此,当人们注册时,他们必须留下用户名。如果它已经存在于我的数据库中,则必须另选一个。
如何使用JavaScript创建用户:
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)
})
})
}
)
Run Code Online (Sandbox Code Playgroud)
我的云功能:
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
})
});
Run Code Online (Sandbox Code Playgroud)
几周前我遇到了完全相同的问题。我相信在事件displayName期间没有办法使用(文档中的代码不起作用)。到目前为止我是这样做的。onCreate
创建一个函数来处理从客户端更新用户的访问令牌。
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));
}
Run Code Online (Sandbox Code Playgroud)
使用更新后的令牌创建 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
});
});
Run Code Online (Sandbox Code Playgroud)
创建一个云函数来处理将用户更新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
});
Run Code Online (Sandbox Code Playgroud)
这 100% 取决于您,可能有更好的方法来处理更新用户的displayName. 请注意,用户经常更改其显示名称和个人资料照片。每次用户更新其默认配置文件时,您都应该更新令牌并存储在本地存储中。
注意:如果令牌过期,Firebase 将刷新令牌并为您返回一个新令牌。
displayName如果您确实想在期间初始化用户onCreate event,则可以尝试如下所示。
exports.createUser = functions.auth.user().onCreate(event => {
let user = event.data;
const displayName = user.displayName || 'Anonymous';
...Update operation code goes below
});
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助你。
| 归档时间: |
|
| 查看次数: |
1591 次 |
| 最近记录: |