From 5340bdfbd6fafada263c98325c53bab2beb207a5 Mon Sep 17 00:00:00 2001 From: sudjoao Date: Wed, 4 Oct 2023 13:59:03 -0300 Subject: [PATCH] feat: use new firebase methods --- app/src/services/Api.js | 4 +-- app/src/services/Firebase.js | 56 ++++++++++++++++++------------------ 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/app/src/services/Api.js b/app/src/services/Api.js index 879c36b2..4011d7ef 100644 --- a/app/src/services/Api.js +++ b/app/src/services/Api.js @@ -11,8 +11,8 @@ const api = axios.create({ api.interceptors.request.use( async (config) => { let accessToken = await AsyncStorage.getItem('accessToken'); - - if (accessToken) { + const user = await firebaseService.getCurrentUser(); + if (user && accessToken) { const expireDate = jwt_decode(accessToken).exp; const now = Date.now() / 1000; diff --git a/app/src/services/Firebase.js b/app/src/services/Firebase.js index 8cdc7e4c..cc0defc7 100644 --- a/app/src/services/Firebase.js +++ b/app/src/services/Firebase.js @@ -1,5 +1,14 @@ -import firebase from 'firebase/compat/app'; -import 'firebase/compat/auth'; +import { initializeApp } from 'firebase/app'; +import { + getAuth, + signInWithEmailAndPassword, + sendEmailVerification, + sendPasswordResetEmail, + getIdToken, + signOut, + signInWithCredential, + onAuthStateChanged, +} from 'firebase/auth'; import Constants from 'expo-constants'; import authConfig from '../config/authmiaajuda-firebase'; import authConfigDev from '../config/authmiaajuda-firebase-dev'; @@ -9,58 +18,49 @@ class FirebaseService { const env = Constants.manifest.releaseChannel; const { apiKey, authDomain, projectId } = env == 'prod' ? authConfig : authConfigDev; - this.firebase = firebase.initializeApp({ + this.app = initializeApp({ apiKey, authDomain, projectId, }); + this.auth = getAuth(); } isEmailVerified() { - return this.firebase.auth().currentUser.emailVerified; + return this.auth.currentUser.emailVerified; } async login(email, password) { - return await this.firebase - .auth() - .signInWithEmailAndPassword(email, password); + return await signInWithEmailAndPassword(this.auth, email, password); } + async sendEmailVerification() { - await this.firebase.auth().currentUser.sendEmailVerification(); + await sendEmailVerification(this.auth.currentUser); } async getUserId() { - return await this.firebase.auth().currentUser?.getIdToken(); + return await getIdToken(this.auth.currentUser); } + async getCurrentUser() { - return this.firebase.auth().currentUser; + return this.auth.currentUser; } + async resetUserPassword(email) { - await this.firebase.auth().sendPasswordResetEmail(email); + await sendPasswordResetEmail(this.auth, email); return true; } - async setPersistence() { - await this.firebase - .auth() - .setPersistence(this.firebase.auth.Auth.Persistence.LOCAL); - } - async getCredentialFacebook(token) { - return await this.firebase.auth.FacebookAuthProvider.credential(token); - } + async signInWithCredential(credential) { - return await this.firebase.auth().signInWithCredential(credential); - } - async getCredentialGoogle(idToken, accessToken) { - return await this.firebase.auth.GoogleAuthProvider.credential( - idToken, - accessToken, - ); + return await signInWithCredential(this.auth, credential); } + async signOut() { - await this.firebase.auth().signOut(); + await signOut(this.auth); } + async onAuthStateChanged(callbackfunction) { - this.firebase.auth().onAuthStateChanged(callbackfunction); + onAuthStateChanged(this.auth, callbackfunction); } }