Integrate Firestore database

This commit is contained in:
2022-10-19 21:10:49 +02:00
parent e582a1ab41
commit da9f105825
5 changed files with 72 additions and 19 deletions

View File

@@ -1,24 +1,62 @@
<template> <template>
<v-row> <v-container>
<v-col class="text-center"> <v-row>
<blockquote class="blockquote"> <v-col v-for="course, id in courses" :key="id">
&#8220;First, solve the problem. Then, write the code.&#8221; <v-card :width="vCardSize" :height="vCardSize" align="center" justify="center" @click="openCourse">
<footer> {{ id.toUpperCase() }} - {{ course.name }}
<small> </v-card>
<em>&mdash;John Johnson</em> </v-col>
</small> <v-col cols="12" class="text-center">
</footer> <blockquote class="blockquote">
</blockquote> &#8220;First, solve the problem. Then, write the code.&#8221;
</v-col> <footer>
</v-row> <small>
<em>&mdash;John Johnson</em>
</small>
</footer>
</blockquote>
</v-col>
</v-row>
</v-container>
</template> </template>
<script> <script>
import { collection, getDocs } from 'firebase/firestore'
export default { export default {
name: 'DashboardPage', name: 'DashboardPage',
layout ({ $auth }) { layout ({ $auth }) {
// Ref: https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth#currentuser // Ref: https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth#currentuser
return $auth.currentUser.emailVerified ? 'default' : 'unverified' return $auth.currentUser.emailVerified ? 'default' : 'unverified'
},
data () {
return {
vCardSize: 150
}
},
computed: {
courses () {
return this.$store.state.courses
}
},
created () {
// Load all courses from the database and convert to JS object
getDocs(collection(this.$db, 'kurse')).then(querySnapshot => {
const courses = {}
querySnapshot.forEach((doc) => {
// doc.data() is never undefined for query doc snapshots
courses[doc.id] = doc.data()
})
// Save courses in Vuex store
this.$store.commit('setCourses', courses)
})
},
methods: {
openCourse () {
// TODO
this.$toast({ content: 'TODO: Eigener Bereich für jeden Kurs.', color: 'info', timeout: 3000 })
}
} }
} }
</script> </script>

View File

@@ -18,9 +18,11 @@ export default {
this.$store.commit('initFirebase') this.$store.commit('initFirebase')
if (user) { if (user) {
// User is signed in; redirect to main page (dashboard) // User is signed in; redirect to main page (dashboard)
this.$store.commit('setUserLoggedIn', true)
this.$router.push({ name: 'dashboard' }) this.$router.push({ name: 'dashboard' })
} else { } else {
// User is signed out; redirect to login page // User is signed out; redirect to login page
this.$store.commit('setUserLoggedIn', false)
this.$router.push({ name: 'login' }) this.$router.push({ name: 'login' })
} }
}) })

View File

@@ -1,6 +1,6 @@
<template> <template>
<v-container> <v-container fill-height>
<v-card v-if="existingUser" max-width="500px" elevation="12" class="mx-auto"> <v-card v-if="existingUser" width="500px" elevation="12" class="mx-auto">
<v-card-text> <v-card-text>
<v-form ref="loginForm" v-model="valid"> <v-form ref="loginForm" v-model="valid">
<v-text-field <v-text-field
@@ -35,7 +35,7 @@
<v-btn text block color="primary" @click="showPwResetDialog = true">Passwort vergessen?</v-btn> <v-btn text block color="primary" @click="showPwResetDialog = true">Passwort vergessen?</v-btn>
</v-card-text> </v-card-text>
</v-card> </v-card>
<v-card v-else max-width="500px" elevation="12" class="mx-auto"> <v-card v-else width="500px" elevation="12" class="mx-auto">
<v-card-text> <v-card-text>
<v-form ref="signupForm" v-model="valid"> <v-form ref="signupForm" v-model="valid">
<v-text-field <v-text-field
@@ -98,7 +98,7 @@
</v-btn> </v-btn>
</v-card-text> </v-card-text>
</v-card> </v-card>
<v-dialog v-model="showPwResetDialog" max-width="500px"> <v-dialog v-model="showPwResetDialog" width="500px">
<v-card> <v-card>
<v-card-title> <v-card-title>
<span class="headline">Passwort zurücksetzen</span> <span class="headline">Passwort zurücksetzen</span>
@@ -139,7 +139,7 @@ export default {
email: '', email: '',
emailRules: [ emailRules: [
v => !!v || this.defaultErrorReqField, v => !!v || this.defaultErrorReqField,
v => /^\w+([.-]?\w+)*@(iu\.org|iubh-fernstudium\.de)+$/.test(v) || 'Keine gültige IU E-Mail-Adresse' v => /^\w+([.-]?\w+)*@(iubh\.de|iubh-fernstudium\.de|iu\.org)+$/.test(v) || 'Keine gültige IU E-Mail-Adresse'
], ],
password: '', password: '',
passwordRules: [ passwordRules: [

View File

@@ -1,5 +1,6 @@
import { initializeApp } from 'firebase/app' import { initializeApp } from 'firebase/app'
import { getAuth } from 'firebase/auth' import { getAuth } from 'firebase/auth'
import { getFirestore } from 'firebase/firestore'
// Firebase configuration // Firebase configuration
const firebaseConfig = { const firebaseConfig = {
@@ -17,8 +18,12 @@ const app = initializeApp(firebaseConfig)
// Initialize Firebase Authentication and get a reference to the service // Initialize Firebase Authentication and get a reference to the service
const auth = getAuth(app) const auth = getAuth(app)
// Initialize Cloud Firestore and get a reference to the service
const db = getFirestore(app)
export default ({ app }, inject) => { export default ({ app }, inject) => {
// Inject $auth in Vue, context and store. // Inject $auth and $db in Vue, context and store.
// Ref: https://nuxtjs.org/docs/directory-structure/plugins/ // Ref: https://nuxtjs.org/docs/directory-structure/plugins/
inject('auth', auth) inject('auth', auth)
inject('db', db)
} }

View File

@@ -1,5 +1,7 @@
export const state = () => ({ export const state = () => ({
firebaseInitialized: false firebaseInitialized: false,
userLoggedIn: false,
courses: []
}) })
export const getters = { export const getters = {
@@ -8,6 +10,12 @@ export const getters = {
export const mutations = { export const mutations = {
initFirebase (state) { initFirebase (state) {
state.firebaseInitialized = true state.firebaseInitialized = true
},
setUserLoggedIn (state, isLoggedIn) {
state.userLoggedIn = isLoggedIn
},
setCourses (state, courses) {
state.courses = courses
} }
} }