0
0
Firebasecloud~10 mins

User session management in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - User session management
User logs in
Firebase creates session token
Token stored on client
User makes requests
Firebase verifies token
If token valid
Allow access
No
Request denied, ask login
This flow shows how Firebase manages user sessions by creating a token at login, storing it on the client, verifying it on requests, and allowing or denying access.
Execution Sample
Firebase
firebase.auth().signInWithEmailAndPassword(email, password)
  .then(userCredential => {
    const user = userCredential.user;
    return user.getIdToken();
  })
  .then(token => {
    // Store token for session
  });
This code logs in a user with email and password, then gets a session token to manage the user session.
Process Table
StepActionFirebase ResponseClient StateNext Step
1User submits login formReceives login requestNo session tokenVerify credentials
2Firebase verifies credentialsCredentials validNo session tokenCreate session token
3Firebase creates session tokenToken generatedNo session tokenSend token to client
4Client receives tokenToken sentStores token locallyUser authenticated
5User makes authenticated requestReceives request with tokenHas tokenVerify token validity
6Firebase verifies tokenToken validHas tokenAllow access to resource
7User makes request with expired tokenReceives request with tokenHas expired tokenReject request, ask login
8User logs outSession endedToken removed from clientUser unauthenticated
💡 Execution stops when user logs out or token expires and user must log in again.
Status Tracker
VariableStartAfter Step 3After Step 4After Step 6After Step 8
sessionTokennullnulltoken123abctoken123abcnull
userAuthenticatedfalsefalsetruetruefalse
Key Moments - 3 Insights
Why does the client store the session token after login?
The client stores the token (see execution_table step 4) so it can send it with future requests to prove the user is logged in.
What happens if the token expires before the user logs out?
At step 7, Firebase rejects requests with expired tokens, forcing the user to log in again to get a new token.
Does logging out immediately remove the token from Firebase?
No, logging out removes the token from the client (step 8), ending the session from the user's side.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the client state after step 4?
ANo session token stored
BUser unauthenticated
CSession token stored locally
DToken expired
💡 Hint
Check the 'Client State' column at step 4 in the execution_table.
At which step does Firebase verify the token validity?
AStep 4
BStep 6
CStep 2
DStep 8
💡 Hint
Look for 'Firebase verifies token' in the 'Action' column of the execution_table.
If the user logs out, what happens to the sessionToken variable according to variable_tracker?
AIt becomes null
BIt changes to a new token
CIt remains the same
DIt becomes expired
💡 Hint
Check the 'sessionToken' row in variable_tracker after step 8.
Concept Snapshot
User session management in Firebase:
- User logs in with credentials
- Firebase creates a session token
- Token stored on client for future requests
- Firebase verifies token on each request
- Expired tokens cause access denial
- Logging out removes token from client
Full Transcript
User session management in Firebase starts when the user logs in with their email and password. Firebase checks the credentials and creates a session token if valid. This token is sent to the client and stored locally. When the user makes requests, the client sends the token to Firebase, which verifies it. If the token is valid, access is allowed. If the token expires or the user logs out, the session ends and the user must log in again to get a new token.