Add database integration
This commit is contained in:
@@ -6,8 +6,8 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { onAuthStateChanged } from 'firebase/auth'
|
import { onAuthStateChanged } from 'firebase/auth'
|
||||||
import { doc, getDoc } from 'firebase/firestore'
|
import { doc, getDoc, setDoc } from 'firebase/firestore'
|
||||||
import { UserConverter } from '~/plugins/user'
|
import { User, UserConverter } from '~/plugins/user'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'IndexPage',
|
name: 'IndexPage',
|
||||||
@@ -33,12 +33,31 @@ export default {
|
|||||||
const ref = doc(this.$db, `benutzer/${this.$auth.currentUser.uid}`).withConverter(UserConverter)
|
const ref = doc(this.$db, `benutzer/${this.$auth.currentUser.uid}`).withConverter(UserConverter)
|
||||||
getDoc(ref).then((docSnap) => {
|
getDoc(ref).then((docSnap) => {
|
||||||
if (docSnap.exists()) {
|
if (docSnap.exists()) {
|
||||||
this.$store.commit('setUser', docSnap.data())
|
// Successfully retrieved user data from the db.
|
||||||
this.$router.push({ name: 'dashboard' })
|
this.redirectToDashboard(docSnap.data())
|
||||||
} else {
|
} else {
|
||||||
this.$toast({ content: 'Benutzer konnte in DB nicht gefunden werden!', color: 'error' })
|
// No user data was found in the database, which means the user has just signed up.
|
||||||
|
this.createUser()
|
||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
},
|
||||||
|
createUser () {
|
||||||
|
const user = new User()
|
||||||
|
|
||||||
|
// Add a new document in collection "users"
|
||||||
|
setDoc(doc(this.$db, 'benutzer', this.$auth.currentUser.uid).withConverter(UserConverter), user)
|
||||||
|
.then((docSnap) => {
|
||||||
|
this.redirectToDashboard()
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
// Couldn't create user data document
|
||||||
|
this.$toast({ content: error, color: 'error' })
|
||||||
|
})
|
||||||
|
},
|
||||||
|
redirectToDashboard (user) {
|
||||||
|
this.$store.commit('setUser', user)
|
||||||
|
this.$router.push({ name: 'dashboard' })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -163,7 +163,6 @@ export default {
|
|||||||
|
|
||||||
createUserWithEmailAndPassword(this.$auth, this.email, this.password)
|
createUserWithEmailAndPassword(this.$auth, this.email, this.password)
|
||||||
.then((userCredential) => {
|
.then((userCredential) => {
|
||||||
// TODO: database integration
|
|
||||||
// User signed up successfully.
|
// User signed up successfully.
|
||||||
// The authentication state observer will redirect the user to the main page (dashboard),
|
// The authentication state observer will redirect the user to the main page (dashboard),
|
||||||
// see pages/index.vue
|
// see pages/index.vue
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ export class User {
|
|||||||
constructor (gamesStarted) {
|
constructor (gamesStarted) {
|
||||||
this.gamesStarted = []
|
this.gamesStarted = []
|
||||||
|
|
||||||
gamesStarted.forEach(e => {
|
if (gamesStarted && gamesStarted.length > 0) {
|
||||||
this.gamesStarted.push({ course: e.kurs, game: e.spiel })
|
gamesStarted.forEach(e => {
|
||||||
})
|
this.gamesStarted.push({ course: e.kurs, game: e.spiel })
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user