Mwt_0239
Mwt_0239

Reputation: 395

Firebase Error auth/admin-restricted-operation after createUserWithEmailAndPassword

Im getting an Firebase: Error (auth/admin-restricted-operation) error after trying to sign up with email. (In settings of my project i have creating users with email allowed.) It could be something in my project settings? I was searching internet to solve that problem like for hours so please help, im really new to firebase.

import React, {useContext, useState, useEffect} from 'react'
import { auth } from '../firebase';
import { createUserWithEmailAndPassword, onAuthStateChanged } from "firebase/auth";

const AuthContext = React.createContext()


export function useAuth(){
    return useContext(AuthContext)
}

export function AuthProvider({children}) {
    const [currentUser, setUser] = useState();
    
    function signup(email, password){
        return createUserWithEmailAndPassword(auth, email, password);
    }

    useEffect(() => {
        const unsubscribe = onAuthStateChanged(auth, user => {
            setUser(user)
        })

        return unsubscribe
    }, [])


    const value = {
        currentUser,
        signup
    }

    return (
        <AuthContext.Provider value={value}>
            {children}
        </AuthContext.Provider>
    )
}

import {React, useRef, setError, useState} from 'react' 
import { useAuth } from '../contexts/AuthContext';

function SignUp() {
    const emailRef = useRef();
    const passwordRef = useRef();
    const passwordConfirmRef = useRef();
    const {signup} = useAuth();    

    const handleSubmit = async (e) => {
        e.preventDefault()
        await signup(emailRef.value, passwordRef.value)
    }

    return (
        <div>
            <form onSubmit={handleSubmit}>
                <label for="email">Email</label>
                <input type="text" id="email" inputRef={emailRef} required></input>
                <label for="password">Password</label>
                <input type="password" id="password" inputRef={passwordRef} required></input>
                <label for="email">Pasword Confirmation</label>
                <input type="text" id="email" inputRef={passwordConfirmRef} required></input>
                <button type="submit" >Sign Up</button>
            </form>
        </div>
    )
}

export default SignUp

import * as firebase from "firebase/app"
import { getAuth } from "firebase/auth";


const app = firebase.initializeApp({
...
})

export const auth = getAuth()
export default app

enter image description here

Upvotes: 9

Views: 20362

Answers (5)

MFK34
MFK34

Reputation: 158

2024 - Solution
I had to update my firebase settings to allow signups. Firebase -> Authentication -> Settings -> Enable create (sign-up).

Upvotes: 3

Mwt_0239
Mwt_0239

Reputation: 395

So uhh i solve that...

  • I forgot to add
export const auth = getAuth(***app***);

i had

export const auth = getAuth();
  • and i used inputRef instead ref in form

Explaining:

It was restricted for admins due to input i was sending it was (undefined, undefined) because of using inputRef instead ref.

Upvotes: 5

Mian Aamir Shehzad
Mian Aamir Shehzad

Reputation: 218

Please check if you're passing the right parameters to the createUserWithEmailAndPassword method. In my case, my callback function was not receiving any text to be passed to setStateMethod. I was using my code like this

onChangeText={() => setEmail()

What I Did? Just pass text to the method like this:

      onChangeText={(text) => setEmail(text)

Upvotes: 3

Dan1ell
Dan1ell

Reputation: 395

You saved me! Everywhere else people are incorrectly saying to enable anonymous authentication as the correct solution for [auth/admin-restricted-operation]. Of course, that is wrong.

Enabling anonymous authentication works at first, so everyone thumbs it up, but it only works because anonymous authentication doesn't require an email and password to be sent. Trouble comes later when we realize we aren't able to log in with the email and password we used to signup when we simply enable anonymous auth without addressing the actual problem.

The real answer is we are not sending email and password correctly, as in your case, so email/password auth says "no, only admins can do that kind of magic".

We have to check our sign-in data. That's the answer. Thanks so much!

Upvotes: 14

MITHUN BISWAS
MITHUN BISWAS

Reputation: 1

I think you can not provide email and password when you call the signup(email, password) function from the others file.

Upvotes: -1

Related Questions