Last active
June 10, 2021 19:30
-
-
Save coxato/6c19264d59e72e647ad39585d17289a8 to your computer and use it in GitHub Desktop.
Easy firebase (manage firebase easy)
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
import { auth, firebase } from './init'; | |
import { dbApi } from './dbApi'; | |
class AuthMethods{ | |
async signup({ firstname, lastname, email, password, phone }){ | |
try { | |
const { user } = await auth.createUserWithEmailAndPassword(email, password); | |
await dbApi.setDocument('users', user.uid, { | |
firstname, | |
lastname, | |
phone, | |
email, | |
uid: user.uid | |
}); | |
return { ok: true, msg: '' }; | |
} catch ({code}) { | |
console.error("[error signup user] " + code); | |
let msg = 'Error'; | |
if(code === 'auth/email-already-in-use'){ | |
msg = "Email ya en uso" | |
} | |
return { ok: false, msg }; | |
} | |
} | |
async login({email, password}){ | |
try { | |
await auth.signInWithEmailAndPassword(email, password); | |
return { ok: true, msg: '' }; | |
} catch ({code}) { | |
console.error("[error signup user] " + code); | |
let msg = 'Error'; | |
// todo put right code | |
if( | |
code === 'auth/wrong-password' | |
|| | |
code === 'auth/invalid-email' | |
|| | |
code === 'auth/user-not-found' | |
){ | |
msg = "Email o contraseña incorrectos" | |
} | |
return { ok: false, msg }; | |
} | |
} | |
async logout(){ | |
await auth.signOut(); | |
} | |
changePassword({oldPassword, newPassword}){ | |
return new Promise((resolve, reject) => { | |
const user = auth.currentUser; | |
// get email credential | |
const credential = firebase.auth.EmailAuthProvider.credential( | |
user.email, | |
oldPassword | |
); | |
// reauthenticate then change password | |
user.reauthenticateWithCredential(credential) | |
.then( userCredential => { | |
// update password | |
userCredential.user.updatePassword(newPassword) | |
.then(() => { | |
resolve({ | |
msg: 'password successfully updated', | |
errCode: null, | |
ok: true | |
}) | |
}) | |
.catch( err => { | |
reject({ | |
msg: 'password not updated ' + err.message, | |
errCode: err.code, | |
ok: false | |
}) | |
}) | |
}) | |
.catch( err => { | |
reject({ | |
msg: 'error with re-authentication ' + err.message, | |
errCode: err.code, | |
ok: false | |
}) | |
}) | |
}); | |
} | |
} | |
const authMethods = new AuthMethods(); | |
export { authMethods } |
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
import { db } from './init'; | |
class DBApi{ | |
// =================== get data =================== | |
// get just one document | |
async getDocument(collectionName, id = null){ | |
try { | |
let docRef; | |
if(id) docRef = db.collection(collectionName).doc(id); | |
else docRef = db.collection(collectionName); | |
let doc = await docRef.get(); | |
if( doc.exists ){ | |
return doc.data(); | |
}else{ | |
return null; | |
} | |
} catch (err) { | |
console.log(err.message) | |
return err; | |
} | |
} | |
// get all simple data | |
async getDocuments(collectionName){ | |
try { | |
const ref = db.collection(collectionName); | |
const documents = await ref.get(); | |
const documentsData = []; | |
// empty data | |
if(documents.empty) return null; | |
// with data, parse documents | |
documents.forEach( elmt => { | |
documentsData.push( | |
{ ...elmt.data(), id: elmt.id } | |
); | |
} | |
); | |
return documentsData; | |
} catch (err) { | |
console.log(err.message) | |
return err; | |
} | |
} | |
// specific queries | |
async getDocumentsByPropertys(collectionName, { property, operator, value }, { limit = null, orderBy = null, orderType = 'asc' }){ | |
try { | |
let docRef = db.collection(collectionName).where(property, operator, value); | |
if(limit) docRef = docRef.limit(limit); | |
if(orderBy) docRef = docRef.orderBy(orderBy, orderType); | |
let documents = await docRef.get(); | |
if(documents.empty){ | |
return null; | |
} | |
// return array with documents | |
let data = []; | |
documents.forEach( d => { | |
data.push(d.data()) | |
}); | |
return data; | |
} catch (err) { | |
console.log(err) | |
return err; | |
} | |
} | |
// specific multiple queries | |
/** | |
* | |
* @param {string} collectionName | |
* @param {array} propertiesArr [{ property, operator, value }] | |
* @returns | |
*/ | |
async getDocumentsByMultiplePropertys(collectionName, propertiesArr){ | |
try { | |
let query = db.collection(collectionName); | |
for(let { property, operator, value } of propertiesArr){ | |
query = query.where(property, operator, value); | |
} | |
let documents = await query.get(); | |
if(documents.empty){ | |
return null; | |
} | |
// return array with documents | |
let data = []; | |
documents.forEach( d => { | |
data.push(d.data()) | |
}); | |
return data; | |
} catch (err) { | |
console.log(err) | |
return err; | |
} | |
} | |
// ==================== insert data =================== | |
// with custom id | |
async setDocument(collectionName, customId, objectToSave ){ | |
try { | |
let docRef = db.collection(collectionName).doc(customId); | |
let inserted = await docRef.set(objectToSave); | |
return inserted; | |
} catch (err) { | |
console.log(err.message); | |
return err; | |
} | |
} | |
// with auto id | |
async addDocument(collectionName, objectToSave){ | |
try { | |
let docRef = db.collection(collectionName); | |
let inserted = await docRef.add(objectToSave); | |
return inserted; | |
} catch (err) { | |
console.log(err.message); | |
return err; | |
} | |
} | |
async addMany(collectionName, arrayObj){ | |
try{ | |
for(const doc of arrayObj){ | |
await this.addDocument(collectionName, doc); | |
} | |
console.log("documents added successfuly"); | |
return true; | |
}catch({message}){ | |
console.error(message); | |
return null; | |
} | |
} | |
// ============================== update ============================== | |
async updateDocument(collectionName, id, objectToUpdate){ | |
try { | |
let docRef = db.collection(collectionName).doc(id); | |
await docRef.update(objectToUpdate); | |
return 'document updated!'; | |
} catch (err) { | |
console.error(err.message); | |
throw new Error(err.message); | |
} | |
} | |
// ========================== delete ==================== | |
async deleteDocument(collectionName, id){ | |
try { | |
// console.log("la collecion y el id", collectionName, id) | |
await db.collection(collectionName).doc(id).delete(); | |
return true; | |
} catch (err) { | |
console.error(err.message); | |
return false; | |
} | |
} | |
} | |
// init and export | |
const dbApi = new DBApi(); | |
export { dbApi }; |
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
import { auth, db } from './init'; | |
import { dbApi } from './dbApi'; | |
import { authMethods } from './auth'; | |
export { | |
auth, | |
db, | |
authMethods, | |
dbApi | |
} |
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
import firebase from 'firebase/app'; | |
import 'firebase/firestore'; | |
import 'firebase/auth'; | |
import { config } from '../config'; | |
firebase.initializeApp(config.firebase); | |
const db = firebase.firestore(); | |
const auth = firebase.auth(); | |
export { | |
db, | |
auth, | |
firebase | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment