Skip to content

Commit 86841dc

Browse files
author
Nuwan Chandrasoma
committed
firebase v9.0+
upgrade the firebase version to 9
1 parent 7ffd121 commit 86841dc

File tree

7 files changed

+2551
-525
lines changed

7 files changed

+2551
-525
lines changed

package-lock.json

+2,522-497
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"serve": "vite preview"
77
},
88
"dependencies": {
9-
"firebase": "^8.6.5",
10-
"vue": "^3.0.5",
11-
"vue-router": "^4.0.8"
9+
"firebase": "^9.9.2",
10+
"vue": "^3.2.37",
11+
"vue-router": "^4.1.3"
1212
},
1313
"devDependencies": {
1414
"@vitejs/plugin-vue": "^1.2.3",

src/App.vue

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<router-link to="/feed"> Feed </router-link> |
77
</span>
88
<span v-if="isLoggedIn">
9-
<button @click="signOut"> Logout </button>
9+
<button @click="handleSignOut"> Logout </button>
1010
</span>
1111
<span v-else>
1212
<router-link to="/register"> Register </router-link> |
@@ -20,24 +20,24 @@
2020

2121
<script setup>
2222
import { ref, watchEffect } from 'vue' // used for conditional rendering
23-
import firebase from 'firebase'
23+
import { getAuth,onAuthStateChanged, signOut } from 'firebase/auth'
2424
import { useRouter } from 'vue-router'
2525
2626
const router = useRouter()
2727
2828
const isLoggedIn = ref(true)
2929
3030
// runs after firebase is initialized
31-
firebase.auth().onAuthStateChanged(function(user) {
31+
onAuthStateChanged(getAuth(),function(user) {
3232
if (user) {
3333
isLoggedIn.value = true // if we have a user
3434
} else {
3535
isLoggedIn.value = false // if we do not
3636
}
3737
})
3838
39-
const signOut = () => {
40-
firebase.auth().signOut()
39+
const handleSignOut = () => {
40+
signOut(getAuth())
4141
router.push('/')
4242
}
4343
</script>

src/main.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
import { createApp } from 'vue'
22
import App from './App.vue'
33
import router from './router'
4-
import firebase from 'firebase'
4+
import { initializeApp } from 'firebase/app'
5+
import { getAuth,connectAuthEmulator } from 'firebase/auth';
56

67
/* code from our Firebase console */
7-
var firebaseConfig = {
8-
apiKey: "YOUR_API_KEY",
9-
authDomain: "YOUR_AUTH_DOMAIN",
10-
projectId: "YOUR_PROJECT_ID",
11-
storageBucket: "YOUR_STORAGE_BUCKET",
12-
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
13-
appId: "YOUR_APP_ID"
14-
}
8+
const firebaseConfig = {
9+
apiKey: "YOUR_API_KEY",
10+
authDomain: "YOUR_AUTH_DOMAIN",
11+
projectId: "YOUR_PROJECT_ID",
12+
storageBucket: "YOUR_STORAGE_BUCKET",
13+
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
14+
appId: "YOUR_APP_ID"
15+
};
1516

1617
// Initialize Firebase
17-
firebase.initializeApp(firebaseConfig)
18+
initializeApp(firebaseConfig)
19+
20+
if (location.hostname === "localhost") {
21+
connectAuthEmulator(getAuth(), "http://localhost:9099");
22+
}
1823

1924
const app = createApp(App)
2025

src/views/Feed.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
</template>
55

66
<script setup>
7-
import firebase from 'firebase'
7+
import { getAuth,onAuthStateChanged } from 'firebase/auth'
88
import { useRouter } from 'vue-router'
99
import { onBeforeUnmount } from 'vue'
1010
1111
const router = useRouter()
12-
const authListener = firebase.auth().onAuthStateChanged(function(user) {
12+
const authListener = onAuthStateChanged(getAuth(),function(user) {
1313
if (!user) { // not logged in
1414
alert('you must be logged in to view this. redirecting to the home page')
1515
router.push('/')

src/views/Register.vue

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@
77

88
<script setup>
99
import { ref } from 'vue'
10-
import firebase from 'firebase'
10+
import { getAuth, createUserWithEmailAndPassword } from 'firebase/auth'
1111
import { useRouter } from 'vue-router' // import router
1212
1313
const email = ref('')
1414
const password = ref('')
1515
1616
const router = useRouter() // get a reference to our vue router
1717
const register = () => {
18-
firebase
19-
.auth() // get the auth api
20-
.createUserWithEmailAndPassword(email.value, password.value) // need .value because ref()
18+
createUserWithEmailAndPassword(getAuth(),email.value, password.value) // need .value because ref()
2119
.then((data) => {
2220
console.log('Successfully registered!');
2321
router.push('/feed') // redirect to the feed

src/views/SignIn.vue

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<script setup>
1010
import { ref } from 'vue'
11-
import firebase from 'firebase'
11+
import { getAuth, signInWithEmailAndPassword } from 'firebase/auth'
1212
import { useRouter } from 'vue-router' // import router
1313
1414
const email = ref('')
@@ -18,9 +18,7 @@ const errMsg = ref() // ERROR MESSAGE
1818
const router = useRouter() // get a reference to our vue router
1919
2020
const signIn = () => { // we also renamed this method
21-
firebase
22-
.auth()
23-
.signInWithEmailAndPassword(email.value, password.value) // THIS LINE CHANGED
21+
signInWithEmailAndPassword(getAuth(),email.value, password.value) // THIS LINE CHANGED
2422
.then((data) => {
2523
console.log('Successfully logged in!');
2624
router.push('/feed') // redirect to the feed

0 commit comments

Comments
 (0)