-
Notifications
You must be signed in to change notification settings - Fork 928
Add linkWithCredential(), linkWithPhoneNumber(), unlink() #3213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1407e9c
Add unlink(), linkWithCredential(), linkWithPhoneNumber()
sam-gc 4f46bc4
Formatting
sam-gc 5768cd4
Formatting
sam-gc 64ff065
PR feedback
sam-gc 2d80d38
Formatting
sam-gc ead029f
Add export to index.ts
sam-gc 6079558
Formatting
sam-gc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,19 @@ | |
*/ | ||
|
||
import * as externs from '@firebase/auth-types-exp'; | ||
|
||
import { OperationType, UserCredential } from '@firebase/auth-types-exp'; | ||
|
||
import { PhoneOrOauthTokenResponse } from '../../api/authentication/mfa'; | ||
import { SignInWithPhoneNumberResponse } from '../../api/authentication/sms'; | ||
import { Auth } from '../../model/auth'; | ||
import { User } from '../../model/user'; | ||
import { AuthCredential } from '../credentials'; | ||
import { PhoneAuthCredential } from '../credentials/phone'; | ||
import { AuthErrorCode } from '../errors'; | ||
import { _reloadWithoutSaving } from '../user/reload'; | ||
import { UserCredentialImpl } from '../user/user_credential_impl'; | ||
import { assert } from '../util/assert'; | ||
import { providerDataAsNames } from '../util/providers'; | ||
|
||
export async function signInWithCredential( | ||
authExtern: externs.Auth, | ||
|
@@ -39,3 +47,51 @@ export async function signInWithCredential( | |
await auth.updateCurrentUser(userCredential.user); | ||
return userCredential; | ||
} | ||
|
||
export async function linkWithCredential( | ||
userExtern: externs.User, | ||
credentialExtern: externs.AuthCredential | ||
): Promise<UserCredential> { | ||
const user = userExtern as User; | ||
const credential = credentialExtern as AuthCredential; | ||
await _assertLinkedStatus(false, user, credential.providerId); | ||
|
||
const response = await credential._linkToIdToken( | ||
user.auth, | ||
await user.getIdToken() | ||
); | ||
|
||
const newCred = _authCredentialFromTokenResponse(response); | ||
await user._updateTokensIfNecessary(response, /* reload */ true); | ||
return new UserCredentialImpl(user, newCred, OperationType.LINK); | ||
avolkovi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
export function _authCredentialFromTokenResponse( | ||
response: PhoneOrOauthTokenResponse | ||
): AuthCredential | null { | ||
const { | ||
temporaryProof, | ||
phoneNumber | ||
} = response as SignInWithPhoneNumberResponse; | ||
if (temporaryProof && phoneNumber) { | ||
return new PhoneAuthCredential({ temporaryProof, phoneNumber }); | ||
} | ||
|
||
// TODO: Handle Oauth cases | ||
return null; | ||
} | ||
|
||
export async function _assertLinkedStatus( | ||
expected: boolean, | ||
user: User, | ||
provider: externs.ProviderId | ||
): Promise<void> { | ||
await _reloadWithoutSaving(user); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there's a potential race condition if this gets called twice, do we care? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussed offline, it should be fine |
||
const providerIds = providerDataAsNames(user.providerData); | ||
|
||
const code = | ||
expected === false | ||
? AuthErrorCode.PROVIDER_ALREADY_LINKED | ||
: AuthErrorCode.NO_SUCH_PROVIDER; | ||
assert(providerIds.has(provider) === expected, user.auth.name, code); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯