Skip to content
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

[WIP] Fix auth infinite loop with multi-tabs #262

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 51 additions & 7 deletions addon/authenticators/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { getOwner } from '@ember/application';

import { Auth, User, UserCredential } from 'firebase/auth';
import {
Auth,
User,
UserCredential,
UserInfo,
} from 'firebase/auth';
import BaseAuthenticator from 'ember-simple-auth/authenticators/base';

import {
Expand All @@ -15,17 +20,51 @@ interface AuthenticateCallback {
(auth: Auth): Promise<UserCredential>;
}

interface CherryPickedUser {
displayName: string | null;
email: string | null;
phoneNumber: string | null;
photoURL: string | null;
providerId: string;
uid: string;
emailVerified: boolean;
isAnonymous: boolean;
providerData: UserInfo[];
refreshToken: string;
tenantId: string | null;
}

interface AuthenticatedData {
user: CherryPickedUser;
}

function parseCherryPickedUser(user: User): CherryPickedUser {
return {
displayName: user.displayName,
email: user.email,
phoneNumber: user.phoneNumber,
photoURL: user.photoURL,
providerId: user.providerId,
uid: user.uid,
emailVerified: user.emailVerified,
isAnonymous: user.isAnonymous,
providerData: user.providerData,
refreshToken: user.refreshToken,
tenantId: user.tenantId,
};
}

export default class FirebaseAuthenticator extends BaseAuthenticator {
/* eslint-disable @typescript-eslint/no-explicit-any */
private get fastboot(): any {
return getOwner(this).lookup('service:fastboot');
}

public async authenticate(callback: AuthenticateCallback): Promise<{ user: User | null }> {
public async authenticate(callback: AuthenticateCallback): Promise<AuthenticatedData> {
const auth = getAuth();
const credential = await callback(auth);

return { user: credential.user };
return { user: parseCherryPickedUser(credential.user) };
}

public invalidate(): Promise<void> {
Expand All @@ -34,10 +73,15 @@ export default class FirebaseAuthenticator extends BaseAuthenticator {
return signOut(auth);
}

public restore(): Promise<{ user: User | null }> {
public restore(): Promise<AuthenticatedData> {
return new Promise((resolve, reject) => {
const auth = getAuth();

if (auth.currentUser) {
resolve({ user: parseCherryPickedUser(auth.currentUser) });
return;
}

if (
this.fastboot?.isFastBoot
&& this.fastboot.request.headers.get('Authorization')?.startsWith('Bearer ')
Expand All @@ -46,7 +90,7 @@ export default class FirebaseAuthenticator extends BaseAuthenticator {

if (token) {
signInWithCustomToken(auth, token).then((credential) => {
resolve({ user: credential.user });
resolve({ user: parseCherryPickedUser(credential.user) });
}).catch(() => {
reject();
});
Expand All @@ -58,11 +102,11 @@ export default class FirebaseAuthenticator extends BaseAuthenticator {
unsubscribe();

if (user) {
resolve({ user });
resolve({ user: parseCherryPickedUser(user) });
} else {
getRedirectResult(auth).then((credential) => {
if (credential) {
resolve({ user: credential.user });
resolve({ user: parseCherryPickedUser(credential.user) });
} else {
reject();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/app/controllers/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class ApplicationController extends Controller {
login(): void {
this.session.authenticate('authenticator:firebase', (auth: Auth) => (
createUserWithEmailAndPassword(auth, '[email protected]', 'foobar')
).then((credential) => credential.user).catch(() => (
).then((credential) => credential).catch(() => (
signInWithEmailAndPassword(auth, '[email protected]', 'foobar')
)));
}
Expand Down
20 changes: 18 additions & 2 deletions tests/unit/authenticators/firebase-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,26 @@ module('Unit | Authenticator | firebase', function (hooks) {
const authenticator = this.owner.lookup('authenticator:firebase');

// Act
const result = await authenticator.authenticate(() => Promise.resolve({ user: 'foo' }));
const result = await authenticator.authenticate(() => Promise.resolve({
user: { displayName: 'foo' },
}));

// Assert
assert.deepEqual(result, { user: 'foo' });
assert.deepEqual(result, {
user: {
displayName: 'foo',
email: undefined,
emailVerified: undefined,
isAnonymous: undefined,
phoneNumber: undefined,
photoURL: undefined,
providerData: undefined,
providerId: undefined,
refreshToken: undefined,
tenantId: undefined,
uid: undefined,
},
});
});
});

Expand Down