Flutter:使用谷歌身份验证在Firebase存储中实现任何身份验证失败



我的应用程序在flutter中使用此代码上传图像:

final StorageReference imageRef = FirebaseStorage.instance.ref().child('postimages');
final StorageUploadTask uploadTask = imageRef.child('/' + currentUser.id + '/post_$postid.jpg').putFile(_image);
var upurl = await (await uploadTask.onComplete).ref.getDownloadURL();
url = upurl.toString();
debugPrint('url:$url');

如果我通过以下未注释的规则授予完全重写访问权限,则工作正常:

rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if true;
}
}
}

但如果我试图实现任何安全性,例如使用以下规则:

rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {      
allow read, write: if request.auth != null;
}
}
}

我最终得到以下异常:[VERBOSE-2:ui_dart_state.cc(177(]未处理的异常:PlatformException(错误-13021,FIRStorageErrorDomain,用户无权访问gs://picturegram.appspot.com/postimages/118190432651181005229/post_b050df01-7b58-4016-8d3c-28c7f3ace630.jpg,null(

该应用程序使用firebase身份验证,谷歌作为登录提供商。读取文档时,身份验证信息似乎总是自动发送。签名似乎非常有效,因为我得到的是具有userid的当前用户。那么为什么请求会失败呢

感谢Gabe的评论,我发现代码确实有效,但我的身份验证不完整,因为我没有将登录信息传递给firebase。为了将来参考,这是使其工作所需的完整登录代码:

final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();
// Obtain the auth details from the request
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
// Create a new credential
final GoogleAuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
// Once signed in, return the UserCredential
UserCredential creds = await  FirebaseAuth.instance.signInWithCredential(credential);
debugPrint('creds:$creds');

最新更新