Using Firebase Authentication on Your Website

I am currently working on developing a login system using Firebase authentication and I have encountered some issues in the process. After following Google's tutorial (https://www.youtube.com/watch?v=rQvOAnNvcNQ), and creating a simple "Index.html" & "Index.js" test, I came across an error stating "Uncaught SyntaxError: Cannot use import statement outside a module index.js:1". I attempted to resolve this by changing the .js file to a .mjs and loading it as a module, but then the .html file had trouble finding the functions in the .mjs file.

The onAuthStateChanged() function consistently logs "No user", indicating that it is functioning correctly.

All of my testing has been done via localhost so far.

If anyone could provide assistance with this issue, it would be highly appreciated!

index.mjs

import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.0.0/firebase-app.js';
import { getAuth, onAuthStateChanged } from 'https://www.gstatic.com/firebasejs/9.0.0/firebase-auth.js';

const firebaseConfig = {
  ...
};

const app = initializeApp(firebaseConfig);

const auth = getAuth(app);

onAuthStateChanged(auth, (user) => {
  if (user) {
    console.log('logged in!');
  } else {
    console.log('No user');
  }
});


function login() {
  window.alert("Login successful")
}

function signUp() {
  window.alert("Signup successful");
}

index.html

<!DOCTYPE html>
<html lang = "en-US">

<head>
  <meta charset = "UTF-8">
  <meta name = "viewport" content = "width=device-width, initial-scale=1.0">
  <title> test </title>
  <script type="module" src="/index.mjs"></script>
</head>
  <body>
    <div class = "login-form">
        <input type = "email" class="login-username" id="login-username" autofocus="true" required="true" placeholder="Email" />
        <input type = "password" class="login-password" id="login-password" required="true" placeholder="Password" />
        <input type = "submit" name="Login" value="Login" class="login-submit" onclick = "login()"/>
        <input type = "submit" name="Signup" value="Signup" class="login-submit" onclick = "signUp()"/>
    </div>
  </body>
</html>

Answer №1

Every situation is unique and requires specific attention. Additional information can provide clarity. In the meantime, consider utilizing the "require" method instead of the import one.

For more guidance, visit this helpful link which offers a solution to potential issues you may encounter.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Getting JSON or JSONP data through a XAMPP local server is a straightforward process

After setting up a local server with XAMPP, my goal is to retrieve JSON / JSONP data from that server. Additional query: Do I need to upload the JSON file directly onto the server? Or can I achieve this using somePHPcoding? If so, which? I have come ac ...

The addition operator cannot be used with the Number type and the value of 1

Encountering an issue where the operator '+' cannot be applied to types 'Number' and '1' buildQuerySpec() { return { PageSize: this.paging.PageCount, CurrentPage: this.paging.PageIndex + 1, MaxSize: '' ...

Steps for choosing all choices in a multi-select menu without changing the order

What is the best way to choose all the options from a multiselect box using Java Script? ...

Sending the method's URL in the controller through an AJAX call

Below is the code snippet for an ajax call: <script> jQuery(document).ready(function() { $("#VEGAS").submit(function(){ var form_data = $("#VEGAS").serialize(); var routeUrl = "<?= url('/'); ?> /PUBLIC/vpage"; $.ajax({ ...

Issue with Vuex map state being undefined when called from a component

Recently, I've been working on updating data in a component every time the Vuex state is not null. I have set up API routes with Laravel that return user information once they are logged in. API routes: Route::group(['middleware' => [&a ...

Ensure that test cases in Angular2 include a line that covers conditions for returning values

I'm having trouble implementing test coverage for an online platform, as I'm not sure how to approach it. Within my service method, I have the following code: public getResults() { return this.http(url,body).map(this.mapResponse); } private ...

Showing user data in a div using JavaScript ajax without the use of jQuery

Recently diving into the world of javascript and ajax, I find myself tinkering with code on jsfiddle. My current goal is to populate a list with usernames and emails upon form submission. Upon submitting the form, an error message pops up: {"error":"key m ...

Implementing a delay between two div elements using jQuery

I have two Divs with the class "sliced", each containing three images with the class "tile". When I animate using jQuery, I am unable to add a delay between the "sliced" classes. However, I have successfully added a delay between the "tile" classes. index ...

What is the method to retrieve the base host in AngularJS?

I need assistance with the following URL: https://192.168.0.10/users/#!/user-profile/20 When I use $location.host, it returns 192.168.0.10 However, I only want to extract https://192.168.0.10 What is the best way to achieve this? ...

Is there a way to integrate jQuery and Javascript into a Firefox add-on?

Having trouble creating a new element on the page? After checking both the page and domain during onload, it seems that everything is in order. But how can you successfully create a new element in the correct window page? window.addEventListener("load", f ...

Organizing Perspectives in AngularJS

Transitioning from Backbone to AngularJS, I am embarking on creating my first application with the latter. To kick things off, my initial task involves migrating an existing backbone application to AngularJS. This application features a main view housing ...

Effective methods for transferring parameters between two separate JavaScript files within an express.js application

Currently, I am working with Express.js and facing a challenge in passing parameters from one JavaScript file to another. How can this be accomplished? The two files involved are 1. process.js var WebPageTest = require('webpagetest'); var wpt ...

"Encountered an issue when attempting to convert undefined or null to an object within a

I am facing an issue with my test setup which looks like this: var jsdom = require('jsdom').jsdom; var document = jsdom('<html></html>', {}); var window = document.defaultView; global.jQuery = global.$ = require('jquer ...

Is there a way to append a URL parameter after a link is clicked using Vue.js?

I am currently working on integrating heading links on my website's sidebar that are linked to the main content using scrollspy. This allows users to easily navigate to different sections of the main content by clicking on the corresponding headings i ...

Is there a way to ensure that the tab character " " remains consistent in size within a textarea at all times?

My current challenge involves inserting tabs of the same size into a textarea. The example shows that the only variation in each line of the testString is the placement of the \t. However, when displayed in the textarea, the tab sizes differ dependi ...

Troubleshoot: Bootbox Confirm Functionality Issues

Can anyone help me troubleshoot this issue? I'm trying to implement code that uses bootbox.confirm when deleting a file, but it's not functioning correctly. $('#fileupload').fileupload({ destroy: function (e, data) { var that = $(th ...

Changing view upon button click in ReactJS: implement conditional rendering

After grasping the fundamentals of ReactJS, I am eager to put my knowledge into practice by building a small application. The application includes two images below. In the Home view (1st image), there are several buttons that, when clicked, should lead to ...

Do I need to provide my email and password while using the Firebase signInWithGoogle()?

After attaching the signInWithGoogle() method to a button within the form, instead of opening a popup window as expected, it prompts me to fill in the email and password fields. Below is the configuration file: import firebase from 'firebase/app' ...

What is the best way to invoke React component code from renderer.js?

Hello everyone, I am diving into the world of React/JS and Electron and have a goal to develop a desktop application using these amazing technologies. Currently, my biggest challenge is figuring out how to call react component code from renderer.js. Let m ...

Disable form input fields while keeping all other elements enabled

Currently, I am facing a dilemma where I must deactivate specific input fields within a form. Given that there are numerous fields to consider, individually disabling each one seems impractical. Can anyone offer advice on how to disable a set of elements ...