如何在用户登录时从firebase应用程序获取数据

时间:2015-04-17 20:00:09

标签: javascript firebase

好的,所以我有一个网站,其中有一个论坛,使用firebase来保存论坛帖子的数据和用户数据登录。我遇到的问题是在用户登录时从用户子项中提取数据,以便论坛可以使用该数据。当用户创建他们的帐户时,他们会创建用户名以及电子邮件和密码。我想做的是在论坛中添加帖子时在论坛中显示该用户名,但无论我尝试什么,似乎都没有从firebase中提取用户名。当用户登录时,我希望将数据从我的登录控制器发送到service.js文件,以便我所有单独的控制器和html页面都可以看到用户名数据并显示我需要的内容。

1 个答案:

答案 0 :(得分:1)

听起来你正在使用角度......我想?

如果是这样,您可以将返回的登录数据保存到$ rootScope,甚至可以更好地使用localstorage。然后只需在需要时从其中一个访问它。

// Create a callback to handle the result of the authentication
function authHandler(error, authData) {
  if (error) {
    console.log("Login Failed!", error);
  } else {
    console.log("Authenticated successfully with payload:", authData);
    $rootScope.user = authData.uid;
  }
}

// Authenticate users with a custom Firebase token
ref.authWithCustomToken("<token>", authHandler);

// Alternatively, authenticate users anonymously
ref.authAnonymously(authHandler);

// Or with an email/password combination
ref.authWithPassword({
  email    : 'bobtony@firebase.com',
  password : 'correcthorsebatterystaple'
}, authHandler);