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 {
|
try {
|
||||||
|
|
||||||
const db = getFirestore(firestore)
|
const db = getFirestore(firestore)
|
||||||
const data = await collection(db, 'adminCodes')
|
const data = await collection(db, 'users')
|
||||||
const result = await getDocs(data)
|
const result = await getDocs(data)
|
||||||
|
|
||||||
return await result.docs.map(doc => doc.data())
|
return await result.docs.map(doc => doc.data())
|
||||||
|
|||||||
@@ -1,14 +1,24 @@
|
|||||||
import { firestore } from '../../../../../../Firebase/Firebase_Config'
|
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()
|
const auth = getAuth()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const response = await createUserWithEmailAndPassword(auth, email, password)
|
const responseUserCreate = await createUserWithEmailAndPassword(auth, email, password)
|
||||||
console.log(response)
|
console.log(responseUserCreate)
|
||||||
|
|
||||||
|
const db = getFirestore(firestore)
|
||||||
|
|
||||||
|
await setDoc(doc(db, 'users', email), {
|
||||||
|
|
||||||
|
name: name,
|
||||||
|
position: position,
|
||||||
|
refferCode: refferCode
|
||||||
|
})
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setMessage('There has been an error registering the user. Please try again later')
|
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 [option, setOption] = useState('login')
|
||||||
|
|
||||||
|
const [name, setName] = useState('')
|
||||||
const [email, setEmail] = useState('')
|
const [email, setEmail] = useState('')
|
||||||
const [password, setPassword] = useState('')
|
const [password, setPassword] = useState('')
|
||||||
const [confirmPassword, setConfirmPassword] = useState('')
|
const [confirmPassword, setConfirmPassword] = useState('')
|
||||||
@@ -88,11 +89,19 @@ const Form = () => {
|
|||||||
|
|
||||||
<form
|
<form
|
||||||
onSubmit={(e) => {
|
onSubmit={(e) => {
|
||||||
FormActions(e, [email, password, confirmPassword, adminReferredCode], option, setLoading, setMessage)
|
FormActions(e, [name, email, password, confirmPassword, adminReferredCode], option, setLoading, setMessage)
|
||||||
clearStates()
|
clearStates()
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="Name"
|
||||||
|
required
|
||||||
|
onChange={(e) => setName(e.target.value)}
|
||||||
|
value={name}
|
||||||
|
/>
|
||||||
|
|
||||||
<input
|
<input
|
||||||
type="email"
|
type="email"
|
||||||
placeholder="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 React from 'react'
|
||||||
import BringAdminCode from '../Firebase Querys/BringAdminCode'
|
import BringAdminCode from '../Firebase Querys/BringAdminCode'
|
||||||
import RegisterNewUser from '../Firebase Querys/RegisterNewUser'
|
import RegisterNewUser from '../Firebase Querys/RegisterNewUser'
|
||||||
|
import MakeRandomString from './MakeRandomString'
|
||||||
|
|
||||||
const ValidateRegister = async (data, setMessage) => {
|
const ValidateRegister = async (data, setMessage) => {
|
||||||
|
|
||||||
const email = data[0]
|
const name = data[0]
|
||||||
const password = data[1]
|
const email = data[1]
|
||||||
const confirmPassword = data[2]
|
const password = data[2]
|
||||||
const adminRefferCode = data[3]
|
const confirmPassword = data[3]
|
||||||
|
const adminRefferCode = data[4]
|
||||||
|
|
||||||
if (password.length < 6) {
|
if (password.length < 6) {
|
||||||
|
|
||||||
@@ -29,10 +31,13 @@ const ValidateRegister = async (data, setMessage) => {
|
|||||||
|
|
||||||
documents.forEach(document => {
|
documents.forEach(document => {
|
||||||
|
|
||||||
if (adminRefferCode === document.code) {
|
if (adminRefferCode === document.refferCode) {
|
||||||
|
|
||||||
adminRefferCodeIsValid = true
|
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