diff --git a/app/adapters/application.js b/app/adapters/application.js index eb97d0f..88e8865 100644 --- a/app/adapters/application.js +++ b/app/adapters/application.js @@ -5,14 +5,21 @@ import { inject as service } from '@ember/service'; export default class ApplicationAdapter extends JSONAPIAdapter { @service session; - namespace = 'api/v1'; + get namespace() { + if (this.session.isAuthenticated) { + return 'api/v1'; + } else { + return 'api/v1/public'; + } + } @computed('session.{data.authenticated.access_token,isAuthenticated}') get headers() { let headers = {}; if (this.session.isAuthenticated) { // OAuth 2 - headers['Authorization'] = `Bearer ${this.session.data.authenticated.access_token}`; + headers['Authorization'] = + `Bearer ${this.session.data.authenticated.access_token}`; } return headers; diff --git a/app/authenticators/jwt.js b/app/authenticators/jwt.js index 3b1d724..255f2a4 100644 --- a/app/authenticators/jwt.js +++ b/app/authenticators/jwt.js @@ -82,7 +82,7 @@ export default BaseAuthenticator.extend({ */ restore(data) { if (this._refreshTokenTimeout) { - run.cancel(this._refreshTokenTimeout); + cancel(this._refreshTokenTimeout); delete this._refreshTokenTimeout; } return this._refreshAccessToken(data); @@ -106,13 +106,14 @@ export default BaseAuthenticator.extend({ */ authenticate(identification, password) { return new RSVP.Promise((resolve, reject) => { - const { - identificationAttributeName, - passwordAttributeName - } = this.getProperties('identificationAttributeName', 'passwordAttributeName'); + const { identificationAttributeName, passwordAttributeName } = + this.getProperties( + 'identificationAttributeName', + 'passwordAttributeName', + ); const data = { [identificationAttributeName]: identification, - [passwordAttributeName]: password + [passwordAttributeName]: password, }; const serverTokenEndpoint = this.get('serverTokenEndpoint'); @@ -141,7 +142,7 @@ export default BaseAuthenticator.extend({ @public */ invalidate() { - run.cancel(this._refreshTokenTimeout); + cancel(this._refreshTokenTimeout); delete this._refreshTokenTimeout; return RSVP.Promise.resolve(); }, @@ -161,22 +162,24 @@ export default BaseAuthenticator.extend({ const options = { body: JSON.stringify(data), headers, - method: 'POST' + method: 'POST', }; return new RSVP.Promise((resolve, reject) => { - fetch(url, options).then((response) => { - response.text().then((text) => { - let json = text ? JSON.parse(text) : {}; - if (!response.ok) { - response.responseJSON = json; - reject(response); - } else { - window.localStorage.setItem('jwtLastRefreshAt', Date.now()); - resolve(json); - } - }); - }).catch(reject); + fetch(url, options) + .then((response) => { + response.text().then((text) => { + let json = text ? JSON.parse(text) : {}; + if (!response.ok) { + response.responseJSON = json; + reject(response); + } else { + window.localStorage.setItem('jwtLastRefreshAt', Date.now()); + resolve(json); + } + }); + }) + .catch(reject); }); }, @@ -219,7 +222,7 @@ export default BaseAuthenticator.extend({ const offset = 1000; // Refresh 1 sec before JWT expires const now = Date.now(); - const waitMs = (jwtPayloadExpiresAt * 1000) - now - offset; //expiresAt is in sec + const waitMs = jwtPayloadExpiresAt * 1000 - now - offset; //expiresAt is in sec if (this._refreshTokenTimeout) { cancel(this._refreshTokenTimeout); @@ -228,12 +231,18 @@ export default BaseAuthenticator.extend({ // Reschedule if the JWT is still valid if (waitMs > 0) { - this._refreshTokenTimeout = later(this, this._refreshAccessToken, data, waitMs); + this._refreshTokenTimeout = later( + this, + this._refreshAccessToken, + data, + waitMs, + ); } }, _refreshAccessToken(data) { - var timeElapsedSinceLastRefresh = Date.now() - window.localStorage.getItem('jwtLastRefreshAt') + var timeElapsedSinceLastRefresh = + Date.now() - window.localStorage.getItem('jwtLastRefreshAt'); if (timeElapsedSinceLastRefresh <= this.get('refreshTokenAfter')) { // Request attempted too soon! Reschedule return this._validateTokenAndScheduleRefresh(data); @@ -242,7 +251,9 @@ export default BaseAuthenticator.extend({ const serverRefreshTokenEndpoint = this.get('serverRefreshTokenEndpoint'); return new RSVP.Promise((resolve, reject) => { - this.makeRequest(serverRefreshTokenEndpoint, data, { Authorization:data["access_token"] }) + this.makeRequest(serverRefreshTokenEndpoint, data, { + Authorization: data['access_token'], + }) .then((response) => { return this._validateTokenAndScheduleRefresh(response); }) @@ -257,7 +268,7 @@ export default BaseAuthenticator.extend({ reason = JSON.stringify(reason.responseJSON); } warn(`JWT token could not be refreshed: ${reason}.`, false, { - id: 'ember-simple-auth-jwt.failedJWTTokenRefresh' + id: 'ember-simple-auth-jwt.failedJWTTokenRefresh', }); reject(); @@ -267,10 +278,12 @@ export default BaseAuthenticator.extend({ _validateTokenAndScheduleRefresh(response) { if (!this._validate(response)) { - return RSVP.Promise.reject('token is missing or invalid in server response'); + return RSVP.Promise.reject( + 'token is missing or invalid in server response', + ); } this._scheduleTokenRefresh(response); return RSVP.Promise.resolve(response); - } + }, }); diff --git a/app/components/topbar/item.hbs b/app/components/topbar/item.hbs new file mode 100644 index 0000000..7c7e0ce --- /dev/null +++ b/app/components/topbar/item.hbs @@ -0,0 +1,2 @@ + +{{@title}} diff --git a/app/components/topbar/navbar.hbs b/app/components/topbar/navbar.hbs new file mode 100644 index 0000000..15f32e2 --- /dev/null +++ b/app/components/topbar/navbar.hbs @@ -0,0 +1,218 @@ + \ No newline at end of file diff --git a/app/components/topbar/profile-item.hbs b/app/components/topbar/profile-item.hbs new file mode 100644 index 0000000..a597339 --- /dev/null +++ b/app/components/topbar/profile-item.hbs @@ -0,0 +1,8 @@ + +{{@title}} \ No newline at end of file diff --git a/app/index.html b/app/index.html index ac0d9f3..f182430 100644 --- a/app/index.html +++ b/app/index.html @@ -5,6 +5,7 @@