From 736754abaf3c6ea253e5b366b0bdd2905b945fde Mon Sep 17 00:00:00 2001 From: Eugen Ciur <eugen@papermerge.com> Date: Sat, 23 Oct 2021 08:22:04 +0200 Subject: [PATCH] Simple client side authentication --- app/adapters/application.js | 14 +++++++++++++ app/authenticators/auth-token.js | 33 ++++++++++++++++++++----------- app/components/nav/topbar.hbs | 4 +++- app/components/nav/topbar.js | 13 ++++++++++++ app/session-stores/application.js | 6 ++++++ 5 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 app/components/nav/topbar.js create mode 100644 app/session-stores/application.js diff --git a/app/adapters/application.js b/app/adapters/application.js index 58e2f79..67c01f4 100644 --- a/app/adapters/application.js +++ b/app/adapters/application.js @@ -1,10 +1,24 @@ import JSONAPIAdapter from '@ember-data/adapter/json-api'; +import { computed } from '@ember/object'; +import { inject as service } from '@ember/service'; + export default class ApplicationAdapter extends JSONAPIAdapter { namespace = 'api'; host = 'http://127.0.0.1:8000'; + @service session; buildURL(...args) { return `${super.buildURL(...args)}/`; } + + @computed('session.data.authenticated.token') + get headers() { + let _headers = {}; + if (this.session.isAuthenticated) { + _headers['Token'] = this.session.data.authenticated.token; + } + + return _headers; + } } diff --git a/app/authenticators/auth-token.js b/app/authenticators/auth-token.js index eb95802..b70e272 100644 --- a/app/authenticators/auth-token.js +++ b/app/authenticators/auth-token.js @@ -1,7 +1,27 @@ import Base from 'ember-simple-auth/authenticators/base'; -class AuthToken extends Base { +export default class AuthToken extends Base { + /* + Simple token based authenticator + + Sends to the server username and password. If credentials are valid, servers + sends back a token. Token string returned from the server is then passed + back and forth between client and server via `Token` header. + */ + + async restore(data) { + /** + * Restores session token from the cookie. + */ + let { token } = data; + + if (token) { + return data; + } else { + throw 'no valid session data'; + } + } async authenticate(username, password) { let response, error; @@ -21,15 +41,4 @@ class AuthToken extends Base { throw new Error(error.non_field_errors[0]); } } - - restore(data) { - //pass - } - - invalidate(data) { - //pass - } } - - -export default AuthToken; \ No newline at end of file diff --git a/app/components/nav/topbar.hbs b/app/components/nav/topbar.hbs index 30065f2..db20911 100644 --- a/app/components/nav/topbar.hbs +++ b/app/components/nav/topbar.hbs @@ -13,9 +13,11 @@ <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> + {{#if session.isAuthenticated}} <li class="nav-item"> - <a class="nav-link" href="#">Link</a> + <Button::Link @text="Logout" @onClick={{this.logout}} /> </li> + {{/if }} <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Dropdown diff --git a/app/components/nav/topbar.js b/app/components/nav/topbar.js new file mode 100644 index 0000000..cdf910f --- /dev/null +++ b/app/components/nav/topbar.js @@ -0,0 +1,13 @@ +import Component from '@glimmer/component'; +import { action } from '@ember/object'; +import {inject as service} from '@ember/service'; + + +export default class TopbarComponent extends Component { + @service session; + + @action + logout() { + this.session.invalidate(); + } +} \ No newline at end of file diff --git a/app/session-stores/application.js b/app/session-stores/application.js new file mode 100644 index 0000000..9bb1495 --- /dev/null +++ b/app/session-stores/application.js @@ -0,0 +1,6 @@ +import CookieStore from 'ember-simple-auth/session-stores/cookie'; + + +export default class ApplicationSessionStore extends CookieStore { + // pass +} \ No newline at end of file -- GitLab