0

I am designing a math problem site using Firebase and I want to display a problem when someone logs in.

What I want in pseudo code is,

if user logged in
document.write([problem])
else
document.write(Please login to see the problem)

Any ideas?

mipe34
  • 5,596
  • 3
  • 26
  • 38
mathguy56
  • 33
  • 6

1 Answers1

1

When using Firebase Simple Login, upon instantiation of the FirebaseAuthClient you will define a callback function that is invoked any time the login state of the user changes.

From https://www.firebase.com/docs/security/simple-login-overview.html:

var chatRef = new Firebase('https://SampleChat.firebaseIO-demo.com');
var authClient = new FirebaseAuthClient(chatRef, function(error, user) {
  if (user) {
    // user authenticated with Firebase
  } else if (error) {
    // an error occurred authenticating the user
  } else {
    // user is logged out
  }
});

For your case, if you have a user object, you can hide any login-related UI and show the problem, otherwise, hide the problem and show any login-related UI.

Then, to log users in, choose one or more of the Firebase Simple Login authentication providers, configure that provider in Forge (accessed via https://<your-firebase>.firebaseio.com) and attempt to authenticate the user via:

authClient.login(<provider>, <options>);

I hope that helps!

Rob DiMarco
  • 13,226
  • 1
  • 43
  • 55
  • So I should write as, `var chatRef = new Firebase('https://.firebaseIO-demo.com'); var authClient = new FirebaseAuthClient(chatRef, function(error, user) { if (user) { document.write("Factor x^2+2x+1") } else if (error) { document.write("An error occurred while logging you in.Try again.") } else { document.write("Please login to see the problem") } });` Is that all? – mathguy56 Feb 19 '13 at 15:48
  • @ShenalSanthush After replacing `.firebaseIO-demo.com` with your Firebase URL (somename.firebaseio.com), that looks good for monitoring user authentication state. Then, call `authClient.login(...)` once a user clicks the login button in your application. – Rob DiMarco Feb 19 '13 at 18:03
  • @ShenalSanthush To see a complete example of email / password authentication using Firebase Simple Login, see [https://groups.google.com/forum/?hl=en_US&fromgroups=#!topic/firebase-talk/POcs7m5gihg](https://groups.google.com/forum/?hl=en_US&fromgroups=#!topic/firebase-talk/POcs7m5gihg). – Rob DiMarco Feb 19 '13 at 18:53
  • I used the following code to create a register button `` (I used for security purposes) But when I click the button,nothing happens.Why? – mathguy56 Feb 21 '13 at 11:05
  • @ShenalSanthush Ensure that you have enabled email / password authentication for your Firebase in Forge (accessible at `.firebaseio.com`. Then, try logging the error field to see if there is anything invalid about your arguments to that method. – Rob DiMarco Feb 22 '13 at 15:31