Is there any way to delete the user from the firebase authentication?How to re-authenticate users in firebase realtime database?Deleting an element from an array in PHPshell_exec escaping quotes in php for Twitter API --> Getting CURL to work with obscure twitter api methodextract url from xml response and redirectABBYY OCR SDK: I am trying a sample script for recognizing business cards but not getting any outputphp Curl posting to PHPBBHow to implement cache system in php for json apicURL not working sometimes and gives empty resultMicrosoft outlook API give 404 errorhow can i check if RESTAPI is down using curl phpFB messenger Bot not getting postback payloads
Using "subway" as name for London Underground?
Can Rydberg constant be in joules?
SQL counting distinct over partition
How to draw a Technology Radar?
Commas in clist_map_inline:nn split values in undesired places
How do I create a Sector in Stellaris?
Did Milano or Benatar approve or comment on their namesake MCU ships?
The use of かります in a sentence
Group Integers by Originality
What's up with this leaf?
Zeros of the Hadamard product of holomorphic functions
Why doesn't Adrian Toomes give up Spider-Man's identity?
Arriving at the same result with the opposite hypotheses
Why would future John risk sending back a T-800 to save his younger self?
How do governments keep track of their issued currency?
Logarithm of exponential
Pre-1972 sci-fi short story or novel: alien(?) tunnel where people try new moves and get destroyed if they're not the correct ones
Extreme flexible working hours: how to control people and activities?
How can I tell the difference between unmarked sugar and stevia?
Were Alexander the Great and Hephaestion lovers?
Which physicist is this quote attributed to?
Applying Graph Theory to Linear Algebra (not the other way around)
Colloquialism for “see you later”
Can U.S. Tax Forms Be Legally HTMLified?
Is there any way to delete the user from the firebase authentication?
How to re-authenticate users in firebase realtime database?Deleting an element from an array in PHPshell_exec escaping quotes in php for Twitter API --> Getting CURL to work with obscure twitter api methodextract url from xml response and redirectABBYY OCR SDK: I am trying a sample script for recognizing business cards but not getting any outputphp Curl posting to PHPBBHow to implement cache system in php for json apicURL not working sometimes and gives empty resultMicrosoft outlook API give 404 errorhow can i check if RESTAPI is down using curl phpFB messenger Bot not getting postback payloads
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am using Firebase Auth Rest API. I have code written in PHP to add the user to the database and firebase authentication. The information I store is kind, idToken, email, refreshToken, expiresIn, localId. It all works great!
Now when I am trying to delete the user from database it works fine but does not delete the user from the firebase authentication. Please find the code below for sign up and deleting the user.
The errors I get is either
CREDENTIALS_TOO_OLD_LOGIN_AGAIN (or)
INVALID_ID_TOKEN.
FIREBASE_KEY is my firebase key and in the $data I am passing the user idToken
/*
* User Sign Up
*/
function user_signup($data)
$response = true;
$data = json_encode($data);
$url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=".FIREBASE_KEY;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$jsonResponse = curl_exec($ch);
if(curl_errno($ch))
$response = false;
curl_close($ch);
return $jsonResponse;
/*
* User Delete
*/
/* function user_delete($data)
$response = true;
$data = json_encode($data);
$url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount?key=".FIREBASE_KEY;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$jsonResponse = curl_exec($ch);
if(curl_errno($ch))
$response = false;
curl_close($ch);
return $jsonResponse;
*/
php firebase-realtime-database firebase-authentication
add a comment |
I am using Firebase Auth Rest API. I have code written in PHP to add the user to the database and firebase authentication. The information I store is kind, idToken, email, refreshToken, expiresIn, localId. It all works great!
Now when I am trying to delete the user from database it works fine but does not delete the user from the firebase authentication. Please find the code below for sign up and deleting the user.
The errors I get is either
CREDENTIALS_TOO_OLD_LOGIN_AGAIN (or)
INVALID_ID_TOKEN.
FIREBASE_KEY is my firebase key and in the $data I am passing the user idToken
/*
* User Sign Up
*/
function user_signup($data)
$response = true;
$data = json_encode($data);
$url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=".FIREBASE_KEY;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$jsonResponse = curl_exec($ch);
if(curl_errno($ch))
$response = false;
curl_close($ch);
return $jsonResponse;
/*
* User Delete
*/
/* function user_delete($data)
$response = true;
$data = json_encode($data);
$url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount?key=".FIREBASE_KEY;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$jsonResponse = curl_exec($ch);
if(curl_errno($ch))
$response = false;
curl_close($ch);
return $jsonResponse;
*/
php firebase-realtime-database firebase-authentication
To be able to delete the user's account, the user must have recently authenticated. The error message seems to indicate that this is not the case. The solution in that case, is to reauthenticate the user, and then try to delete their account agian.
– Frank van Puffelen
Mar 24 at 20:39
Thank you for your comment. I used securetoken.googleapis.com/v1/token?key='.FIREBASE_KEY and posted grant_type=refresh_token&refresh_token='.$user_refreshToken. The result I got was "access_token": "eyJhbGc..", "expires_in": "3600", "token_type": "Bearer", "refresh_token": "AEu4IL1..", "id_token": "eyJhbGciOiJS..", "user_id": "u2Jvb4844HN2..", "project_id": "4133...." And then I used the googleapis.com/identitytoolkit/v3/relyingparty/… and posted id_Token in post field and I got
– Sundar Rama Penumarthi
Mar 25 at 1:08
and the result i got is "error": "code": 400, "message": "INVALID_ID_TOKEN", "errors": [ "message": "INVALID_ID_TOKEN", "domain": "global", "reason": "invalid" ]
– Sundar Rama Penumarthi
Mar 25 at 1:14
@FrankvanPuffelen Could you please guide me on how do I re-authenticate the user? TYIA.
– Sundar Rama Penumarthi
Mar 25 at 1:56
Sorry, I don't know how to do that from PHP. I'd recommend updating your question though, since it's quite clear that a reauthenticate is required.
– Frank van Puffelen
Mar 25 at 2:06
add a comment |
I am using Firebase Auth Rest API. I have code written in PHP to add the user to the database and firebase authentication. The information I store is kind, idToken, email, refreshToken, expiresIn, localId. It all works great!
Now when I am trying to delete the user from database it works fine but does not delete the user from the firebase authentication. Please find the code below for sign up and deleting the user.
The errors I get is either
CREDENTIALS_TOO_OLD_LOGIN_AGAIN (or)
INVALID_ID_TOKEN.
FIREBASE_KEY is my firebase key and in the $data I am passing the user idToken
/*
* User Sign Up
*/
function user_signup($data)
$response = true;
$data = json_encode($data);
$url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=".FIREBASE_KEY;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$jsonResponse = curl_exec($ch);
if(curl_errno($ch))
$response = false;
curl_close($ch);
return $jsonResponse;
/*
* User Delete
*/
/* function user_delete($data)
$response = true;
$data = json_encode($data);
$url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount?key=".FIREBASE_KEY;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$jsonResponse = curl_exec($ch);
if(curl_errno($ch))
$response = false;
curl_close($ch);
return $jsonResponse;
*/
php firebase-realtime-database firebase-authentication
I am using Firebase Auth Rest API. I have code written in PHP to add the user to the database and firebase authentication. The information I store is kind, idToken, email, refreshToken, expiresIn, localId. It all works great!
Now when I am trying to delete the user from database it works fine but does not delete the user from the firebase authentication. Please find the code below for sign up and deleting the user.
The errors I get is either
CREDENTIALS_TOO_OLD_LOGIN_AGAIN (or)
INVALID_ID_TOKEN.
FIREBASE_KEY is my firebase key and in the $data I am passing the user idToken
/*
* User Sign Up
*/
function user_signup($data)
$response = true;
$data = json_encode($data);
$url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=".FIREBASE_KEY;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$jsonResponse = curl_exec($ch);
if(curl_errno($ch))
$response = false;
curl_close($ch);
return $jsonResponse;
/*
* User Delete
*/
/* function user_delete($data)
$response = true;
$data = json_encode($data);
$url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount?key=".FIREBASE_KEY;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$jsonResponse = curl_exec($ch);
if(curl_errno($ch))
$response = false;
curl_close($ch);
return $jsonResponse;
*/
php firebase-realtime-database firebase-authentication
php firebase-realtime-database firebase-authentication
asked Mar 24 at 18:02
Sundar Rama PenumarthiSundar Rama Penumarthi
1613
1613
To be able to delete the user's account, the user must have recently authenticated. The error message seems to indicate that this is not the case. The solution in that case, is to reauthenticate the user, and then try to delete their account agian.
– Frank van Puffelen
Mar 24 at 20:39
Thank you for your comment. I used securetoken.googleapis.com/v1/token?key='.FIREBASE_KEY and posted grant_type=refresh_token&refresh_token='.$user_refreshToken. The result I got was "access_token": "eyJhbGc..", "expires_in": "3600", "token_type": "Bearer", "refresh_token": "AEu4IL1..", "id_token": "eyJhbGciOiJS..", "user_id": "u2Jvb4844HN2..", "project_id": "4133...." And then I used the googleapis.com/identitytoolkit/v3/relyingparty/… and posted id_Token in post field and I got
– Sundar Rama Penumarthi
Mar 25 at 1:08
and the result i got is "error": "code": 400, "message": "INVALID_ID_TOKEN", "errors": [ "message": "INVALID_ID_TOKEN", "domain": "global", "reason": "invalid" ]
– Sundar Rama Penumarthi
Mar 25 at 1:14
@FrankvanPuffelen Could you please guide me on how do I re-authenticate the user? TYIA.
– Sundar Rama Penumarthi
Mar 25 at 1:56
Sorry, I don't know how to do that from PHP. I'd recommend updating your question though, since it's quite clear that a reauthenticate is required.
– Frank van Puffelen
Mar 25 at 2:06
add a comment |
To be able to delete the user's account, the user must have recently authenticated. The error message seems to indicate that this is not the case. The solution in that case, is to reauthenticate the user, and then try to delete their account agian.
– Frank van Puffelen
Mar 24 at 20:39
Thank you for your comment. I used securetoken.googleapis.com/v1/token?key='.FIREBASE_KEY and posted grant_type=refresh_token&refresh_token='.$user_refreshToken. The result I got was "access_token": "eyJhbGc..", "expires_in": "3600", "token_type": "Bearer", "refresh_token": "AEu4IL1..", "id_token": "eyJhbGciOiJS..", "user_id": "u2Jvb4844HN2..", "project_id": "4133...." And then I used the googleapis.com/identitytoolkit/v3/relyingparty/… and posted id_Token in post field and I got
– Sundar Rama Penumarthi
Mar 25 at 1:08
and the result i got is "error": "code": 400, "message": "INVALID_ID_TOKEN", "errors": [ "message": "INVALID_ID_TOKEN", "domain": "global", "reason": "invalid" ]
– Sundar Rama Penumarthi
Mar 25 at 1:14
@FrankvanPuffelen Could you please guide me on how do I re-authenticate the user? TYIA.
– Sundar Rama Penumarthi
Mar 25 at 1:56
Sorry, I don't know how to do that from PHP. I'd recommend updating your question though, since it's quite clear that a reauthenticate is required.
– Frank van Puffelen
Mar 25 at 2:06
To be able to delete the user's account, the user must have recently authenticated. The error message seems to indicate that this is not the case. The solution in that case, is to reauthenticate the user, and then try to delete their account agian.
– Frank van Puffelen
Mar 24 at 20:39
To be able to delete the user's account, the user must have recently authenticated. The error message seems to indicate that this is not the case. The solution in that case, is to reauthenticate the user, and then try to delete their account agian.
– Frank van Puffelen
Mar 24 at 20:39
Thank you for your comment. I used securetoken.googleapis.com/v1/token?key='.FIREBASE_KEY and posted grant_type=refresh_token&refresh_token='.$user_refreshToken. The result I got was "access_token": "eyJhbGc..", "expires_in": "3600", "token_type": "Bearer", "refresh_token": "AEu4IL1..", "id_token": "eyJhbGciOiJS..", "user_id": "u2Jvb4844HN2..", "project_id": "4133...." And then I used the googleapis.com/identitytoolkit/v3/relyingparty/… and posted id_Token in post field and I got
– Sundar Rama Penumarthi
Mar 25 at 1:08
Thank you for your comment. I used securetoken.googleapis.com/v1/token?key='.FIREBASE_KEY and posted grant_type=refresh_token&refresh_token='.$user_refreshToken. The result I got was "access_token": "eyJhbGc..", "expires_in": "3600", "token_type": "Bearer", "refresh_token": "AEu4IL1..", "id_token": "eyJhbGciOiJS..", "user_id": "u2Jvb4844HN2..", "project_id": "4133...." And then I used the googleapis.com/identitytoolkit/v3/relyingparty/… and posted id_Token in post field and I got
– Sundar Rama Penumarthi
Mar 25 at 1:08
and the result i got is "error": "code": 400, "message": "INVALID_ID_TOKEN", "errors": [ "message": "INVALID_ID_TOKEN", "domain": "global", "reason": "invalid" ]
– Sundar Rama Penumarthi
Mar 25 at 1:14
and the result i got is "error": "code": 400, "message": "INVALID_ID_TOKEN", "errors": [ "message": "INVALID_ID_TOKEN", "domain": "global", "reason": "invalid" ]
– Sundar Rama Penumarthi
Mar 25 at 1:14
@FrankvanPuffelen Could you please guide me on how do I re-authenticate the user? TYIA.
– Sundar Rama Penumarthi
Mar 25 at 1:56
@FrankvanPuffelen Could you please guide me on how do I re-authenticate the user? TYIA.
– Sundar Rama Penumarthi
Mar 25 at 1:56
Sorry, I don't know how to do that from PHP. I'd recommend updating your question though, since it's quite clear that a reauthenticate is required.
– Frank van Puffelen
Mar 25 at 2:06
Sorry, I don't know how to do that from PHP. I'd recommend updating your question though, since it's quite clear that a reauthenticate is required.
– Frank van Puffelen
Mar 25 at 2:06
add a comment |
1 Answer
1
active
oldest
votes
There are two ways to interact with the Firebase REST APIs:
- By authenticating your requests with a user's ID token, with the same permissions and limitations as if they would interact with your application on their own
- By authenticating with the credentials of a Service Account, which gives you full access to your application, without any limitations.
To delete a user, you can use both methods, but when using a user's ID token, you have to authenticate as the user (effectively impersonating them) before being able to perform any actions on behalf of said user.
The better solution would be to use an Admin SDK to perform that task. By authenticating your requests to the Firebase REST APIs with Service Account Credentials as described in
Add the Firebase Admin SDK to Your Server, you will be able to perform administrative tasks (like deleting a user from the authentication database) more easily.
Here are the steps to get started with Service Account based authentication:
- Generate Service Account credentials on https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk
- Use the Google Auth Library for PHP to be able to make authenticated calls the Google/Firebase APIs https://github.com/googleapis/google-auth-library-php#call-the-apis
- When you have created an HTTP client with the help of the Auth library, you can call this API endpoint to delete the user
$client->post('https://www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount', [
'json' => [
'localId' => 'uid-of-user-to-delete'
]
]);
The localId parameter is not documented on https://firebase.google.com/docs/reference/rest/auth/#section-delete-account, but it's used from within the official admin SDK and works.
Using an Admin SDK (https://firebase.google.com/docs/admin/setup#initialize_the_sdk) would be the recommended way to perform administrative tasks like this. Official SDKs exist for Node.js, Java, Python, Go and C# - I maintain an unofficial one for PHP that you can find at https://github.com/kreait/firebase-php. With it, you could perform the same task like this:
$serviceAccount = ServiceAccount::fromJsonFile('service_account.json');
$firebase = (new Factory())
->withServiceAccount($serviceAccount)
->create();
$firebase->getAuth()->deleteUser('uid-of-user-to-delete');
On a side note:
I would consider storing a user's ID token in a separate database a security risk: if your database gets compromised, attackers gain access to your user's ID tokens and can use those who aren't expired yet to access your application.
The recommended flow to pass a user from your frontend (web, mobile) to your backend (server) is:
- Use a Firebase Client SDK in your frontend, e.g. in your web application
- Let the user sign in to Firebase in the frontend via the client SDK, and when a user successfully signed in, retrieve the ID token on your client, send it to your backend and verify the ID token on your backend.
- Once you've verified the ID token, you can extract the Firebase ID of your user from the ID token and save it to your database, e.g. in a table that maps your local user id to the Firebase User ID without the need to store their full ID token (= full credentials)
Thank you @jeromegamez! For the detailed explanation It makes more sense now.
– Sundar Rama Penumarthi
Mar 25 at 23:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55326844%2fis-there-any-way-to-delete-the-user-from-the-firebase-authentication%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are two ways to interact with the Firebase REST APIs:
- By authenticating your requests with a user's ID token, with the same permissions and limitations as if they would interact with your application on their own
- By authenticating with the credentials of a Service Account, which gives you full access to your application, without any limitations.
To delete a user, you can use both methods, but when using a user's ID token, you have to authenticate as the user (effectively impersonating them) before being able to perform any actions on behalf of said user.
The better solution would be to use an Admin SDK to perform that task. By authenticating your requests to the Firebase REST APIs with Service Account Credentials as described in
Add the Firebase Admin SDK to Your Server, you will be able to perform administrative tasks (like deleting a user from the authentication database) more easily.
Here are the steps to get started with Service Account based authentication:
- Generate Service Account credentials on https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk
- Use the Google Auth Library for PHP to be able to make authenticated calls the Google/Firebase APIs https://github.com/googleapis/google-auth-library-php#call-the-apis
- When you have created an HTTP client with the help of the Auth library, you can call this API endpoint to delete the user
$client->post('https://www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount', [
'json' => [
'localId' => 'uid-of-user-to-delete'
]
]);
The localId parameter is not documented on https://firebase.google.com/docs/reference/rest/auth/#section-delete-account, but it's used from within the official admin SDK and works.
Using an Admin SDK (https://firebase.google.com/docs/admin/setup#initialize_the_sdk) would be the recommended way to perform administrative tasks like this. Official SDKs exist for Node.js, Java, Python, Go and C# - I maintain an unofficial one for PHP that you can find at https://github.com/kreait/firebase-php. With it, you could perform the same task like this:
$serviceAccount = ServiceAccount::fromJsonFile('service_account.json');
$firebase = (new Factory())
->withServiceAccount($serviceAccount)
->create();
$firebase->getAuth()->deleteUser('uid-of-user-to-delete');
On a side note:
I would consider storing a user's ID token in a separate database a security risk: if your database gets compromised, attackers gain access to your user's ID tokens and can use those who aren't expired yet to access your application.
The recommended flow to pass a user from your frontend (web, mobile) to your backend (server) is:
- Use a Firebase Client SDK in your frontend, e.g. in your web application
- Let the user sign in to Firebase in the frontend via the client SDK, and when a user successfully signed in, retrieve the ID token on your client, send it to your backend and verify the ID token on your backend.
- Once you've verified the ID token, you can extract the Firebase ID of your user from the ID token and save it to your database, e.g. in a table that maps your local user id to the Firebase User ID without the need to store their full ID token (= full credentials)
Thank you @jeromegamez! For the detailed explanation It makes more sense now.
– Sundar Rama Penumarthi
Mar 25 at 23:12
add a comment |
There are two ways to interact with the Firebase REST APIs:
- By authenticating your requests with a user's ID token, with the same permissions and limitations as if they would interact with your application on their own
- By authenticating with the credentials of a Service Account, which gives you full access to your application, without any limitations.
To delete a user, you can use both methods, but when using a user's ID token, you have to authenticate as the user (effectively impersonating them) before being able to perform any actions on behalf of said user.
The better solution would be to use an Admin SDK to perform that task. By authenticating your requests to the Firebase REST APIs with Service Account Credentials as described in
Add the Firebase Admin SDK to Your Server, you will be able to perform administrative tasks (like deleting a user from the authentication database) more easily.
Here are the steps to get started with Service Account based authentication:
- Generate Service Account credentials on https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk
- Use the Google Auth Library for PHP to be able to make authenticated calls the Google/Firebase APIs https://github.com/googleapis/google-auth-library-php#call-the-apis
- When you have created an HTTP client with the help of the Auth library, you can call this API endpoint to delete the user
$client->post('https://www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount', [
'json' => [
'localId' => 'uid-of-user-to-delete'
]
]);
The localId parameter is not documented on https://firebase.google.com/docs/reference/rest/auth/#section-delete-account, but it's used from within the official admin SDK and works.
Using an Admin SDK (https://firebase.google.com/docs/admin/setup#initialize_the_sdk) would be the recommended way to perform administrative tasks like this. Official SDKs exist for Node.js, Java, Python, Go and C# - I maintain an unofficial one for PHP that you can find at https://github.com/kreait/firebase-php. With it, you could perform the same task like this:
$serviceAccount = ServiceAccount::fromJsonFile('service_account.json');
$firebase = (new Factory())
->withServiceAccount($serviceAccount)
->create();
$firebase->getAuth()->deleteUser('uid-of-user-to-delete');
On a side note:
I would consider storing a user's ID token in a separate database a security risk: if your database gets compromised, attackers gain access to your user's ID tokens and can use those who aren't expired yet to access your application.
The recommended flow to pass a user from your frontend (web, mobile) to your backend (server) is:
- Use a Firebase Client SDK in your frontend, e.g. in your web application
- Let the user sign in to Firebase in the frontend via the client SDK, and when a user successfully signed in, retrieve the ID token on your client, send it to your backend and verify the ID token on your backend.
- Once you've verified the ID token, you can extract the Firebase ID of your user from the ID token and save it to your database, e.g. in a table that maps your local user id to the Firebase User ID without the need to store their full ID token (= full credentials)
Thank you @jeromegamez! For the detailed explanation It makes more sense now.
– Sundar Rama Penumarthi
Mar 25 at 23:12
add a comment |
There are two ways to interact with the Firebase REST APIs:
- By authenticating your requests with a user's ID token, with the same permissions and limitations as if they would interact with your application on their own
- By authenticating with the credentials of a Service Account, which gives you full access to your application, without any limitations.
To delete a user, you can use both methods, but when using a user's ID token, you have to authenticate as the user (effectively impersonating them) before being able to perform any actions on behalf of said user.
The better solution would be to use an Admin SDK to perform that task. By authenticating your requests to the Firebase REST APIs with Service Account Credentials as described in
Add the Firebase Admin SDK to Your Server, you will be able to perform administrative tasks (like deleting a user from the authentication database) more easily.
Here are the steps to get started with Service Account based authentication:
- Generate Service Account credentials on https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk
- Use the Google Auth Library for PHP to be able to make authenticated calls the Google/Firebase APIs https://github.com/googleapis/google-auth-library-php#call-the-apis
- When you have created an HTTP client with the help of the Auth library, you can call this API endpoint to delete the user
$client->post('https://www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount', [
'json' => [
'localId' => 'uid-of-user-to-delete'
]
]);
The localId parameter is not documented on https://firebase.google.com/docs/reference/rest/auth/#section-delete-account, but it's used from within the official admin SDK and works.
Using an Admin SDK (https://firebase.google.com/docs/admin/setup#initialize_the_sdk) would be the recommended way to perform administrative tasks like this. Official SDKs exist for Node.js, Java, Python, Go and C# - I maintain an unofficial one for PHP that you can find at https://github.com/kreait/firebase-php. With it, you could perform the same task like this:
$serviceAccount = ServiceAccount::fromJsonFile('service_account.json');
$firebase = (new Factory())
->withServiceAccount($serviceAccount)
->create();
$firebase->getAuth()->deleteUser('uid-of-user-to-delete');
On a side note:
I would consider storing a user's ID token in a separate database a security risk: if your database gets compromised, attackers gain access to your user's ID tokens and can use those who aren't expired yet to access your application.
The recommended flow to pass a user from your frontend (web, mobile) to your backend (server) is:
- Use a Firebase Client SDK in your frontend, e.g. in your web application
- Let the user sign in to Firebase in the frontend via the client SDK, and when a user successfully signed in, retrieve the ID token on your client, send it to your backend and verify the ID token on your backend.
- Once you've verified the ID token, you can extract the Firebase ID of your user from the ID token and save it to your database, e.g. in a table that maps your local user id to the Firebase User ID without the need to store their full ID token (= full credentials)
There are two ways to interact with the Firebase REST APIs:
- By authenticating your requests with a user's ID token, with the same permissions and limitations as if they would interact with your application on their own
- By authenticating with the credentials of a Service Account, which gives you full access to your application, without any limitations.
To delete a user, you can use both methods, but when using a user's ID token, you have to authenticate as the user (effectively impersonating them) before being able to perform any actions on behalf of said user.
The better solution would be to use an Admin SDK to perform that task. By authenticating your requests to the Firebase REST APIs with Service Account Credentials as described in
Add the Firebase Admin SDK to Your Server, you will be able to perform administrative tasks (like deleting a user from the authentication database) more easily.
Here are the steps to get started with Service Account based authentication:
- Generate Service Account credentials on https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk
- Use the Google Auth Library for PHP to be able to make authenticated calls the Google/Firebase APIs https://github.com/googleapis/google-auth-library-php#call-the-apis
- When you have created an HTTP client with the help of the Auth library, you can call this API endpoint to delete the user
$client->post('https://www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount', [
'json' => [
'localId' => 'uid-of-user-to-delete'
]
]);
The localId parameter is not documented on https://firebase.google.com/docs/reference/rest/auth/#section-delete-account, but it's used from within the official admin SDK and works.
Using an Admin SDK (https://firebase.google.com/docs/admin/setup#initialize_the_sdk) would be the recommended way to perform administrative tasks like this. Official SDKs exist for Node.js, Java, Python, Go and C# - I maintain an unofficial one for PHP that you can find at https://github.com/kreait/firebase-php. With it, you could perform the same task like this:
$serviceAccount = ServiceAccount::fromJsonFile('service_account.json');
$firebase = (new Factory())
->withServiceAccount($serviceAccount)
->create();
$firebase->getAuth()->deleteUser('uid-of-user-to-delete');
On a side note:
I would consider storing a user's ID token in a separate database a security risk: if your database gets compromised, attackers gain access to your user's ID tokens and can use those who aren't expired yet to access your application.
The recommended flow to pass a user from your frontend (web, mobile) to your backend (server) is:
- Use a Firebase Client SDK in your frontend, e.g. in your web application
- Let the user sign in to Firebase in the frontend via the client SDK, and when a user successfully signed in, retrieve the ID token on your client, send it to your backend and verify the ID token on your backend.
- Once you've verified the ID token, you can extract the Firebase ID of your user from the ID token and save it to your database, e.g. in a table that maps your local user id to the Firebase User ID without the need to store their full ID token (= full credentials)
answered Mar 25 at 9:24
jeromegamezjeromegamez
2,1611424
2,1611424
Thank you @jeromegamez! For the detailed explanation It makes more sense now.
– Sundar Rama Penumarthi
Mar 25 at 23:12
add a comment |
Thank you @jeromegamez! For the detailed explanation It makes more sense now.
– Sundar Rama Penumarthi
Mar 25 at 23:12
Thank you @jeromegamez! For the detailed explanation It makes more sense now.
– Sundar Rama Penumarthi
Mar 25 at 23:12
Thank you @jeromegamez! For the detailed explanation It makes more sense now.
– Sundar Rama Penumarthi
Mar 25 at 23:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55326844%2fis-there-any-way-to-delete-the-user-from-the-firebase-authentication%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
To be able to delete the user's account, the user must have recently authenticated. The error message seems to indicate that this is not the case. The solution in that case, is to reauthenticate the user, and then try to delete their account agian.
– Frank van Puffelen
Mar 24 at 20:39
Thank you for your comment. I used securetoken.googleapis.com/v1/token?key='.FIREBASE_KEY and posted grant_type=refresh_token&refresh_token='.$user_refreshToken. The result I got was "access_token": "eyJhbGc..", "expires_in": "3600", "token_type": "Bearer", "refresh_token": "AEu4IL1..", "id_token": "eyJhbGciOiJS..", "user_id": "u2Jvb4844HN2..", "project_id": "4133...." And then I used the googleapis.com/identitytoolkit/v3/relyingparty/… and posted id_Token in post field and I got
– Sundar Rama Penumarthi
Mar 25 at 1:08
and the result i got is "error": "code": 400, "message": "INVALID_ID_TOKEN", "errors": [ "message": "INVALID_ID_TOKEN", "domain": "global", "reason": "invalid" ]
– Sundar Rama Penumarthi
Mar 25 at 1:14
@FrankvanPuffelen Could you please guide me on how do I re-authenticate the user? TYIA.
– Sundar Rama Penumarthi
Mar 25 at 1:56
Sorry, I don't know how to do that from PHP. I'd recommend updating your question though, since it's quite clear that a reauthenticate is required.
– Frank van Puffelen
Mar 25 at 2:06