mirror of
https://github.com/FranP-code/Hangman-game-with-React.git
synced 2025-10-13 00:42:32 +00:00
Register logic actualizated
This commit is contained in:
@@ -6,7 +6,7 @@ const BringAdminCode = async () => {
|
||||
try {
|
||||
|
||||
const db = getFirestore(firestore)
|
||||
const data = await collection(db, 'adminCodes')
|
||||
const data = await collection(db, 'users')
|
||||
const result = await getDocs(data)
|
||||
|
||||
return await result.docs.map(doc => doc.data())
|
||||
|
||||
@@ -1,14 +1,24 @@
|
||||
import { firestore } from '../../../../../../Firebase/Firebase_Config'
|
||||
import {getAuth, createUserWithEmailAndPassword} from 'firebase/auth'
|
||||
import {getAuth, createUserWithEmailAndPassword, } from 'firebase/auth'
|
||||
import { getFirestore, setDoc, doc } from 'firebase/firestore'
|
||||
|
||||
const RegisterNewUser = async (email, password, setMessage) => {
|
||||
const RegisterNewUser = async (name, email, password, setMessage, position, refferCode) => {
|
||||
|
||||
const auth = getAuth()
|
||||
|
||||
try {
|
||||
|
||||
const response = await createUserWithEmailAndPassword(auth, email, password)
|
||||
console.log(response)
|
||||
const responseUserCreate = await createUserWithEmailAndPassword(auth, email, password)
|
||||
console.log(responseUserCreate)
|
||||
|
||||
const db = getFirestore(firestore)
|
||||
|
||||
await setDoc(doc(db, 'users', email), {
|
||||
|
||||
name: name,
|
||||
position: position,
|
||||
refferCode: refferCode
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
setMessage('There has been an error registering the user. Please try again later')
|
||||
|
||||
@@ -8,6 +8,7 @@ const Form = () => {
|
||||
|
||||
const [option, setOption] = useState('login')
|
||||
|
||||
const [name, setName] = useState('')
|
||||
const [email, setEmail] = useState('')
|
||||
const [password, setPassword] = useState('')
|
||||
const [confirmPassword, setConfirmPassword] = useState('')
|
||||
@@ -88,11 +89,19 @@ const Form = () => {
|
||||
|
||||
<form
|
||||
onSubmit={(e) => {
|
||||
FormActions(e, [email, password, confirmPassword, adminReferredCode], option, setLoading, setMessage)
|
||||
FormActions(e, [name, email, password, confirmPassword, adminReferredCode], option, setLoading, setMessage)
|
||||
clearStates()
|
||||
setLoading(true)
|
||||
}}
|
||||
>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Name"
|
||||
required
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
value={name}
|
||||
/>
|
||||
|
||||
<input
|
||||
type="email"
|
||||
placeholder="Email"
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
const MakeRandomString = (length) => {
|
||||
|
||||
let result = '';
|
||||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
const charactersLength = characters.length;
|
||||
|
||||
for ( var i = 0; i < length; i++ ) {
|
||||
|
||||
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export default MakeRandomString
|
||||
@@ -1,13 +1,15 @@
|
||||
import React from 'react'
|
||||
import BringAdminCode from '../Firebase Querys/BringAdminCode'
|
||||
import RegisterNewUser from '../Firebase Querys/RegisterNewUser'
|
||||
import MakeRandomString from './MakeRandomString'
|
||||
|
||||
const ValidateRegister = async (data, setMessage) => {
|
||||
|
||||
const email = data[0]
|
||||
const password = data[1]
|
||||
const confirmPassword = data[2]
|
||||
const adminRefferCode = data[3]
|
||||
const name = data[0]
|
||||
const email = data[1]
|
||||
const password = data[2]
|
||||
const confirmPassword = data[3]
|
||||
const adminRefferCode = data[4]
|
||||
|
||||
if (password.length < 6) {
|
||||
|
||||
@@ -29,10 +31,13 @@ const ValidateRegister = async (data, setMessage) => {
|
||||
|
||||
documents.forEach(document => {
|
||||
|
||||
if (adminRefferCode === document.code) {
|
||||
if (adminRefferCode === document.refferCode) {
|
||||
|
||||
adminRefferCodeIsValid = true
|
||||
RegisterNewUser(email, password, setMessage)
|
||||
|
||||
const position = document.position
|
||||
const refferCode = MakeRandomString(30)
|
||||
RegisterNewUser(name, email, password, setMessage, position, refferCode)
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user