0
0
Firebasecloud~5 mins

Custom authentication tokens in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to let users sign in to your app using your own system instead of the usual email or social login. Custom Authentication Tokens let you create special login tokens that Firebase accepts, so users can access your app securely with your own user IDs.
When you have your own user database and want to let those users sign in to Firebase services.
When you want to add extra information to user login tokens for your app's special needs.
When you want to control exactly who can sign in and what their ID is in Firebase.
When you want to integrate Firebase authentication with an existing login system.
When you want to create temporary login tokens for users without passwords.
Commands
Log in to Firebase CLI to authenticate your local machine with your Firebase project.
Terminal
firebase login
Expected OutputExpected
✔ Success! Logged in as your-email@example.com
Run a Node.js script that creates a custom authentication token for a user with a specific user ID.
Terminal
node createCustomToken.js
Expected OutputExpected
Custom token created: eyJhbGciOiJSUzI1NiIsImtpZCI6Ij...
Sign in to Firebase using the custom token generated, to verify the token works and the user is authenticated.
Terminal
firebase auth:signInWithCustomToken eyJhbGciOiJSUzI1NiIsImtpZCI6Ij...
Expected OutputExpected
✔ Successfully signed in with custom token.
Key Concept

If you remember nothing else from this pattern, remember: custom tokens let you securely sign in users with your own IDs by creating tokens Firebase trusts.

Common Mistakes
Using an expired or incorrectly signed custom token.
Firebase rejects tokens that are expired or not signed with your project's private key, so sign-in fails.
Always generate tokens using your Firebase Admin SDK with the correct credentials and use them quickly before they expire.
Trying to create custom tokens on the client side.
Creating tokens requires your private key and should only be done on a secure server to avoid security risks.
Generate custom tokens only on your trusted backend server using Firebase Admin SDK.
Summary
Log in to Firebase CLI to connect your machine to your Firebase project.
Use Firebase Admin SDK on a secure server to create custom authentication tokens for your users.
Sign in to Firebase using these tokens to authenticate users with your own user IDs.