Skip to content

Instantly share code, notes, and snippets.

@hashrock
Created August 6, 2016 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hashrock/90c33155d6e55d08d46907a55e8da6f4 to your computer and use it in GitHub Desktop.
Save hashrock/90c33155d6e55d08d46907a55e8da6f4 to your computer and use it in GitHub Desktop.
Junk firebase wrapper
{
"database": {
"rules": "database-rules.json"
},
"storage": {
"rules": "storage.rules"
},
"hosting": {
"public": "./",
"ignore": [
"firebase.json",
"database-rules.json",
"storage.rules",
"**/node_modules/**"
]
}
}
declare var firebase: any;
export class Junk {
auth: any
database: any
storage: any
messagesRef: any
static LOADING_IMAGE_URL: string = 'https://www.google.com/images/spin-32.gif';
constructor(onAuthStateChanged: OnAuthStateChangedCallback) {
this.auth = firebase.auth();
this.database = firebase.database();
this.storage = firebase.storage();
// Initiates Firebase auth and listen to auth state changes.
this.auth.onAuthStateChanged(onAuthStateChanged);
};
fetch(num: number, cb: FetchCallback) {
// Reference to the /messages/ database path.
this.messagesRef = this.database.ref('messages');
// Make sure we remove all previous listeners.
this.messagesRef.off();
this.messagesRef.limitToLast(num).on('child_added', cb);
this.messagesRef.limitToLast(num).on('child_changed', cb);
}
login() {
// Sign in Firebase using popup auth and Google as the identity provider.
var provider = new firebase.auth.GoogleAuthProvider();
this.auth.signInWithPopup(provider);
}
logout() {
this.auth.signOut();
}
post(text: string) {
var currentUser = this.auth.currentUser;
return this.messagesRef.push({
user_id: currentUser.uid,
name: currentUser.displayName,
text: text,
photoUrl: currentUser.photoURL || '/images/profile_placeholder.png'
})
}
private getRefFromURL(imageUri: string) {
return this.storage.refFromURL(imageUri)
}
postTemp() {
var currentUser = this.auth.currentUser;
return this.messagesRef.push({
user_id: currentUser.uid,
name: currentUser.displayName,
imageUrl: Junk.LOADING_IMAGE_URL,
photoUrl: currentUser.photoURL || '/images/profile_placeholder.png'
})
}
upload(file: File, cb: UploadCallback) {
// Upload the image to Firebase Storage.
var currentUser = this.auth.currentUser;
var uploadTask = this.storage.ref(currentUser.uid + '/' + Date.now() + '/' + file.name)
.put(file, { 'contentType': file.type });
// Listen for upload completion.
uploadTask.on('state_changed', null, function (error: any) {
console.error('There was an error uploading a file to Firebase Storage:', error);
}, function () {
cb(uploadTask.snapshot.metadata.fullPath);
});
}
get user() {
return this.auth.currentUser;
}
private getImageUrl(filePath: string) {
return this.storage.ref(filePath).toString()
}
fetchImage(imageUri: any, start: FetchImageCallback, loaded: FetchImageCallback){
if (imageUri.startsWith('gs://')) {
start(Junk.LOADING_IMAGE_URL)
this.getRefFromURL(imageUri).getMetadata().then(function (metadata: any) {
loaded(metadata.downloadURLs[0])
});
} else {
loaded(imageUri)
}
}
}
interface UploadCallback { (filePath: string): void }
interface FetchCallback { (): JunkItem }
class JunkItemBody{
user_id: string
name: string
text: string
photoUrl: string
imageUrl: string
}
interface JunkItemValue { (): JunkItemBody }
export class JunkItem{
key: string
val: JunkItemValue
}
interface OnAuthStateChangedCallback {(): JunkUser}
export class JunkUser{
photoURL: string
displayName: string
}
interface FetchImageCallback {(src:string): void}
export class JunkUtil{
static isImage(file: File):boolean{
return !!file.type.match('image.*')
}
}
service firebase.storage {
match /b/<STORAGE ID HERE>/o {
match /{userId}/{timestamp}/{fileName} {
allow write: if request.auth.uid == userId;
allow read;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment