diff --git a/app/adapters/application.js b/app/adapters/application.js index 58e2f79c329edda79e026c9f33dff8816902b1b4..67c01f459a3052414449f8d208eb068d9a1f4209 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 eb958029b0cfb5bcdb8af6727fb5c6b8c0346661..b70e2724598fa6baca126afb795f4552e77ebe5c 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 30065f2ca4f634a0aa2ab4012c9d6fe16bf99834..db209110ebe7cfacc032d1f608261bd5d7c48d31 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 0000000000000000000000000000000000000000..cdf910f0d62afe56d830dae38fec018a0c5d0869 --- /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 0000000000000000000000000000000000000000..9bb1495666de2002cc487636f8fd8f21befe7333 --- /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