/
useFirebase.ts
99 lines (86 loc) · 3.19 KB
/
useFirebase.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { useState } from "react";
import firebase from "firebase/app";
import "firebase/auth";
import "firebase/functions";
import "firebase/storage";
import { Alert } from "react-native";
import * as Facebook from "expo-facebook";
import * as Google from "expo-google-app-auth";
import { keys } from "../../config/keys";
interface IFirebaseMedia {
name: string;
url: string
}
export const useFirebase = () => {
const [imageUrls, setImageUrls] = useState<IFirebaseMedia[]>();
const [audioUrls, setAudioUrls] = useState<IFirebaseMedia[]>();
const signUp = async (email: string, password: string) => {
try {
await firebase.auth().createUserWithEmailAndPassword(email, password);
const currentUser = firebase.auth().currentUser;
} catch ({ message }) {
Alert.alert(`Signup Error: ${message}`);
}
};
const signIn = async (email: string, password: string) => {
try {
await firebase.auth().signInWithEmailAndPassword(email, password);
} catch ({ message }) {
Alert.alert(`Login Error: ${message}`);
}
};
const signInWithGoogle = async () => {
try {
const result = await Google.logInAsync({
androidClientId: keys.ANDROID_CLIENT_ID,
iosClientId: keys.IOS_CLIENT_ID,
behavior: 'web',
scopes: ['profile', 'email']
});
if (result.type === 'success') {
await firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL);
const credential = firebase.auth.GoogleAuthProvider.credential(result.idToken, result.accessToken);
const googleProfileData = await firebase.auth().signInWithCredential(credential);
}
} catch ({ message }) {
Alert.alert(`Google Login Error: ${message}`);
}
};
const signInWithFacebook = async () => {
try {
await Facebook.initializeAsync(keys.FACEBOOK_APP_ID);
const result = await Facebook.logInWithReadPermissionsAsync({
permissions: ['public_profile', 'email']
});
if (result.type === 'success') {
await firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL);
const credential = firebase.auth.FacebookAuthProvider.credential(result.token);
const facebookProfileData = await firebase.auth().signInWithCredential(credential);
}
} catch ({ message }) {
Alert.alert(`Facebook Login Error: ${message}`);
}
};
const logOut = async () => {
try {
await firebase.auth().signOut();
} catch ({ message }) {
Alert.alert("There is an issue with the system");
}
};
const getMedia = async (mediaPath: string, mediaType: string) => {
try {
const mediaRef = firebase.storage().ref().child(mediaPath);
const mediaList = await mediaRef.listAll();
let mediaUrls = mediaList.items.map(async (itemRef) => {
return { name: itemRef.name, url: await itemRef.getDownloadURL() };
});
let mediaDownloads = Promise.all(mediaUrls);
let media = await mediaDownloads;
mediaType === 'image' ? setImageUrls(media) : setAudioUrls(media);
} catch ({ message }) {
//
}
};
return { signUp, signIn, signInWithGoogle, signInWithFacebook, logOut, getMedia, imageUrls, audioUrls };
};