This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#548 Compatibility with the Firebase Web API
- Loading branch information
1 parent
7a9cc9f
commit 48c609d
Showing
9 changed files
with
2,337 additions
and
82 deletions.
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 |
---|---|---|
|
@@ -2,9 +2,11 @@ import { Observable } from "tns-core-modules/data/observable"; | |
import { alert, prompt } from "tns-core-modules/ui/dialogs"; | ||
import { ios as iosUtils } from "tns-core-modules/utils/utils"; | ||
import { isIOS } from "tns-core-modules/platform"; | ||
import { AddEventListenerResult } from "nativescript-plugin-firebase"; | ||
import { AddEventListenerResult, User } from "nativescript-plugin-firebase"; | ||
import * as fs from "tns-core-modules/file-system"; | ||
|
||
const firebase = require("nativescript-plugin-firebase"); | ||
const firebaseWebApi = require("nativescript-plugin-firebase/app"); | ||
|
||
declare const assert: any; | ||
|
||
|
@@ -13,6 +15,223 @@ export class HelloWorldModel extends Observable { | |
public userEmailOrPhone: string; | ||
private userListenerWrapper: AddEventListenerResult; | ||
private companiesListenerWrapper: AddEventListenerResult; | ||
private onAuthStateChangedHandlerSet = false; | ||
|
||
|
||
/*********************************************** | ||
* Web API usage examples | ||
***********************************************/ | ||
|
||
private ensureWebOnAuthChangedHandler(): void { | ||
if (!this.onAuthStateChangedHandlerSet) { | ||
this.onAuthStateChangedHandlerSet = true; | ||
firebaseWebApi.auth().onAuthStateChanged((user?: User) => { | ||
console.log(">> auth state changed: " + user); | ||
if (user) { | ||
this.set("userEmailOrPhone", user.email ? user.email : (user.phoneNumber ? user.phoneNumber : "N/A")); | ||
alert({ | ||
title: "User signed in", | ||
message: JSON.stringify(user), | ||
okButtonText: "Nice!" | ||
}); | ||
} else { | ||
alert({ | ||
title: "User signed out", | ||
okButtonText: "Bye!" | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public doWebInit(): void { | ||
firebaseWebApi.initializeApp(); | ||
} | ||
|
||
public doWebLoginAnonymously(): void { | ||
this.ensureWebOnAuthChangedHandler(); | ||
firebaseWebApi.auth().signInAnonymously() | ||
.catch(err => { | ||
alert({ | ||
title: "Login error", | ||
message: JSON.stringify(err), | ||
okButtonText: "OK, pity" | ||
}); | ||
} | ||
); | ||
} | ||
|
||
public doWebLoginByPassword(): void { | ||
this.ensureWebOnAuthChangedHandler(); | ||
firebaseWebApi.auth().signInWithEmailAndPassword('[email protected]', 'firebase') | ||
.catch(err => { | ||
alert({ | ||
title: "Login error", | ||
message: JSON.stringify(err), | ||
okButtonText: "OK, pity" | ||
}); | ||
} | ||
); | ||
} | ||
|
||
public doWebFetchProvidersForEmail(): void { | ||
const user = firebaseWebApi.auth().currentUser; | ||
if (!user || !user.email) { | ||
alert({ | ||
title: "Can't fetch providers", | ||
message: "No user with emailaddress logged in.", | ||
okButtonText: "OK, makes sense.." | ||
}); | ||
return; | ||
} | ||
|
||
firebaseWebApi.auth().fetchProvidersForEmail(user.email).then( | ||
result => { | ||
alert({ | ||
title: `Providers for ${user.email}`, | ||
message: JSON.stringify(result), // likely to be ["password"] | ||
okButtonText: "Thanks!" | ||
}); | ||
}, | ||
errorMessage => { | ||
alert({ | ||
title: "Fetch Providers for Email error", | ||
message: errorMessage, | ||
okButtonText: "OK, pity.." | ||
}); | ||
} | ||
); | ||
} | ||
|
||
public doWebLogout(): void { | ||
firebaseWebApi.auth().signOut() | ||
.then(() => { | ||
this.set("userEmailOrPhone", null); | ||
alert({ | ||
title: "Logout OK", | ||
okButtonText: "OK, bye!" | ||
}); | ||
}) | ||
.catch(error => { | ||
alert({ | ||
title: "Logout error", | ||
message: JSON.stringify(error), | ||
okButtonText: "Hmmkay" | ||
}); | ||
} | ||
); | ||
} | ||
|
||
public doWebCreateUser(): void { | ||
firebaseWebApi.auth().createUserWithEmailAndPassword('[email protected]', 'firebase') | ||
.then(result => { | ||
alert({ | ||
title: "User created", | ||
message: JSON.stringify(result), | ||
okButtonText: "Nice!" | ||
}); | ||
}) | ||
.catch( | ||
error => { | ||
alert({ | ||
title: "No user created", | ||
message: JSON.stringify(error), | ||
okButtonText: "OK, got it" | ||
}); | ||
} | ||
); | ||
} | ||
|
||
public doWebGetCurrentUser(): void { | ||
const user = firebaseWebApi.auth().currentUser; | ||
if (user) { | ||
alert({ | ||
title: "Current user", | ||
message: JSON.stringify(user), | ||
okButtonText: "Nice!" | ||
}); | ||
} else { | ||
alert({ | ||
title: "No current user", | ||
okButtonText: "OK, thanks" | ||
}); | ||
} | ||
} | ||
|
||
public doWebAddValueEventListenerForCompanies(): void { | ||
const path = "/companies"; | ||
const onValueEvent = result => { | ||
if (result.error) { | ||
alert({ | ||
title: "Listener error", | ||
message: result.error, | ||
okButtonText: "Darn!" | ||
}); | ||
} else { | ||
this.set("path", path); | ||
this.set("key", result.key); | ||
this.set("value", JSON.stringify(result.val())); | ||
} | ||
}; | ||
|
||
firebaseWebApi.database().ref(path).on("value", onValueEvent); | ||
} | ||
|
||
public doWebRemoveValueEventListenersForCompanies(): void { | ||
const path = "/companies"; | ||
firebaseWebApi.database().ref(path).off("value"); | ||
} | ||
|
||
public doWebGetValueForCompanies(): void { | ||
const path = "/companies"; | ||
firebaseWebApi.database().ref(path) | ||
.once("value") | ||
.then(result => { | ||
this.set("path", path); | ||
this.set("key", result.key); | ||
this.set("value", JSON.stringify(result.val())); | ||
}) | ||
.catch(error => console.log("doWebGetValueForCompanies error: " + error)); | ||
} | ||
|
||
public doWebStoreCompaniesBySetValue(): void { | ||
firebaseWebApi.database().ref("/companies") | ||
.set([ | ||
{ | ||
name: 'Telerik (web)', | ||
country: 'Bulgaria', | ||
since: 2000, | ||
updateTs: firebase.ServerValue.TIMESTAMP | ||
}, | ||
{ | ||
name: 'Google (web)', | ||
country: 'USA', | ||
since: 1900, | ||
updateTs: firebase.ServerValue.TIMESTAMP | ||
} | ||
] | ||
) | ||
.then(() => console.log("firebase.setValue done")) | ||
.catch(error => console.log("firebase.setValue error: " + error)); | ||
} | ||
|
||
public doWebRemoveCompanies(): void { | ||
firebaseWebApi.database().ref("/companies").remove() | ||
.then(() => console.log("firebase.remove done")) | ||
.catch((err) => console.log("firebase.remove error: " + err)); | ||
} | ||
|
||
public doWebQueryBulgarianCompanies(): void { | ||
const path = "/companies"; | ||
const child = "name"; | ||
firebaseWebApi.database().ref(path).orderByChild(child); | ||
} | ||
|
||
|
||
|
||
/*********************************************** | ||
* Native API usage examples | ||
***********************************************/ | ||
|
||
public doInit(): void { | ||
firebase.init({ | ||
|
@@ -724,6 +943,21 @@ export class HelloWorldModel extends Observable { | |
); | ||
} | ||
|
||
public doGetValueForCompanies(): void { | ||
firebase.getValue('/companies').then( | ||
result => { | ||
alert({ | ||
title: "Value retrieved", | ||
message: JSON.stringify(result), | ||
okButtonText: "OK" | ||
}); | ||
}, | ||
error => { | ||
console.log("doGetValueForCompanies error: " + error); | ||
} | ||
); | ||
} | ||
|
||
public doUserStoreByPush(): void { | ||
firebase.push( | ||
'/users', | ||
|
This file contains 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 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,80 @@ | ||
import * as firebase from "../../firebase"; | ||
import { LoginType, User } from "../../firebase"; | ||
|
||
export module auth { | ||
export class Auth { | ||
private authStateChangedHandler; | ||
public currentUser: User; | ||
|
||
public onAuthStateChanged(handler: (user: User) => void): void { | ||
this.authStateChangedHandler = handler; | ||
console.log(">> added onAuthStateChanged handler"); | ||
}; | ||
|
||
public signOut(): Promise<any> { | ||
return new Promise((resolve, reject) => { | ||
firebase.logout() | ||
.then(() => { | ||
this.currentUser = undefined; | ||
this.authStateChangedHandler && this.authStateChangedHandler(); | ||
resolve(); | ||
}) | ||
.catch(err => { | ||
reject({ | ||
// code: "", | ||
message: err | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
public signInWithEmailAndPassword(email: string, password: string): Promise<any> { | ||
return new Promise((resolve, reject) => { | ||
firebase.login({ | ||
type: LoginType.PASSWORD, | ||
passwordOptions: { | ||
email: email, | ||
password: password | ||
} | ||
}).then((user: User) => { | ||
this.currentUser = user; | ||
this.authStateChangedHandler && this.authStateChangedHandler(user); | ||
resolve(); | ||
}, (err => { | ||
reject({ | ||
// code: "", | ||
message: err | ||
}); | ||
})); | ||
}); | ||
} | ||
|
||
public createUserWithEmailAndPassword(email: string, password: string): Promise<any> { | ||
return firebase.createUser({ | ||
email: email, | ||
password: password | ||
}); | ||
} | ||
|
||
public signInAnonymously(): Promise<any> { | ||
return new Promise((resolve, reject) => { | ||
firebase.login({ | ||
type: LoginType.ANONYMOUS | ||
}).then((user: User) => { | ||
this.currentUser = user; | ||
this.authStateChangedHandler && this.authStateChangedHandler(user); | ||
resolve(); | ||
}, (err => { | ||
reject({ | ||
// code: "", | ||
message: err | ||
}); | ||
})); | ||
}); | ||
} | ||
|
||
public fetchProvidersForEmail(email: string): Promise<any> { | ||
return firebase.fetchProvidersForEmail(email); | ||
} | ||
} | ||
} |
Oops, something went wrong.