Demonstrate a basic setup of Vue and Firebase. Should include the following:
- Email Authentication with Firebase
- Firestore Database with VueFire wrapper
- Environment variables
- Firebase Storage integration (TBD)
npm init vue@latest
cd <your-project-name>
VSCode + Volar (and disable Vetur) + TypeScript Vue Plugin (Volar).
See Vite Configuration Reference.
npm install
npm run dev
npm run build
Run Headed Component Tests with Cypress Component Testing
npm run test:unit:dev # or `npm run test:unit` for headless testing
Run End-to-End Tests with Cypress
npm run test:e2e:dev
This runs the end-to-end tests against the Vite development server. It is much faster than the production build.
But it's still recommended to test the production build with test:e2e
before deploying (e.g. in CI environments):
npm run build
npm run test:e2e
Lint with ESLint
npm run lint
These steps demonstrate how to install Firebase and Firestore DB in your Vue project.
- Add a new Project under "Your Firebase projects".
- Choose your prefered options.
- When the project is created, click on "Web" under "Get started by adding Firebase to your app". Give your a name and Register app.
- Add Firebase SDK
npm install firebase --save
- copy the whole snippet of code that includes SDK config
- back in your project, create a new file under
/src
calledfirebase
without any extension - paste previously copied code into the newly created file
Vuefire is a lightweight wrapper that handles realtime binding between Vue/Vuex and Firebase databases, such as real-time databases or Cloud Firestore. It has some inbuilt logic that always keeps local data in sync with remote Firebase databases.
npm install vuefire --save
- Copy the following snippet and replace
firebase
file contents with it
import { initializeApp } from 'firebase/app'
import { getFirestore, collection } from 'firebase/firestore'
export const firebaseApp = initializeApp({
// keep the original keys untouched and move them here
})
// used for the firestore refs
const db = getFirestore(firebaseApp)
// here we can export reusable database references
export const todosRef = collection(db, 'todos')
- Create Firestore Database
- Click on "Start collection" under "Panel view" when the Database is created. Collection ID is your DB name. In this example it's "todos". You should also add the first key and value. If your DB name is different, replace 'todos' in the above example wqith your own name.
- Replace contents of
src/main.js
with the following snippet:
import { createApp } from 'vue'
import { VueFire } from 'vuefire'
import App from './App.vue'
import { firebaseApp } from './firebase'
const app = createApp(App)
app
.use(VueFire, {
firebaseApp,
modules: [],
})
app.mount('#app')
- Copy the following snippet and paste into a newly created file called
ToDoList.vue
undersrc/components
:
<script setup>
import { useCollection } from 'vuefire'
import { todosRef } from '../firebase'
const todos = useCollection(todosRef)
</script>
<template>
<ul>
<li v-for="todo in todos" :key="todo.id">
<span>{{ todo.name }}</span>
</li>
</ul>
</template>
Replace todo
, todos
, todo.id
or todo.name
with your own names.
2. Add the newly created component to App.vue
<script setup>
...
import ToDoList from './ToDoList.vue'
</script>
<template>
...
<ToDoList />
</template>
Env Variables and Modes | Vite (vitejs.dev)
- Create
.env
file in the root of the app - Create ENV variables for configuration variables. Each key should start with
VITE_
. Example:
VITE_APIKEY=123
- Add reference to your newly created env. variables in
src/firebase
:
const {
VITE_APIKEY,
...
} = import.meta.env
- Replace key values in
src/firebase
with env. variables. Example:
export const firebaseApp = initializeApp({
apiKey: VITE_APIKEY,
...
})
- Enable Email/Password as your Sign-in provider. Click Save.
- Select "Users" tab and click "Add user". Add Email address and password.
- Follow VueFire Authentication installation tutorial. Edit
src/main.js
:- Import
VueFireAuth
from vuefire. - Add
VueFireAuth()
into modules
- Import
import { createApp } from 'vue'
import { VueFire, VueFireAuth } from 'vuefire'
import App from './App.vue'
import { firebaseApp } from './firebase'
const app = createApp(App)
app
.use(VueFire, {
firebaseApp,
modules: [
VueFireAuth(),
],
})
app.mount('#app')
- Follow Firebase docs. Create a new file
src/SignIn.vue
and add this snippet as content:
<script setup>
import { getAuth, signInWithEmailAndPassword } from 'firebase/auth'
import { useCurrentUser } from 'vuefire'
const user = useCurrentUser()
const auth = getAuth()
const email = <email>
const password = <password>
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
console.log(userCredential.user)
})
.catch((error) => {
console.log(error.code)
console.log(error.message)
})
</script>
<template>
<p v-if="user">Hello {{ user.email }}</p>
</template>
- Replace email nad password with the credentials from Firebase newly created user. Import "SignIn" component to
App.vue
and render it. If the credentials are correct, you will see a greeting. Otherwise, the greeting won't be displayed.
- Go to Firebase Storage and click "Get Started" button. Choose between production mode and test mode.
- Edit
src/firebase
, import getStorage from firebase/storage, then initialize Cloud Storage and get a reference to the service.
import { getStorage } from "firebase/storage"
...
const storage = getStorage(app)
- Go to VueFire FileUpload example and copy the provided snippet to Upload files a new file
src/StorageUpload.vue
. Import this file inApp.vue
and render it. - Display your image on the page with the following line of code reading image URL if tit exists
<img v-if="url" :src="url" />
- Install VueUse as this example uses file upload composition utility
npm i @vueuse/core --save
https://github.com/TheTranceMachine/composition-book/commit/64e85af13fa35b22c3020efc96cda5feb391121e
https://github.com/TheTranceMachine/composition-book/commit/5e05628a91470be00864bcc192b8b6f9a713c1a4
https://github.com/TheTranceMachine/composition-book/commit/982f834d4f92da8be5c4603433ece2c8e377f9a9