From 5e8b228be5f1437da5f36c8197fd56358db5c5eb Mon Sep 17 00:00:00 2001 From: Admir Sabanovic Date: Thu, 18 May 2017 17:09:16 +0200 Subject: [PATCH] some refactoring and basic authentication --- client/auth/authentication.js | 69 +++++++++ client/components/article.comp.js | 10 +- client/components/layout/c-nav.comp.js | 134 +++++++++++------ client/components/popular-tags.comp.js | 80 +++++++++++ client/index.js | 17 +++ client/pages/editor.comp.js | 29 ++++ client/pages/home.comp.js | 192 +++++++++++++++---------- client/pages/login.comp.js | 115 ++++++++++----- client/pages/register.comp.js | 4 +- client/pages/settings.comp.js | 35 +++++ client/router/router-handler.js | 8 ++ 11 files changed, 534 insertions(+), 159 deletions(-) create mode 100644 client/auth/authentication.js create mode 100644 client/components/popular-tags.comp.js create mode 100644 client/pages/editor.comp.js create mode 100644 client/pages/settings.comp.js diff --git a/client/auth/authentication.js b/client/auth/authentication.js new file mode 100644 index 0000000..f72bc53 --- /dev/null +++ b/client/auth/authentication.js @@ -0,0 +1,69 @@ +export class Authentication { + constructor() { + if (!Authentication.inst) { + Authentication.inst = this; + } else { + throw new Error('use instance'); + } + + var unparsedAuth = localStorage.getItem('auth'); + if (unparsedAuth) { + this._auth = JSON.parse(unparsedAuth); + } + this._callbacks = []; + return Authentication.inst; + } + + get auth() { + if (this._auth) { + var unparsedAuth = localStorage.getItem('auth'); + if (unparsedAuth) { + this._auth = JSON.parse(unparsedAuth); + } + } + return JSON.parse(localStorage.getItem('auth')); + } + + set auth(value) { + localStorage.setItem('auth', JSON.stringify(value)); + } + + onAuthenticate(callback) { + this._callbacks.push(callback); + } + + + doAuthentication(email, password) { + let user = { + 'user': { + 'email': email, 'password': password + } + }; + return new Promise((resolve, reject) => { + fetch('https://conduit.productionready.io/api/users/login', { + method: 'post', + headers: { + 'Accept': 'application/json, text/plain, */*', + 'Content-Type': 'application/json' + }, + body: JSON.stringify(user) + }).then(res=>res.json()).then(res => { + if (res.user) { + this.auth = res.user; + resolve(res.user); + setTimeout(() => { + this._callbacks.forEach(callback => callback(res.user)) + }); + } else { + reject(res.errors) + } + }); + }); + + } + + static get instance() { + return Authentication.inst; + } +} +Authentication.inst = null; diff --git a/client/components/article.comp.js b/client/components/article.comp.js index 947b17b..7300473 100644 --- a/client/components/article.comp.js +++ b/client/components/article.comp.js @@ -2,13 +2,14 @@ import {RouterHandler} from "../router/router-handler"; "use strict"; export class ArticleComponent extends HTMLElement { - +//todo generate tag list constructor() { super(); this.model = { author: '', heart: 0 }; + this.updateHearts = this.updateHearts.bind(this); } static get observedAttributes() { @@ -20,15 +21,14 @@ export class ArticleComponent extends HTMLElement { } disconnectedCallback() { - console.log('disconnected'); const button = this.querySelector('#ion-heart'); - button.removeEventListener('click', this.updateHearts.bind(this)); + button.removeEventListener('click', this.updateHearts); } connectedCallback() { this.innerHTML = this.render(); const button = this.querySelector('#ion-heart'); - button.addEventListener('click', this.updateHearts.bind(this)); + button.addEventListener('click', this.updateHearts); const authorButton = this.querySelector('#author'); authorButton.addEventListener('click', (e) => { @@ -70,7 +70,7 @@ export class ArticleComponent extends HTMLElement {

${this.model.title}

-

${this.model.description}

+

${this.model.description ? this.model.description : ''}

Read more...