grocery-app

Форк
0
/
firebase.ts 
76 строк · 2.0 Кб
1
import { initializeApp } from 'firebase/app';
2
import {
3
  createUserWithEmailAndPassword,
4
  getAuth,
5
  signInWithEmailAndPassword,
6
  signOut,
7
  updateProfile,
8
} from 'firebase/auth';
9
import axios from 'axios';
10

11
// Initialize Firebase
12
const firebase = {
13
  apiKey: process.env.REACT_APP_API_KEY,
14
  authDomain: `${process.env.REACT_APP_PROJECT_ID}.firebaseapp.com`,
15
  projectId: process.env.REACT_APP_PROJECT_ID,
16
  storageBucket: `${process.env.REACT_APP_PROJECT_ID}.appspot.com`,
17
  messagingSenderId: process.env.REACT_APP_SENDER_ID,
18
  appId: process.env.REACT_APP_APP_ID,
19
};
20

21
const app = initializeApp(firebase);
22

23
// Initialize Firebase Authentication and get a reference to the service
24
const auth = getAuth(app);
25

26
export async function createUser(email, password, username) {
27
  const response = await createUserWithEmailAndPassword(auth, email, password);
28
  await updateProfile(auth.currentUser, { displayName: username });
29

30
  const user = await response.user;
31
  const token = await response.user.getIdToken();
32

33
  return {
34
    user,
35
    token,
36
  };
37
}
38

39
export async function login(email, password) {
40
  const response = await signInWithEmailAndPassword(auth, email, password);
41
  const user = await response.user;
42
  const token = await response.user.getIdToken();
43

44
  return {
45
    user,
46
    token,
47
  };
48
}
49

50
export async function logout() {
51
  await signOut(auth);
52
}
53

54
const dbUrl =
55
  'https://grocery-app-46fec-default-rtdb.europe-west1.firebasedatabase.app/';
56

57
export async function getCategories() {
58
  return await axios.get(dbUrl + 'categories.json').then((response) => {
59
    return response.data;
60
  });
61
}
62

63
export async function getDeals() {
64
  return await axios.get(dbUrl + 'deals.json').then((response) => {
65
    return response.data;
66
  });
67
}
68

69
export async function getProducts() {
70
  return await axios.get(dbUrl + 'products.json').then((response) => {
71
    return 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

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.