import { Text, View, Pressable, TextInput, Alert, ScrollView} from ‘react-native’;…
- User
import { Text, View, Pressable, TextInput, Alert, ScrollView} from ‘react-native’; import Header from ‘…/components/Header’; import Footer from ‘…/components/Footer’; import { gStyle } from ‘…/styles/style’; import React, {useState} from ‘react’; import { firebase } from ‘…/Firebase/firebase’; import ‘firebase/compat/auth’; import ‘firebase/compat/database’; import ‘firebase/compat/firestore’;
export default function Registration() { const [name, setName] = useState(‘’); const [surname, setSurname]=useState(‘’); const [email, setEmail] = useState(‘’); const [phone, setPhone] = useState(‘’); const [password, setPassword] = useState(‘’); const [confirmPassword, setConfirmPassword]=useState(‘’); const handleRegistration=()=>{ if( name===‘’|| surname===‘’|| email===‘’|| phone===‘’|| password===‘’|| confirmPassword===‘’ ){ Alert.alert(‘Ошибка!’,‘Пожалуйста, заполните все поля для регистрации’); } if(password!==confirmPassword){ Alert.alert(‘Ошибка!’,‘Пароли не совпадают’); return; } firebase.auth() .createUserWithEmailAndPassword(email, password) .then((result)=>{ const userDetails={ name, surname, email, phone, }; firebase.database().ref(users/${result.user.uid}).set(userDetails); }) } return (
export default function Auth() { const navigation = useNavigation(); const [email, setEmail] = useState(‘’); const [phone, setPhone] = useState(‘’); const [password, setPassword] = useState(‘’); const [errorMessage, setErrorMessage] = React.useState(null); const handleLogin=()=>{ if (!email || !password) { Alert.alert(‘Ошибка!’,‘Неверная почта или пароль’); return; } firebase.auth().signInWithEmailAndPassword(email, password) .then(()=>{ navigation.navigate(‘Profile’); }) .catch((error)=>{ setErrorMessage(error.message); console.log(error); }) } return (
Войти в личный{“\n”}кабинет Почта Пароль Забыли пароль? Войти <Pressable onPress={()=>navigation.navigate(‘Registration’)}
Я не зарегистрирован(а)
thats my auth file import { Text, View, Image, ScrollView, Pressable } from ‘react-native’; import Header from ‘…/components/Header’; import Footer from ‘…/components/Footer’; import { gStyle } from ‘…/styles/style’; import { firebase } from ‘…/Firebase/firebase’; import ‘firebase/compat/auth’; import ‘firebase/compat/database’; import ‘firebase/compat/firestore’; import React, {useState, useEffect} from ‘react’;
export default function Profile() { useEffect(() => { const currentUserID = firebase.auth().currentUser.uid;
firebase .database() .ref(‘users/’ + currentUserID) .once(‘value’) .then((snapshot) => { const data = snapshot.val(); setUserData(data); }); }, []); const [userData, setUserData] = useState(null); return (
); }
thats profile How to do that user can see his info (name, surname, email, phone)?
- Assistant
To allow the user to see their information in the Profile screen, you need to retrieve it from the Firebase database and set it in the state. You can do it in the useEffect hook, which will run when the component mounts. Here's an example:
import { firebase } from ‘…/Firebase/firebase’; import ‘firebase/compat/database’; import React, {useState, useEffect} from ‘react’; export default function Profile() { const [userData, setUserData] = useState(null); useEffect(() => { const currentUserID = firebase.auth().currentUser.uid; firebase .database() .ref('users/' + currentUserID) .once('value') .then((snapshot) => { const data = snapshot.val(); setUserData(data); }); }, []); return ( <View> <Text>Name: {userData?.name}</Text> <Text>Surname: {userData?.surname}</Text> <Text>Email: {userData?.email}</Text> <Text>Phone: {userData?.phone}</Text> {/* other profile content */} </View> ); }In this example, we first create a state variable called and set it to null. Then, in the useEffect hook, we use the method to retrieve the user data from the Firebase database using the current user's ID. Once we have the data, we set it in the state using the function.
- User
there is an err: possible unhandled promise rejection (id:8) error: permission denied at .. Client doesnt have permossion to access the desired data
- Assistant
The error message
permission deniedsuggests that you might not have set up the Firebase database security rules correctly. When you set up your database, Firebase generates default security rules that restrict access to only authenticated users. If you didn't modify these rules, you might be getting the error because the user doesn't have permission to read from the database.To fix the issue, you need to update the database rules to give the correct permissions. Here's an example of a simple rule that allows authenticated users to read and write their own user data:
{ "rules": { "users": { "$uid": { ".read": "$uid === auth.uid", ".write": "$uid === auth.uid" } } } }In this example, we are setting the security rules for the "users" node, which contains data for each user. The
$uidis a wildcard that matches the current user's ID. The and rules allow the user to read and write their own data. - User
can u write it without quotation marks
- Assistant
Sure! Here's the same code without quotation marks:
{ rules: { users: { $uid: { .read: $uid === auth.uid, .write: $uid === auth.uid } } } }Note that while you don't need to use quotation marks, the syntax highlighting in some editors or IDEs might not work without them.
- User
{ "rules": { ".read": true, ".write": false } "users":{ "uid":{ "uid" === auth.uid, ".write": $uid === auth.uid } } }
- Assistant
There is a syntax error in the rules you provided. You should separate rules for different paths with a comma. Also, you need to use the
$character to access theuidwildcard parameter. Here is the corrected version of your rules:{ "rules": { ".read": true, ".write": false, "users": { "$uid": { ".read": "$uid === auth.uid", ".write": "$uid === auth.uid" } } } }In this example, we set a default rule that allows anyone to read any data, but only authenticated users can write. Then, we set up rules for the "users" node where the
$uidwildcard parameter is used to match individual users. We allow each user to read and write their own data by using the$uidandauth.uidcomparison.Note that the syntax for rules is very strict, and even small errors can cause them to fail to deploy. Be sure to test your rules thoroughly and check for any error messages in the Firebase console.
- User
{ "rules": { ".read": true, ".write": false, "users":{ "uid":{ "uid" === auth.uid, ".write": $uid === auth.uid } } }
- Assistant
There is another syntax error in your Firebase security rules. The line
"uid" === auth.uidis not valid because you need to use the$character to reference the wildcard parameter. Here is the corrected version of your rules:{ "rules": { ".read": true, ".write": false, "users": { "$uid": { ".write": "$uid === auth.uid" } } } }In this example, we allow anyone to read data from the database, but only authenticated users can write to it. Then, we set up a rule for the "users" node that matches each user's
$uid. We allow each user to write their own data by comparing the$uidto theauth.uidparameter.It's important to note that security rules for Firebase databases should be set up carefully to ensure that your data is protected from unauthorized access. Be sure to test your rules and check the Firebase console for any errors or warnings.
- User
do i need use ``?
- Assistant
Yes, you need to use backticks (```) if you want to use string interpolation to include variables inside your Firebase security rules. For example:
{ "rules": { ".read": true, ".write": false, "users": { "$uid": { ".write": `$uid === auth.uid` } } } }In this example, we use backticks to wrap the
$uid === auth.uidcomparison, which allows us to include the value of theauth.uidparameter inside the string using the${}notation. Using backticks is necessary to use string interpolation and avoid syntax errors in your Firebase security rules. - User
oh god, can u use '' insted
because some text disappears when u use - Assistant
Sure thing! You can use single quotes (
'') instead of backticks to write your Firebase security rules. Keep in mind that when using single quotes, you won't be able to use string interpolation, so the code will be less dynamic. Here's an example:{ "rules": { ".read": true, ".write": false, "users": { "$uid": { ".write": "$uid === auth.uid" } } } }In this example, we use single quotes to write the comparison between
$uidandauth.uid. This code is equivalent to the previous example, but it's less dynamic because you can't include variables inside the string using string interpolation. Instead, you have to concatenate strings using the+operator.While using single quotes is a valid way of writing your Firebase security rules, keep in mind that you might find it more difficult to write complex rules using this syntax.
- User
{ "rules": { ".read": true, ".write": false, "users":{ "uid":{ "uid" === auth.uid, ".write": $uid === auth.uid } } }
- Assistant
There is a syntax error in your Firebase security rules. The
"uid" === auth.uidexpression is not a valid rule, as it compares a string to theauth.uidparameter. To perform a comparison between the values of$uidandauth.uid, you need to use the$character to access$uidand enclose your comparison in backticks to use string interpolation. Here is the corrected version of your rules:{ "rules": { ".read": true, ".write": false, "users": { "$uid": { ".write": "`" + $uid + "` === auth.uid" } } } }In this example, we allow anyone to read data from the database and only authenticated users to write to it. Then we set up a rule for the "users" node that matches each user's
$uid. We allow each user to write their own data using the$uidwildcard parameter with string interpolation and by comparing it to theauth.uidparameter.Note that the syntax for Firebase security rules is strict