diff --git a/firebase/firebase-tests.ts b/firebase/firebase-tests.ts index eb6be83734..647ecb663d 100644 --- a/firebase/firebase-tests.ts +++ b/firebase/firebase-tests.ts @@ -11,6 +11,94 @@ dataRef.auth(AUTH_TOKEN, function(error, result) { } }); +// Log me in +dataRef.authWithCustomToken(AUTH_TOKEN, function(error, authData) { + if (error) { + console.log('Login Failed!', error); + } else { + console.log('Authenticated successfully with payload:', authData); + } +}); + +// Log me in +dataRef.authAnonymously(function(error, authData) { + if (error) { + console.log('Login Failed!', error); + } else { + console.log('Authenticated successfully with payload:', authData); + } +}); + +// Log me in +dataRef.authWithPassword({ + "email" : "bobtony@firebase.com", + "password" : "correcthorsebatterystaple" +}, function(error, authData) { + if (error) { + console.log('Login Failed!', error); + } else { + console.log('Authenticated successfully with payload:', authData); + } +}); + +// Log me in +dataRef.authWithOAuthPopup("twitter", function(error, authData) { + if (error) { + console.log('Login Failed!', error); + } else { + console.log('Authenticated successfully with payload:', authData); + } +}); + +// Log me in +dataRef.authWithOAuthRedirect("twitter", function(error) { + if (error) { + console.log('Login Failed!', error); + } else { + // We'll never get here, as the page will redirect on success. + } +}); + +// Authenticate with Facebook using an existing OAuth 2.0 access token +dataRef.authWithOAuthToken("facebook", "", function(error, authData) { + if (error) { + console.log('Login Failed!', error); + } else { + console.log('Authenticated successfully with payload:', authData); + } +}); +// Authenticate with Twitter using an existing OAuth 1.0a credential set +dataRef.authWithOAuthToken("twitter", { + "user_id" : "", + "oauth_token" : "", + "oauth_token_secret" : "", +}, function(error, authData) { + if (error) { + console.log('Login Failed!', error); + } else { + console.log('Authenticated successfully with payload:', authData); + } +}); + +var authData = dataRef.getAuth(); +if (authData) { + console.log('Authenticated user with uid:', authData.uid); +} + +var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com'); +firebaseRef.onAuth(function(authData) { + if (authData) { + console.log('Client is authenticated with uid ' + authData.uid); + } else { + // Client is unauthenticated + } +}); + +var onAuthChange = function(authData) { /*...*/ }; +firebaseRef.onAuth(onAuthChange); +// Sometime later... +firebaseRef.offAuth(onAuthChange); + //Time to log out! dataRef.unauth(); @@ -32,10 +120,64 @@ var sampleChatRef2 :Firebase= fredRef2.root(); var x3:string = sampleChatRef2.toString(); // x is now 'https://SampleChat.firebaseIO-demo.com'. +var fredRef = new Firebase("https://samplechat.firebaseio-demo.com/users/fred"); +var key = fredRef.key(); // key === "fred" +key = fredRef.child("name/last").key(); // key === "last" +key = fredRef.root().key(); // key === null, since fredRef refers to the root of the Firebase. + var fredRef3:Firebase = new Firebase('https://SampleChat.firebaseIO-demo.com/users/fred'); var x4:string = fredRef3.name(); // x is now 'fred'. +/* + * $set + */ +var fredNameRef = new Firebase('https://samplechat.firebaseio-demo.com/users/fred/name'); +fredNameRef.child('first').set('Fred'); +fredNameRef.child('last').set('Flintstone'); +// We've written 'Fred' to the Firebase location storing fred's first name, +// and 'Flintstone' to the location storing his last name + +fredNameRef.set({ first: 'Fred', last: 'Flintstone' }); +// Exact same effect as the previous example, except we've written +// fred's first and last name simultaneously + +var onComplete = function(error) { + if (error) { + console.log('Synchronization failed'); + } else { + console.log('Synchronization succeeded'); + } +}; +fredNameRef.set({ first: 'Fred', last: 'Flintstone' }, onComplete); +// Same as the previous example, except we will also log a message +// when the data has finished synchronizing + + +/* + * $update + */ +var fredNameRef = new Firebase('https://samplechat.firebaseio-demo.com/users/fred/name'); +// Modify the 'first' and 'last' children, but leave other data at fredNameRef unchanged +fredNameRef.update({ first: 'Fred', last: 'Flintstone' }); + +// Same as the previous example, except we will also display an alert +// message when the data has finished synchronizing. +var onComplete = function(error) { + if (error) { + console.log('Synchronization failed'); + } else { + console.log('Synchronization succeeded'); + } +}; +fredNameRef.update({ first: 'Wilma', last: 'Flintstone' }, onComplete); + +var fredRef = new Firebase('https://samplechat.firebaseio-demo.com/users/fred'); +//The following 2 function calls are equivalent +fredRef.update({ name: { first: 'Fred', last: 'Flintstone' }}); +fredRef.child('name').set({ first: 'Fred', last: 'Flintstone' }); + + // Increment Fred's rank by 1. var fredRankRef:Firebase = new Firebase('https://SampleChat.firebaseIO-demo.com/users/fred/rank'); fredRankRef.transaction(function(currentRank: number) { diff --git a/firebase/firebase.d.ts b/firebase/firebase.d.ts index bac32fbc63..796d14d59e 100644 --- a/firebase/firebase.d.ts +++ b/firebase/firebase.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Firebase API +// Type definitions for Firebase API 2.0.2 // Project: https://www.firebase.com/docs/javascript/firebase // Definitions by: Vincent Botone // Definitions: https://github.com/borisyankov/DefinitelyTyped @@ -43,16 +43,92 @@ interface IFirebaseQuery { } declare class Firebase implements IFirebaseQuery { + /** + * Constructs a new Firebase reference from a full Firebase URL. + */ constructor(firebaseURL: string); - auth(authToken: string, onComplete?: (error: any, result: IFirebaseAuthResult) => void, onCancel?:(error: any) => void): void; + /** + * @deprecated Use authWithCustomToken() instead. + * Authenticates a Firebase client using the provided authentication token or Firebase Secret. + */ + auth(authToken: string, onComplete?: (error: any, result: IFirebaseAuthResult) => void, onCancel?: (error: any) => void): void; + /** + * Authenticates a Firebase client using an authentication token or Firebase Secret. + */ + authWithCustomToken(autoToken: string, onComplete: (error: any, authData: IFirebaseAuthData) => void, options?:Object): void; + /** + * Authenticates a Firebase client using a new, temporary guest account. + */ + authAnonymously(onComplete: (error: any, authData: IFirebaseAuthData) => void, options?: Object): void; + /** + * Authenticates a Firebase client using an email / password combination. + */ + authWithPassword(credentials: IFirebaseCredentials, onComplete: (error: any, authData: IFirebaseAuthData) => void, options?: Object): void; + /** + * Authenticates a Firebase client using a popup-based OAuth flow. + */ + authWithOAuthPopup(provider: string, onComplete:(error: any, authData: IFirebaseAuthData) => void, options?: Object): void; + /** + * Authenticates a Firebase client using a redirect-based OAuth flow. + */ + authWithOAuthRedirect(provider: string, onComplete: (error: any) => void, options?: Object): void; + /** + * Authenticates a Firebase client using OAuth access tokens or credentials. + */ + authWithOAuthToken(provider: string, credentials: string, onComplete: (error: any, authData: IFirebaseAuthData) => void, options?: Object): void; + authWithOAuthToken(provider: string, credentials: Object, onComplete: (error: any, authData: IFirebaseAuthData) => void, options?: Object): void; + /** + * Synchronously access the current authentication state of the client. + */ + getAuth(): IFirebaseAuthData; + /** + * Listen for changes to the client's authentication state. + */ + onAuth(onComplete: (authData: IFirebaseAuthData) => void, context?: Object): void; + /** + * Detaches a callback previously attached with onAuth(). + */ + offAuth(onComplete: (authData: IFirebaseAuthData) => void, context?: Object): void; + /** + * Unauthenticates a Firebase client. + */ unauth(): void; + /** + * Gets a Firebase reference for the location at the specified relative path. + */ child(childPath: string): Firebase; + /** + * Gets a Firebase reference to the parent location. + */ parent(): Firebase; + /** + * Gets a Firebase reference to the root of the Firebase. + */ root(): Firebase; + /** + * Returns the last token in a Firebase location. + */ + key(): string; + /** + * @deprecated Use key() instead. + * Returns the last token in a Firebase location. + */ name(): string; + /** + * Gets the absolute URL corresponding to this Firebase reference's location. + */ toString(): string; + /** + * Writes data to this Firebase location. + */ set(value: any, onComplete?: (error: any) => void): void; - update(value: any, onComplete?: (error: any) => void): void; + /** + * Writes the enumerated children to this Firebase location. + */ + update(value: Object, onComplete?: (error: any) => void): void; + /** + * + */ remove(onComplete?: (error: any) => void): void; push(value: any, onComplete?: (error: any) => void): Firebase; setWithPriority(value: any, priority: string, onComplete?: (error: any) => void): void; @@ -73,3 +149,17 @@ declare class Firebase implements IFirebaseQuery { goOffline(): void; goOnline(): void; } + +// Reference: https://www.firebase.com/docs/web/api/firebase/getauth.html +interface IFirebaseAuthData { + uid: string; + provider: string; + token: string; + expires: number; + auth: Object; +} + +interface IFirebaseCredentials { + email: string; + password: string; +} \ No newline at end of file