我正在尝试为我的 Firebase 应用程序提供 Google 登录。遵循https://www.firebase.com/docs/security/simple-login-overview.html
似乎在成功登录后,可以获得一个用户,因此它可以例如存储在 Angular 范围中 - 例如 $scope.loggedInUser。(根据您的实现,它不一定是 Angular)
我的问题是,Firebase 返回的具有大量身份验证令牌的用户是否存在安全风险?代码是用 Javascript 编写的,黑客应该能够通过在浏览器中嵌入一些代码来劫持和窃取用户。
引起我关注的位是:accessToken、firebaseAuthToken
如果这是一种风险,我们如何确保它?
请参考以下代码进行身份验证和用户数据:
这是身份验证的代码:
authModule.controller( 'AuthController', [
'$scope',
'$firebase',
function ( $scope, $firebase ) {
var ref = new Firebase( 'https://test123.firebaseio.com' );
var auth = new FirebaseSimpleLogin( ref, function ( error, user ) {
if ( user ) {
$scope.loggedInUser = user; // user has authenticated, this user contains security information
}
} );
$scope.login = function () {
auth.login( "google", {
scope: …Run Code Online (Sandbox Code Playgroud) angularjs firebase firebase-security angularfire firebasesimplelogin
请参阅下面的更新
我有一个角度应用程序,在该控制器的页面上有一个控制器和一个指令,它们都引用相同的firebase数据(我有充分的理由将它们分开).当我对一个引用进行更改时,除非保存数据,否则其他引用不会反映更改.在不必保存数据的情况下更新所有本地firebase引用的最佳方法是什么.我想让用户在保存之前看看他/她的更改会是什么样子.
我有一个说明我遇到的问题的傻瓜.键入一个输入,其他输入都不会更新,直到您保存.我希望他们都能在用户输入时保持同步.
http://jsfiddle.net/dberringer/F7MVB/2/
function MyCtrl($scope, $firebase) {
var ref1 = $firebase(new Firebase("https://fiddler.firebaseio.com/test1"));
ref1.$on('value', function () {
$scope.foo = ref1;
});
$scope.save1 = function () {
ref1.$save();
};
var ref2 = $firebase(new Firebase("https://fiddler.firebaseio.com/test1"));
ref2.$on('value', function () {
$scope.bar = ref2;
});
$scope.save2 = function () {
ref2.$save();
};
$scope.bas = $firebase(new Firebase("https://fiddler.firebaseio.com/test1"));
$scope.saveBas = function () {
$scope.bas.$save();
};
$scope.biz = $firebase(new Firebase("https://fiddler.firebaseio.com/test1"));
$scope.saveBiz = function () {
$scope.biz.$save();
};
}
Run Code Online (Sandbox Code Playgroud)
谢谢!!
更新: 抱歉.我应该更清楚.我发布的小提琴只是一种快速而肮脏的方式来说明问题.这里有一个更准确的情况表示,以及为什么能够在不保存的情况下传播更改会有所帮助.我试图弄清楚是否有一种开箱即用的方法将一个firebase引用上的更改传播到另一个firebase引用,而无需用户将数据保存到firebase.也许我需要触发一个方法或事件来告诉所有更新的引用以匹配a.那会很好.
正如您所看到的,我的所有模型逻辑都在myModel工厂中.我的控制器很好而且很薄.my-things指令可能存在也可能不存在于任何给定页面上,因此该指令直接访问模型而不是通过控制器的范围.我的控制器不需要为他们可能需要或可能不需要的指令注入一堆模型.在使用firebase时,走这条路线感觉非常干净自然.如果这是不可能的,人们有另一种方法来装配模型,与firebase资源交互,这会促进这个吗?再次感谢!
http://jsfiddle.net/dberringer/F7MVB/5/
angular.module('myApp',['firebase'])
.factory('myModel', function ($q, …Run Code Online (Sandbox Code Playgroud) 我已经构建了一个带有firebase的应用程序,可以登录用户并获得他们的id,但我无法弄清楚如何将这个与用户提交字符串相结合.
请参阅此处的代码笔:http://codepen.io/chriscruz/pen/OPPeLg
HTML下面:
<html ng-app="fluttrApp">
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<script src="https://cdn.firebase.com/js/client/2.0.2/firebase.js"></script>
<script src="https://cdn.firebase.com/libs/angularfire/0.9.0/angularfire.min.js"></script>
</head>
<body ng-controller="fluttrCtrl">
<button ng-click="auth.$authWithOAuthPopup('google')">Login with Google</button>
<li>Welcome, {{user.google.displayName }}</li>
<button ng-click="auth.$unauth()">Logout with Google</button>
<input ng-submit= "UpdateFirebaseWithString()" ng-model="string" ></input>
Run Code Online (Sandbox Code Playgroud)
Javascript下面:
<script>
var app = angular.module("fluttrApp", ["firebase"]);
app.factory("Auth", ["$firebaseAuth", function($firebaseAuth) {
var ref = new Firebase("https://crowdfluttr.firebaseio.com/");
return $firebaseAuth(ref);
}]);
app.controller("fluttrCtrl", ["$scope", "Auth", function($scope, Auth) {
$scope.auth = Auth;
$scope.user = $scope.auth.$getAuth();
$scope.UpdateFirebaseWithString = function () {
url = "https://crowdfluttr.firebaseio.com/ideas"
var ref = …Run Code Online (Sandbox Code Playgroud) 要在Angularfire 0.9中创建新用户,我可以使用$ firebaseAuth服务和$ createUser方法.
但是,据我所知,此方法不会返回任何新用户凭据.因此,如果我想向该用户添加配置文件,那么检索该用户Auth Data的最佳方法是什么,特别是"uid",这样我就可以为我的新用户保存配置文件数据.以下是我想要做的一个例子
var FIREBASEURL = "https://<my-firebase>.firebaseio.com"
var ref = new Firebase(FIREBASEURL);
$rootScope.authObj = $firebaseAuth(ref);
var newUser = {
email: "email@email.com",
password: "password",
displayName: "Display Name",
favFood: "Food"
};
$rootScope.authObj.$createUser(newUser.email, newUser.password).then(function() {
console.log("User created successfully!");
// Retrieve new Auth Data specifically uid
var newAuthData = ??????????
// Remove password from object and create user profile
delete newUser.password;
newUser.timestamp = Firebase.ServerValue.TIMESTAMP;
var userRef = new Firebase(FIREBASEURL + '/users/');
userRef.child( newAuthData.uid ).set(newUser);
}).catch(function(error) {
console.error("Error: ", error);
});
Run Code Online (Sandbox Code Playgroud) 我无法从AngularFire同步数组中获取单个记录.
这是我的服务:
app.factory("Projects", ["$firebaseArray", function($firebaseArray) {
// create a reference to the Firebase where we will store our data
var ref = new Firebase("https://XXXXXXXXXX.firebaseio.com");
var childRef = ref.child('projects');
var projects = $firebaseArray(childRef);
return {
all: projects,
create: function (projects) {
return projects.$add(project);
},
get: function (projectId) {
console.log(projectId);
projects.$loaded().then(function(x) {
var project = x.$getRecord(projectId);
console.log(project); // This print null
}).catch(function(error) {
console.log("Error:", error);
});
},
delete: function (project) {
return projects.$remove(project);
}
};
}
]);
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
app.controller('ProjectViewCtrl', function ($scope, $routeParams, …Run Code Online (Sandbox Code Playgroud) 以下代码段有效,除非用户的浏览器配置(例如iOS与Chrome)将其发送到$ authWithOAuthRedirect块 - 然后失败.
如果失败,我的意思是$ authWithOAuthRedirect方法有效,用户可以批准身份验证,但无法将范围正确发送给Google,也不会请求电子邮件访问.
var provider = 'google';
var scope = {scope:'email'};
var auth = $firebaseAuth(FirebaseInstance.firebase);
auth.$authWithOAuthPopup(provider, scope).then(function (authData, error) {
if (error && error.code === "TRANSPORT UNAVAILABLE") {
auth.$authWithOAuthRedirect(provider, function(error) {}, scope);
}
});
Run Code Online (Sandbox Code Playgroud)
简化后,此代码将无法请求用户的电子邮件:
var provider = 'google';
var scope = {scope:'email'};
var auth = $firebaseAuth(FirebaseInstance.firebase);
auth.$authWithOAuthRedirect(provider, function(error) {}, scope);
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
我正在使用AngularJS构建一个简单的Firebase应用程序.此应用通过Google对用户进行身份验证 每个用户都有一个书籍列表.任何人都可以看到书籍,即使他们没有经过身份验证.只有书籍的创作者才能编辑它.但是,即使其他人添加了书籍,个人用户也需要能够记录他们已经阅读过的书籍.
我rules.json喜欢这样:
{
"rules": {
".read": false,
".write": false,
"book": {
"$uid": {
".write": "auth !== null && auth.uid === $uid",
}
".read": true,
}
}
}
Run Code Online (Sandbox Code Playgroud)
而我正试着写一本书,只需:
$firebaseArray(new Firebase(URL + "/book")).$add({foo: "bar"})
Run Code Online (Sandbox Code Playgroud)
尝试这样做时,我收到"许可被拒绝"错误,虽然我似乎能够read在Forge中手动创建书籍.
我还认为,存储读者的最佳方式是使其成为本书的一个属性(一组$uid用于登录的读者). ".write"好像它会阻止这个,所以我该怎么做?
"$uid": {
".write": "auth !== null && auth.uid === $uid",
"readers": {
".write": "auth !== null"
}
},
Run Code Online (Sandbox Code Playgroud)
似乎验证规则在这里也是合适的......类似的东西newData.val() == auth.uid,但我不确定如何验证这readers应该是这些值的数组(或特别是一组).
firebase firebase-security angularfire firebase-authentication
FirebaseError有一个"代码"属性,但是如何在promise的catch方法中读取它?以下引发TypeScript错误:Property 'code' does not exist on type 'Error'.
this.af.database
.object(`/some/path`)
.set(newObj)
.then(data => {
console.log('success');
})
.catch(err => {
// Property 'code' does not exist on type 'Error'.
console.log(`code`, err.code);
});
Run Code Online (Sandbox Code Playgroud) 使用AngularFire2我尝试从单个Firestore文档中获取数据.有效的是:
this.addressRef = afs.doc<Address>('addresses/key')
this.addressRef.valueChanges().subscribe(val => {......}
Run Code Online (Sandbox Code Playgroud)
我想只运行一次订阅代码.通常我使用take(1)来实现这一点(与Firebase DB一起使用).但是这段代码:
this.addressRef.valueChanges().take(1).subscribe(val => {......}
Run Code Online (Sandbox Code Playgroud)
给出错误:TypeError:this.addressRef.valueChanges(...).take不是函数.
VS Code列出了采取行动.这是AngularFire2中的一个错误,我做错了还是有其他(更好的)方法在获取数据后停止订阅?我也尝试过topromise,但这也失败了.
我在我的应用程序中有此代码,我用来上传图像并获取其 url,以便我可以将其保存在数据库中,图像采用 base64 格式并且上传成功,正如我在console.log(snapshot);输出中看到的那样,并通过检查然而,也在我的 firebase 存储中,快照的 downloadUrl 属性未定义,我不知道为什么。这不是它应该工作的方式
storage.$putString(b64, 'data_url', {contentType:'image/jpg'}).$complete(function(snapshot) {
console.log(snapshot);
item.avatarUrl=snapshot.downloadURL;
agents.$add(item).then(function(ref) {
});
});
Run Code Online (Sandbox Code Playgroud)