📜  firebase auth rest api (1)

📅  最后修改于: 2023-12-03 15:00:45.613000             🧑  作者: Mango

Firebase Auth REST API

Firebase Auth REST API is a set of HTTP APIs which allows application developers to manage the authentication of end-users programmatically without needing a user-interface. With these APIs, developers can authenticate users with Email/Password, Google, Facebook, Twitter, GitHub, and other third-party authentication providers.

Features
  • Create User: Create a new user with email and password authentication provider.
  • Sign In with Email/Password: Authenticate a user with email and password.
  • Sign In with OAuth Providers: Authenticate a user with OAuth provider's access token.
  • Link Accounts: Link an authenticated user's account to a third-party OAuth provider.
  • Unlink Accounts: Remove the association between a user's account and the third-party OAuth provider.
  • Get User by UID: Get user information by UID.
  • Get User by Email: Get user information by email.
  • Get User by Phone Number: Get user information by phone number.
  • Update User: Update a user's information, such as email, display name, and profile photo.
  • Verify Id Token: Verify the signature and payload of a Firebase ID token.
  • Refresh Token: Exchange a refresh token for a new Firebase ID token.
Getting Started
Prerequisites

Before you can use Firebase Auth REST API, ensure you have the following:

  1. A Firebase project with the Firebase Auth enabled. If you don't already have a project, create one here.
  2. An API key for the project, which you can obtain from the Project Settings page of the Firebase console.
Authentication

To authenticate your requests to Firebase Auth REST API, you need to include an Authorization header with a valid Firebase ID token, which you can get by calling the Firebase Authentication API from your application or by generating one with your service account credentials.

Here's an example of a HTTP Authorization header with a Firebase ID token:

Authorization: Bearer <Firebase ID token>
Example

Here's an example of how you might use Firebase Auth REST API to create a new user:

POST https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=<API key>
Content-Type: application/json

{
  "email": "user@example.com",
  "password": "password123"
}

And here's the response from the server:

HTTP/1.1 200 OK
Content-Type: application/json

{
  "idToken": "<Firebase ID Token>",
  "refreshToken": "<Refresh Token>",
  "expiresIn": "3600",
  "localId": "0123456789abcdefg",
  "email": "user@example.com"
}
Conclusion

Firebase Auth REST API provides a simple and secure way to authenticate users and manage their accounts for your application. By enabling this API in your Firebase project, you can easily integrate different authentication providers and customize your user experience.