Skip to content

Instantly share code, notes, and snippets.

@stursby
Last active September 8, 2024 11:22
Show Gist options
  • Save stursby/73d85379717f22cc52fe3993408da0dd to your computer and use it in GitHub Desktop.
Save stursby/73d85379717f22cc52fe3993408da0dd to your computer and use it in GitHub Desktop.
Vue + Firebase + Auth Demo

Vue + Firebase + Auth Demo

A simple App using Vue.js & Firebase with Auth.

See the DEMO.

{
"rules": {
"items": {
"$uid": {
".read": "auth != null && auth.uid == $uid",
".write": "auth != null && auth.uid == $uid"
}
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Vue + Firebase + Auth Demo</title>
<style> body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; } </style>
</head>
<body>
<div id="app">
<h1>Simple Auth Demo</h1>
<p v-if="loading">Loading...</p>
<template v-if=user>
<img :src="user.photoURL" alt="avatar" style="width: 30px; height: 30px; border-radius: 50%;">
<button @click="signOut">Sign Out</button>
<ul>
<li v-for="item in items" :key="item['.key']">{{ item.name }}
<button @click="removeItem(item)">&times;</button>
</li>
</ul>
<p v-if="!items.length">No items found! Add one below.</p>
<label for="item">Add Item</label> <br>
<input type="text" v-model="item" @keyup.enter="addItem">
</template>
<template v-if="!user && !loading">
<button @click="signInWithGoogle">Sign in with Google</button>
</template>
</div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.6.10/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.6.10/firebase-database.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.6.10/firebase-auth.js"></script>
<script src="https://unpkg.com/vuefire/dist/vuefire.js"></script>
<script>
var config = {
apiKey: 'AIzaSyA8zh_Wf7ja7PWuEPK7iyXjVQAufNNCwNM',
authDomain: 'simple-auth-demo.firebaseapp.com',
databaseURL: 'https://simple-auth-demo.firebaseio.com'
}
const firebaseApp = firebase.initializeApp(config)
const db = firebaseApp.database()
const vm = new Vue({
el: '#app',
beforeCreate: function() {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
this.user = user
this.$bindAsArray('items', db.ref(`items/${user.uid}`))
}
this.loading = false
})
},
data: {
loading: true,
user: null,
items: [],
item: ''
},
methods: {
signInWithGoogle: function() {
const provider = new firebase.auth.GoogleAuthProvider()
firebase.auth().signInWithRedirect(provider).then((result) => {
this.user = result.user
}).catch(err => console.log(error))
},
signOut: function() {
firebase.auth().signOut().then(() => {
this.user = null
}).catch(err => console.log(error))
},
addItem: function() {
this.$firebaseRefs.items.push({
name: this.item
}).then(() => {
this.item = ''
})
},
removeItem: function(item) {
this.$firebaseRefs.items.child(item['.key']).remove()
}
}
})
</script>
</body>
</html>
@MichaelCurrin
Copy link

Thanks for sharing.

Once logged in, there's a bug.

The "loading..." notification remains and adding an item does nothing.

Screen Shot 2021-12-13 at 8 46 53 am

@stursby
Copy link
Author

stursby commented Dec 13, 2021

@MichaelCurrin this code is pretty old haha but looks like Vuefire changed the method from v1 to v2: https://vuefire.vuejs.org/vuefire/upgrading-from-v1.html#changes-to-bindasarray-bindasobject-and-unbind

@MichaelCurrin
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment