grocery-app
/
firebase.ts
76 строк · 2.0 Кб
1import { initializeApp } from 'firebase/app';
2import {
3createUserWithEmailAndPassword,
4getAuth,
5signInWithEmailAndPassword,
6signOut,
7updateProfile,
8} from 'firebase/auth';
9import axios from 'axios';
10
11// Initialize Firebase
12const firebase = {
13apiKey: process.env.REACT_APP_API_KEY,
14authDomain: `${process.env.REACT_APP_PROJECT_ID}.firebaseapp.com`,
15projectId: process.env.REACT_APP_PROJECT_ID,
16storageBucket: `${process.env.REACT_APP_PROJECT_ID}.appspot.com`,
17messagingSenderId: process.env.REACT_APP_SENDER_ID,
18appId: process.env.REACT_APP_APP_ID,
19};
20
21const app = initializeApp(firebase);
22
23// Initialize Firebase Authentication and get a reference to the service
24const auth = getAuth(app);
25
26export async function createUser(email, password, username) {
27const response = await createUserWithEmailAndPassword(auth, email, password);
28await updateProfile(auth.currentUser, { displayName: username });
29
30const user = await response.user;
31const token = await response.user.getIdToken();
32
33return {
34user,
35token,
36};
37}
38
39export async function login(email, password) {
40const response = await signInWithEmailAndPassword(auth, email, password);
41const user = await response.user;
42const token = await response.user.getIdToken();
43
44return {
45user,
46token,
47};
48}
49
50export async function logout() {
51await signOut(auth);
52}
53
54const dbUrl =
55'https://grocery-app-46fec-default-rtdb.europe-west1.firebasedatabase.app/';
56
57export async function getCategories() {
58return await axios.get(dbUrl + 'categories.json').then((response) => {
59return response.data;
60});
61}
62
63export async function getDeals() {
64return await axios.get(dbUrl + 'deals.json').then((response) => {
65return response.data;
66});
67}
68
69export async function getProducts() {
70return await axios.get(dbUrl + 'products.json').then((response) => {
71return response.data;
72});
73}
74
75// For more information on how to access Firebase in your project,
76// see the Firebase documentation: https://firebase.google.com/docs/web/setup#access-firebase
77