-
Notifications
You must be signed in to change notification settings - Fork 945
add signInWithEmailAndPassword & signInWithEmailLink to auth-next #3209
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
5 commits
Select commit
Hold shift + click to select a range
366e9ef
Cleanup credential inheritance tree
avolkovi 56bcf85
Add signInWithEmailAndPassword and signInWithEmailLink to auth-next
avolkovi 37f78e9
j/k phone credential/provider are actually public
avolkovi 7d74696
Add tests
avolkovi 9162f00
PR feedback
avolkovi 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
94 changes: 94 additions & 0 deletions
94
packages-exp/auth-exp/src/core/credentials/anonymous.test.ts
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { ProviderId, SignInMethod } from '@firebase/auth-types-exp'; | ||
import * as mockFetch from '../../../test/mock_fetch'; | ||
import { expect, use } from 'chai'; | ||
import * as chaiAsPromised from 'chai-as-promised'; | ||
import { testAuth } from '../../../test/mock_auth'; | ||
import { Auth } from '../../model/auth'; | ||
import { AnonymousCredential } from './anonymous'; | ||
import { mockEndpoint } from '../../../test/api/helper'; | ||
import { Endpoint } from '../../api'; | ||
import { APIUserInfo } from '../../api/account_management/account'; | ||
|
||
use(chaiAsPromised); | ||
|
||
describe('core/credentials/anonymous', () => { | ||
let auth: Auth; | ||
let credential: AnonymousCredential; | ||
|
||
beforeEach(async () => { | ||
auth = await testAuth(); | ||
credential = new AnonymousCredential(); | ||
}); | ||
|
||
it('should have an anonymous provider', () => { | ||
expect(credential.providerId).to.eq(ProviderId.ANONYMOUS); | ||
}); | ||
|
||
it('should have an anonymous sign in method', () => { | ||
expect(credential.signInMethod).to.eq(SignInMethod.ANONYMOUS); | ||
}); | ||
|
||
describe('#toJSON', () => { | ||
it('throws', () => { | ||
expect(credential.toJSON).to.throw(Error); | ||
}); | ||
}); | ||
|
||
describe('#_getIdTokenResponse', () => { | ||
const serverUser: APIUserInfo = { | ||
localId: 'local-id' | ||
}; | ||
|
||
beforeEach(() => { | ||
mockFetch.setUp(); | ||
mockEndpoint(Endpoint.SIGN_UP, { | ||
idToken: 'id-token', | ||
refreshToken: 'refresh-token', | ||
expiresIn: '1234', | ||
localId: serverUser.localId! | ||
}); | ||
}); | ||
afterEach(mockFetch.tearDown); | ||
|
||
it('calls signUp', async () => { | ||
const idTokenResponse = await credential._getIdTokenResponse(auth); | ||
expect(idTokenResponse.idToken).to.eq('id-token'); | ||
expect(idTokenResponse.refreshToken).to.eq('refresh-token'); | ||
expect(idTokenResponse.expiresIn).to.eq('1234'); | ||
expect(idTokenResponse.localId).to.eq(serverUser.localId); | ||
}); | ||
}); | ||
|
||
describe('#_linkToIdToken', () => { | ||
it('throws', async () => { | ||
await expect( | ||
credential._linkToIdToken(auth, 'id-token') | ||
).to.be.rejectedWith(Error); | ||
}); | ||
}); | ||
|
||
describe('#_matchIdTokenWithUid', () => { | ||
it('throws', () => { | ||
expect(() => credential._matchIdTokenWithUid(auth, 'other-uid')).to.throw( | ||
Error | ||
); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { ProviderId, SignInMethod } from '@firebase/auth-types-exp'; | ||
import { signUp } from '../../api/authentication/sign_up'; | ||
import { Auth } from '../../model/auth'; | ||
import { IdTokenResponse } from '../../model/id_token'; | ||
import { debugFail } from '../util/assert'; | ||
import { AuthCredential } from '.'; | ||
|
||
export class AnonymousCredential implements AuthCredential { | ||
providerId = ProviderId.ANONYMOUS; | ||
signInMethod = SignInMethod.ANONYMOUS; | ||
|
||
toJSON(): never { | ||
debugFail('Method not implemented.'); | ||
} | ||
|
||
static fromJSON(_json: object | string): AnonymousCredential | null { | ||
debugFail('Method not implemented'); | ||
} | ||
|
||
async _getIdTokenResponse(auth: Auth): Promise<IdTokenResponse> { | ||
return signUp(auth, { | ||
returnSecureToken: true | ||
}); | ||
} | ||
|
||
async _linkToIdToken(_auth: Auth, _idToken: string): Promise<never> { | ||
debugFail("Can't link to an anonymous credential"); | ||
} | ||
|
||
_matchIdTokenWithUid(_auth: Auth, _uid: string): Promise<never> { | ||
debugFail('Method not implemented.'); | ||
} | ||
} |
169 changes: 169 additions & 0 deletions
169
packages-exp/auth-exp/src/core/credentials/email.test.ts
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 |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { ProviderId, SignInMethod } from '@firebase/auth-types-exp'; | ||
import { expect, use } from 'chai'; | ||
import * as chaiAsPromised from 'chai-as-promised'; | ||
import { testAuth } from '../../../test/mock_auth'; | ||
import { Auth } from '../../model/auth'; | ||
import { EmailAuthProvider } from '../providers/email'; | ||
import { EmailAuthCredential } from './email'; | ||
import * as mockFetch from '../../../test/mock_fetch'; | ||
import { mockEndpoint } from '../../../test/api/helper'; | ||
import { Endpoint } from '../../api'; | ||
import { APIUserInfo } from '../../api/account_management/account'; | ||
|
||
use(chaiAsPromised); | ||
|
||
describe('core/credentials/email', () => { | ||
let auth: Auth; | ||
let apiMock: mockFetch.Route; | ||
const serverUser: APIUserInfo = { | ||
localId: 'local-id' | ||
}; | ||
|
||
beforeEach(async () => { | ||
auth = await testAuth(); | ||
}); | ||
|
||
context('email & password', () => { | ||
const credential = new EmailAuthCredential( | ||
'some-email', | ||
'some-password', | ||
EmailAuthProvider.EMAIL_PASSWORD_SIGN_IN_METHOD | ||
); | ||
|
||
beforeEach(() => { | ||
mockFetch.setUp(); | ||
apiMock = mockEndpoint(Endpoint.SIGN_IN_WITH_PASSWORD, { | ||
idToken: 'id-token', | ||
refreshToken: 'refresh-token', | ||
expiresIn: '1234', | ||
localId: serverUser.localId! | ||
}); | ||
}); | ||
afterEach(mockFetch.tearDown); | ||
|
||
it('should have an email provider', () => { | ||
expect(credential.providerId).to.eq(ProviderId.PASSWORD); | ||
}); | ||
|
||
it('should have an anonymous sign in method', () => { | ||
expect(credential.signInMethod).to.eq(SignInMethod.EMAIL_PASSWORD); | ||
}); | ||
|
||
describe('#toJSON', () => { | ||
it('throws', () => { | ||
expect(credential.toJSON).to.throw(Error); | ||
}); | ||
}); | ||
|
||
describe('#_getIdTokenResponse', () => { | ||
it('call sign in with password', async () => { | ||
const idTokenResponse = await credential._getIdTokenResponse(auth); | ||
expect(idTokenResponse.idToken).to.eq('id-token'); | ||
expect(idTokenResponse.refreshToken).to.eq('refresh-token'); | ||
expect(idTokenResponse.expiresIn).to.eq('1234'); | ||
expect(idTokenResponse.localId).to.eq(serverUser.localId); | ||
expect(apiMock.calls[0].request).to.eql({ | ||
returnSecureToken: true, | ||
email: 'some-email', | ||
password: 'some-password' | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#_linkToIdToken', () => { | ||
it('throws', async () => { | ||
await expect( | ||
credential._linkToIdToken(auth, 'id-token') | ||
).to.be.rejectedWith(Error); | ||
}); | ||
}); | ||
|
||
describe('#_matchIdTokenWithUid', () => { | ||
it('throws', () => { | ||
expect(() => | ||
credential._matchIdTokenWithUid(auth, 'other-uid') | ||
).to.throw(Error); | ||
}); | ||
}); | ||
}); | ||
|
||
context('email link', () => { | ||
const credential = new EmailAuthCredential( | ||
'some-email', | ||
'oob-code', | ||
EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD | ||
); | ||
|
||
beforeEach(() => { | ||
mockFetch.setUp(); | ||
apiMock = mockEndpoint(Endpoint.SIGN_IN_WITH_EMAIL_LINK, { | ||
idToken: 'id-token', | ||
refreshToken: 'refresh-token', | ||
expiresIn: '1234', | ||
localId: serverUser.localId! | ||
}); | ||
}); | ||
afterEach(mockFetch.tearDown); | ||
|
||
it('should have an email provider', () => { | ||
expect(credential.providerId).to.eq(ProviderId.PASSWORD); | ||
}); | ||
|
||
it('should have an anonymous sign in method', () => { | ||
expect(credential.signInMethod).to.eq(SignInMethod.EMAIL_LINK); | ||
}); | ||
|
||
describe('#toJSON', () => { | ||
it('throws', () => { | ||
expect(credential.toJSON).to.throw(Error); | ||
}); | ||
}); | ||
|
||
describe('#_getIdTokenResponse', () => { | ||
it('call sign in with email link', async () => { | ||
const idTokenResponse = await credential._getIdTokenResponse(auth); | ||
expect(idTokenResponse.idToken).to.eq('id-token'); | ||
expect(idTokenResponse.refreshToken).to.eq('refresh-token'); | ||
expect(idTokenResponse.expiresIn).to.eq('1234'); | ||
expect(idTokenResponse.localId).to.eq(serverUser.localId); | ||
expect(apiMock.calls[0].request).to.eql({ | ||
email: 'some-email', | ||
oobCode: 'oob-code' | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#_linkToIdToken', () => { | ||
it('throws', async () => { | ||
await expect( | ||
credential._linkToIdToken(auth, 'id-token') | ||
).to.be.rejectedWith(Error); | ||
}); | ||
}); | ||
|
||
describe('#_matchIdTokenWithUid', () => { | ||
it('throws', () => { | ||
expect(() => | ||
credential._matchIdTokenWithUid(auth, 'other-uid') | ||
).to.throw(Error); | ||
}); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as externs from '@firebase/auth-types-exp'; | ||
import { signInWithPassword } from '../../api/authentication/email_and_password'; | ||
import { signInWithEmailLink } from '../../api/authentication/email_link'; | ||
import { Auth } from '../../model/auth'; | ||
import { IdTokenResponse } from '../../model/id_token'; | ||
import { AuthErrorCode, AUTH_ERROR_FACTORY } from '../errors'; | ||
import { EmailAuthProvider } from '../providers/email'; | ||
import { debugFail } from '../util/assert'; | ||
import { AuthCredential } from '.'; | ||
|
||
export class EmailAuthCredential implements AuthCredential { | ||
readonly providerId = EmailAuthProvider.PROVIDER_ID; | ||
|
||
constructor( | ||
readonly email: string, | ||
readonly password: string, | ||
readonly signInMethod: externs.SignInMethod | ||
) {} | ||
|
||
toJSON(): never { | ||
debugFail('Method not implemented.'); | ||
} | ||
|
||
static fromJSON(_json: object | string): EmailAuthCredential | null { | ||
debugFail('Method not implemented'); | ||
} | ||
|
||
async _getIdTokenResponse(auth: Auth): Promise<IdTokenResponse> { | ||
switch (this.signInMethod) { | ||
case EmailAuthProvider.EMAIL_PASSWORD_SIGN_IN_METHOD: | ||
return signInWithPassword(auth, { | ||
returnSecureToken: true, | ||
email: this.email, | ||
password: this.password | ||
}); | ||
case EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD: | ||
return signInWithEmailLink(auth, { | ||
email: this.email, | ||
oobCode: this.password | ||
}); | ||
default: | ||
throw AUTH_ERROR_FACTORY.create(AuthErrorCode.INTERNAL_ERROR, { | ||
avolkovi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
appName: auth.name | ||
}); | ||
} | ||
} | ||
|
||
async _linkToIdToken(_auth: Auth, _idToken: string): Promise<never> { | ||
debugFail('Method not implemented.'); | ||
} | ||
|
||
_matchIdTokenWithUid(_auth: Auth, _uid: string): Promise<never> { | ||
debugFail('Method not implemented.'); | ||
} | ||
} |
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.
what does prefixing it with
_
do?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.
required by linter when parameters are unused
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.
ah we should settle on which thing to do. I was doing
void <variable>
but your version seems cleaner