title: Use Firebase Cloud Function for Create User(Remove Automatically Login When register user)
body: <p>When user register from client side(mobile app) , user automatically login app i dont want auto login so, I did some research, I had to use a firebase cloud function to solve this.</p>
  <p>But I get a few errors when calling the function , how can i fix these errors</p>
  <p><strong>First error :</strong></p>
  <p>Access to XMLHttpRequest at 'https://***.cloudfunctions.net/createUser' from origin 'http://localhost:8100' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.</p>
  <p><strong>second error :</strong></p>
  <p>zone-evergreen.js:2845 POST https://****.cloudfunctions.net/createUser net::ERR_FAILED</p>
  <p><strong>Third Error</strong></p>
  <p>core.js:4197 ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: &quot;Unknown Error&quot;, url: &quot;https://***.cloudfunctions.net/createUser&quot;, ok: false, …}</p>
  <p><strong>Firebase Console log</strong></p>
  <p>2:02:23.363 ÖS
  createUser
  Function execution started</p>
  <p>2:02:23.390 ÖS
  createUser
  Function execution took 28 ms, finished with status: 'crash'</p>
  <p><strong>cloud function :</strong></p>
  <pre><code>const functions = require('firebase-functions');
  const cors = require('cors')({ origin: true });
  const admin = require('firebase-admin')
  admin.initializeApp();
  
  exports.createUser = functions.https.onRequest((request, response) =&gt; {
    return cors(req, res, () =&gt; {
    if (request.method !== &quot;POST&quot;) {
        response.status(405).send(&quot;Method Not Allowed&quot;);
    } else {
        let body = request.body;
  
        const email = body.email;
        const password = body.password;
       
  
        admin.auth().createUser({
            email: email,
            emailVerified: false,
            password: password,
        })
        .then((userRecord) =&gt; {
            return response.status(200).send(&quot;Successfully created new user: &quot; +userRecord.uid);
        })
        .catch((error) =&gt; {
            return response.status(400).send(&quot;Failed to create user: &quot; + error);
        });
    }
  })
  });
  </code></pre>
  <p><strong>client side :</strong></p>
  <pre><code> signUp(email,password){
      let body = {
        email : email,
        password : password
      }
       this.http.post(
          'https://****.cloudfunctions.net/createUser',
        body
        ).subscribe(a=&gt;{console.log(&quot;Work&quot;)});
  }
  </code></pre>
  <p><strong>EDIT (new cloud function code) :</strong></p>
  <p>1st and 2nd bug fixed 3 still continues but I can create users.</p>
  <pre><code>exports.createUser = functions.https.onRequest((req, res) =&gt; {
    res.set('Access-Control-Allow-Origin', '*');
    res.set('Access-Control-Allow-Methods', ' POST, OPTIONS');
    res.set('Access-Control-Allow-Headers', '*');
     if (req.method === 'OPTIONS') {
       res.end();
    }
     else {
      
        let body = req.body;
        console.log(body.email)
        console.log(&quot;body.password&quot;)
        const email = body.email;
        const password = body.password;
       
  
        admin.auth().createUser({
            email: email,
            emailVerified: false,
            password: password,
        })
        .then((userRecord) =&gt; {
            return res.status(200).send(&quot;Successfully created new user: &quot; +userRecord.uid);
        })
        .catch((error) =&gt; {
            return res.status(400).send(&quot;Failed to create user: &quot; + error);
        });
    }
  
  });
  </code></pre>