Skip to content

Commit

Permalink
fix(typescript): use correct ts version and fix promise types
Browse files Browse the repository at this point in the history
Angular's compiler CLI requires typescript@^1.9.0-dev. The built-in
Promise typings for that version of	TS don't play well with Firebase
Promise when using Promise.resolve, so the firebase_sdk_auth_backend
has a function that will cast the promises as the correct type.
  • Loading branch information
jeffbcross committed Sep 16, 2016
1 parent 71ea5e6 commit 90ccc2d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"traceur": "0.0.96",
"tsd": "^0.6.5",
"typedoc": "github:jeffbcross/typedoc",
"typescript": "next",
"typescript": "^2.0.2",
"typings": "^1.3.2",
"zone.js": "^0.6.21"
},
Expand Down
21 changes: 13 additions & 8 deletions src/auth/firebase_sdk_auth_backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class FirebaseSdkAuthBackend extends AuthBackend {
}

createUser(creds: EmailPasswordCredentials): Promise<FirebaseAuthState> {
return Promise.resolve(this._fbAuth.createUserWithEmailAndPassword(creds.email, creds.password))
return castPromise<firebase.User>(this._fbAuth.createUserWithEmailAndPassword(creds.email, creds.password))
.then((user: firebase.User) => authDataToAuthState(user));
}

Expand Down Expand Up @@ -64,17 +64,17 @@ export class FirebaseSdkAuthBackend extends AuthBackend {
}

authWithCustomToken(token: string): Promise<FirebaseAuthState> {
return Promise.resolve(this._fbAuth.signInWithCustomToken(token))
return castPromise<firebase.User>((this._fbAuth.signInWithCustomToken(token)))
.then((user: firebase.User) => authDataToAuthState(user));
}

authAnonymously(): Promise<FirebaseAuthState> {
return Promise.resolve(this._fbAuth.signInAnonymously())
return castPromise<firebase.User>(this._fbAuth.signInAnonymously())
.then((user: firebase.User) => authDataToAuthState(user));
}

authWithPassword(creds: EmailPasswordCredentials): Promise<FirebaseAuthState> {
return Promise.resolve(this._fbAuth.signInWithEmailAndPassword(creds.email, creds.password))
return castPromise<firebase.User>(this._fbAuth.signInWithEmailAndPassword(creds.email, creds.password))
.then((user: firebase.User) => authDataToAuthState(user));
}

Expand All @@ -83,7 +83,7 @@ export class FirebaseSdkAuthBackend extends AuthBackend {
if (options.scope) {
options.scope.forEach(scope => providerFromFirebase.addScope(scope));
}
return Promise.resolve(this._fbAuth.signInWithPopup(providerFromFirebase));
return castPromise<firebase.auth.UserCredential>(this._fbAuth.signInWithPopup(providerFromFirebase));
}

/**
Expand All @@ -92,16 +92,16 @@ export class FirebaseSdkAuthBackend extends AuthBackend {
* You should subscribe to the FirebaseAuth object to listen succesful login
*/
authWithOAuthRedirect(provider: AuthProviders, options?: any): Promise<void> {
return Promise.resolve(this._fbAuth.signInWithRedirect(this._enumToAuthProvider(provider)));
return castPromise<void>(this._fbAuth.signInWithRedirect(this._enumToAuthProvider(provider)));
}

authWithOAuthToken(credential: firebase.auth.AuthCredential): Promise<FirebaseAuthState> {
return Promise.resolve(this._fbAuth.signInWithCredential(credential))
return castPromise<firebase.User>(this._fbAuth.signInWithCredential(credential))
.then((user: firebase.User) => authDataToAuthState(user));
}

getRedirectResult(): Observable<firebase.auth.UserCredential> {
return Observable.fromPromise(Promise.resolve(this._fbAuth.getRedirectResult()));
return Observable.fromPromise(castPromise<firebase.auth.UserCredential>(this._fbAuth.getRedirectResult()));
}

private _enumToAuthProvider(providerId: AuthProviders): any {
Expand All @@ -119,3 +119,8 @@ export class FirebaseSdkAuthBackend extends AuthBackend {
}
}
}

// Cast Firebase promises as Zone-patched Promises
function castPromise<T>(promiseLike: PromiseLike<T>): Promise<T> {
return Promise.resolve(promiseLike) as Promise<T>;
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"node_modules/zone.js/dist/zone.js.d.ts"
],
"angularCompilerOptions": {
"skipTemplateCodegen": true
"skipTemplateCodegen": true,
"strictMetadataEmit": true
}
}
3 changes: 2 additions & 1 deletion tsconfig.publish.es5.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"node_modules/zone.js/dist/zone.js.d.ts"
],
"angularCompilerOptions": {
"skipTemplateCodegen": true
"skipTemplateCodegen": true,
"strictMetadataEmit": true
}
}
4 changes: 3 additions & 1 deletion tsconfig.publish.es6.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"es2015",
"dom"
],
"skipLibCheck": true,
"moduleResolution": "node"
},
"files": [
Expand All @@ -25,6 +26,7 @@
"node_modules/zone.js/dist/zone.js.d.ts"
],
"angularCompilerOptions": {
"skipTemplateCodegen": true
"skipTemplateCodegen": true,
"strictMetadataEmit": true
}
}

0 comments on commit 90ccc2d

Please sign in to comment.