From 891f2193799b01ddc34c9d49d4981eb5a7ee01eb Mon Sep 17 00:00:00 2001 From: sava Date: Fri, 27 Mar 2020 22:24:01 +0100 Subject: [PATCH] basic authentication --- index.html | 6 +- it1/bibliotek/bibliotek.js | 62 ++++++++++++------- it1/bibliotek/bok.js | 34 ++++++---- it1/bibliotek/index.html | 16 ++--- it1/bruker/index.html | 30 +++++++++ it1/{bibliotek/bibliotek.css => css/form.css} | 18 ++++-- it1/index.html | 13 ++-- it1/js/auth.js | 18 ++++++ it1/{ => js}/init-firebase.js | 2 - it1/js/navbar.js | 43 +++++++++++++ it1/main.css | 9 +++ it1/navbar/navbar.js | 28 --------- main.css | 5 ++ 13 files changed, 197 insertions(+), 87 deletions(-) create mode 100644 it1/bruker/index.html rename it1/{bibliotek/bibliotek.css => css/form.css} (58%) create mode 100644 it1/js/auth.js rename it1/{ => js}/init-firebase.js (92%) create mode 100644 it1/js/navbar.js delete mode 100644 it1/navbar/navbar.js diff --git a/index.html b/index.html index 673d6f6..cf3df0f 100644 --- a/index.html +++ b/index.html @@ -6,10 +6,10 @@ - +

Noting to see here

-

You can check out work I did in IT1 (informasjons teknologi 1) a Norwegian optional vacational course here.

+

You can check out work I did in IT1 (informasjons teknologi 1) a Norwegian optional vacational course here.

-
+ \ No newline at end of file diff --git a/it1/bibliotek/bibliotek.js b/it1/bibliotek/bibliotek.js index 593bf21..ab07645 100644 --- a/it1/bibliotek/bibliotek.js +++ b/it1/bibliotek/bibliotek.js @@ -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 = '
' query.forEach(function(doc) { book = doc.data() - html += book.toHtml() + html += book.toHtml(user) }) $('#books').html(html) - }) - .catch(function(error) { - $('#books').html('

Kunne ikke hente bøker!

'+error+'

') + }).catch(function(error) { + $('#books').html('

Kunne ikke hente bøker!


'+error+'

') $('#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('

La til bok '+$('#title')[0].value+'!

') - $('#status').css('color', 'green') - }).catch(function() { - $('#status').html('

Problem med å legge til bok '+$('#title')[0].value+'!

') - $('#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('

La til bok '+$('#title')[0].value+'!

') + $('#status').css('color', 'green') + list(firebase.auth().currentUser) + }).catch(function() { + $('#status').html('

Problem med å legge til bok '+$('#title')[0].value+'!

') + $('#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) +}) +*/ + diff --git a/it1/bibliotek/bok.js b/it1/bibliotek/bok.js index a769e24..fbd38ba 100644 --- a/it1/bibliotek/bok.js +++ b/it1/bibliotek/bok.js @@ -1,37 +1,47 @@ class Book { - constructor(title, author, publisher, rating, published) { + constructor(title, author, publisher, rating, published, id=0) { + this.cfid = id this.title = title this.author = author this.publisher = publisher this.rating = rating - this.published = published.toDate() + this.published = published } toString() { - return this.title+' av '+this.author+', ternignkast '+this.rating+', utgitt '+this.published.toString().slice(0,15)+', forlag; '+this.publisher + return this.title+' av '+this.author+', terningkast '+this.rating+', utgitt '+this.published.toString().slice(0,15)+', forlag; '+this.publisher } - toHtml() { + toHtml(user=false) { let span = function(string, color='red') { return ''+string+'' } - return '

'+span(this.title)+' av '+span(this.author)+', ternignkast '+span(this.rating)+ - ', utgitt '+span(this.published.toString().slice(0,15), 'green')+', forlag; '+span(this.publisher, 'yellow')+'

' + return '

'+span(this.title)+' av '+span(this.author)+', terningkast '+span(this.rating)+', utgitt ' + +span(this.published.toString().slice(0,15), 'green')+', forlag; '+span(this.publisher, 'yellow') + +(user?'   Slett':'')+'

' } } bookConverter = { toFirestore: function(book) { + let millis = Date.parse(book.published) return { - title: book.title, - author: book.author, - publisher: book.publisher, - rating: book.rating, - published: book.published + tittel: book.title, + forfatter: book.author, + forlag: book.publisher, + terningkast: book.rating, + utgitt: new firebase.firestore.Timestamp(millis/1000, 0) } }, fromFirestore: function(snapshot, options) { const data = snapshot.data(options) - return new Book(data.tittel, data.forfatter, data.forlag, data.terningkast, data.utgitt) + return new Book( + data.tittel, + data.forfatter, + data.forlag, + data.terningkast, + data.utgitt.toDate(), + snapshot.id + ) } } diff --git a/it1/bibliotek/index.html b/it1/bibliotek/index.html index 94638e0..a76ecb3 100644 --- a/it1/bibliotek/index.html +++ b/it1/bibliotek/index.html @@ -4,16 +4,18 @@ Firebase basert bibliotek - - - + + + + + - + - -
+ +
@@ -37,7 +39,7 @@

 

- Hent bøker + Hent bøker
diff --git a/it1/bruker/index.html b/it1/bruker/index.html new file mode 100644 index 0000000..349fc2e --- /dev/null +++ b/it1/bruker/index.html @@ -0,0 +1,30 @@ + + + + Firebase basert bibliotek + + + + + + + + + + + + +
+ +
+ + +
+ Logg inn + +
+
+
+ + + \ No newline at end of file diff --git a/it1/bibliotek/bibliotek.css b/it1/css/form.css similarity index 58% rename from it1/bibliotek/bibliotek.css rename to it1/css/form.css index 7f9763f..815a6ea 100644 --- a/it1/bibliotek/bibliotek.css +++ b/it1/css/form.css @@ -1,17 +1,14 @@ #content { margin: 2vh; + margin-left: 13vw; + margin-right: 42.0vw; } .top { margin-top: 9vh; } -.middle { - margin-left: 13vw; - margin-right: 42.0vw; -} - #status { margin-top: 1vh; } @@ -24,3 +21,14 @@ margin-bottom: 0.33vh; } +.form-control { + background-color: #333; + border-color: #222; + color: lime; +} +.form-control:focus { + background-color: #555; + border-color: #333; + color: lime; +} + diff --git a/it1/index.html b/it1/index.html index 1057215..e9a8eab 100644 --- a/it1/index.html +++ b/it1/index.html @@ -1,20 +1,19 @@ - Simple firebase logon + Simple firebase stuff - + + + + - +

Denne siden inneholder ting jeg har laget med firebase i IT1!


diff --git a/it1/js/auth.js b/it1/js/auth.js new file mode 100644 index 0000000..b634540 --- /dev/null +++ b/it1/js/auth.js @@ -0,0 +1,18 @@ + +var signin = function() { + firebase.auth() + .signInWithEmailAndPassword($('#mail')[0].value, $('#password')[0].value) + .then(function() { + document.location.href = "/it1/" + }).catch(function(error) { + $('#status').html('

Kunne ikke logge inn som '+$('#mail')[0].value+'!

'+error.code+': '+error.message+'

') + $('#status').css('color', 'red') + }) +} + +$(document).keydown(function(e) { + if (e.which == 13){ + $("#login").click() + } +}) + diff --git a/it1/init-firebase.js b/it1/js/init-firebase.js similarity index 92% rename from it1/init-firebase.js rename to it1/js/init-firebase.js index 18b9816..703589d 100644 --- a/it1/init-firebase.js +++ b/it1/js/init-firebase.js @@ -8,5 +8,3 @@ firebase.initializeApp({ messagingSenderId: "206703382262", appId: "1:206703382262:web:be48f32d11a15fa28f26d7" }) -var db = firebase.firestore() - diff --git a/it1/js/navbar.js b/it1/js/navbar.js new file mode 100644 index 0000000..23bd7d9 --- /dev/null +++ b/it1/js/navbar.js @@ -0,0 +1,43 @@ + +$('#navbar').parent().html(` +`) + +var signout = function() { + firebase.auth() + .signOut() + .then(function() { + document.location.href = '/it1/' + }).catch(function(error) { + alert('could not log out: '+error.message) + }) +} + +firebase.auth().onAuthStateChanged(function(user) { + if (user) { + $('#login-btn').attr('href', 'javascript: signout()') + $('#login-btn').html('Logout') + } else { + $('#login-btn').attr('href', '/it1/bruker/login') + $('#login-btn').html('Login') + } +}) + +for (var i = 0; i < $('#navbar').children().length; i++) { + $('#navbar').children().css('color', 'springgreen') +} + diff --git a/it1/main.css b/it1/main.css index 278634a..7383d54 100644 --- a/it1/main.css +++ b/it1/main.css @@ -1,4 +1,13 @@ +body { + background-color: rgb(24, 26, 27); + color: rgb(218, 215, 210); +} + +li.right > ul { /* TODO: FIX! */ + align-content: right; +} + #oppgaver { margin-left: 3vh; } diff --git a/it1/navbar/navbar.js b/it1/navbar/navbar.js deleted file mode 100644 index f8043e0..0000000 --- a/it1/navbar/navbar.js +++ /dev/null @@ -1,28 +0,0 @@ -$('#navbar').parent().html(` -`) - -for (var i = 0; i < $('#navbar').children().length; i++) { - $('#navbar').children().css('color', 'green') -} - diff --git a/main.css b/main.css index 47dd81e..312ef6d 100644 --- a/main.css +++ b/main.css @@ -1,4 +1,9 @@ +body { + background-color: rgb(24, 26, 27); + color: lime; +} + div#content > * { padding: 2vh; margin: 4vh;