basic authentication

This commit is contained in:
sava
2020-03-27 22:24:01 +01:00
parent 8676f94dcc
commit 891f219379
13 changed files with 197 additions and 87 deletions

View File

@@ -1,44 +1,60 @@
var firestore = firebase.firestore
var db = firebase.firestore()
var list = function() {
var list = function(user) {
// db.collection('bibliotek').doc("8ZBX5gmIaRwQBCjgLLg0").get().then(function(doc){console.log(doc.data())})
// db.collection('bibliotek').get().then(function(query){query.forEach(function(doc){console.log(doc.data())})})
db.collection('bibliotek')
.withConverter(bookConverter)
.get()
.then(function(query) {
html = ''
html = '<br/>'
query.forEach(function(doc) {
book = doc.data()
html += book.toHtml()
html += book.toHtml(user)
})
$('#books').html(html)
})
.catch(function(error) {
$('#books').html('<h3>Kunne ikke hente bøker!</h3><p>'+error+'</p>')
}).catch(function(error) {
$('#books').html('<h3>Kunne ikke hente bøker!</h3><br/><p>'+error+'</p>')
$('#books').css('color', 'red')
})
}
var add = function() {
let millis = Date.parse($('#published')[0].value)
db.collection("bibliotek").add({ // withConverter ?
tittel: $('#title')[0].value,
forfatter: $('#author')[0].value,
forlag: $('#publisher')[0].value,
terningkast: $('#rating')[0].value,
utgitt: new firestore.Timestamp(millis/1000, 0)
}).then(function() {
$('#status').html('<p>La til bok '+$('#title')[0].value+'!</p>')
$('#status').css('color', 'green')
}).catch(function() {
$('#status').html('<p>Problem med å legge til bok '+$('#title')[0].value+'!</p>')
$('#status').css('color', 'red')
})
db.collection("bibliotek")
.withConverter(bookConverter)
.add(new Book(
$('#title')[0].value,
$('#author')[0].value,
$('#publisher')[0].value,
$('#rating')[0].value,
$('#published')[0].value
)).then(function() {
$('#status').html('<p>La til bok '+$('#title')[0].value+'!</p>')
$('#status').css('color', 'green')
list(firebase.auth().currentUser)
}).catch(function() {
$('#status').html('<p>Problem med å legge til bok '+$('#title')[0].value+'!</p>')
$('#status').css('color', 'red')
})
}
$(document).ready(function() {
list()
var remove = function(id) {
db.collection("bibliotek").doc(id)
.delete().then(function() {
list(firebase.auth().currentUser)
}).catch(function(error) {
alert("Error removing document: ", error)
})
}
firebase.auth().onAuthStateChanged(function(user) {
list(user)
})
/*
$(document).ready(function() {
list(firebase.auth().currentUser)
})
*/