Newaj
Newaj

Reputation: 4450

Firebase authentication error with custom token

I am trying to authenticate user in firebase with KakaoTalk credentials. For that, I've got accesstoken from KakaoTalk. And then trying to authenticate user with that token. Here is my code :

      String token = await kakaoService.getAccessToken();
      await firebaseAuth.signInWithCustomToken(
        token: token,
      );

Got acceess token like this : nmAzFpOF9XrijP-ZoFpQbVluGZ4lLDbZxOCXIAo9c-sAAAFxrID6xA

But getting this error :

The custom token format is incorrect. Please check the documentation. [ Invalid assertion format. 3 dot separated segments required. ]

Whats wrong here? Am I missing something?

Upvotes: 5

Views: 8328

Answers (2)

MurrayR
MurrayR

Reputation: 426

Check out the Firebase documentation regarding the use of custom token: https://firebase.google.com/docs/auth/admin/create-custom-tokens#create_custom_tokens_using_a_third-party_jwt_library

Firebase needs to successfully decode the auth token your client submits then use its claims to validate access to your Firebase resources. As such, Firebase requires that custom tokens be formatted according to the rules spelled out in their docs. (They describe a very typical JSON Web Token.)

The access token you're getting from KakaoTalk does not follow Firebase's token rules so Firebase doesn't know what to do with it. I suggest you revisit the KakaoTalk docs to see if it can generate a standard RS256 JWT token with which Firebase can work.

Upvotes: 4

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

It seems that the token returns by kakaoService.getAccessToken() is not a valid custom token for Firebase Authentication. In fact, given the error message, it doesn't even seem to be a JWT.

Custom tokens for Firebase Authentication must have a specific format, that is documented in creating custom tokens. You'll typically want to follow this process to get a valid token for Firebase Authentication:

  1. Sign the user in to the identity provider (KakaoTalk in your case).
  2. Decode the token from the provider, to get the verified information about the user.
  3. Create a custom token for the user with the Firebase Authentication Admin SDK.
  4. Use that token to sign in to Firebase on the client.

Steps 2 and 3 must happen in a trusted environment, such as your development machine, a server you control, or Cloud Functions.

Upvotes: 4

Related Questions