-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from velrest/authentication
feat(authentication): implement a login modal for the gwr api
- Loading branch information
Showing
23 changed files
with
399 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<UkModal | ||
@visible={{this.authFetch.showAuthModal}} | ||
@btnClose={{false}} | ||
@escClose={{false}} | ||
@bgClose={{false}} | ||
@container="#ember-ebau-gwr-modal-container" as |Modal| | ||
> | ||
<Modal.header> | ||
<h2 class="uk-modal-title"> | ||
{{t "ember-gwr.components.loginModal.header"}} | ||
</h2> | ||
</Modal.header> | ||
<Modal.body> | ||
<div class="uk-grid-small" uk-grid> | ||
<div class="uk-width-1-2"> | ||
<input | ||
name="username" | ||
class="uk-input" | ||
placeholder={{t "ember-gwr.components.loginModal.username"}} | ||
value={{this.username}} | ||
{{on "input" this.updateValue}} | ||
/> | ||
</div> | ||
<div class="uk-width-1-2"> | ||
<input | ||
name="password" | ||
type="password" | ||
class="uk-input" | ||
placeholder={{t "ember-gwr.components.loginModal.password"}} | ||
value={{this.password}} | ||
{{on "input" this.updateValue}} | ||
/> | ||
</div> | ||
</div> | ||
</Modal.body> | ||
<Modal.footer @class="uk-text-right"> | ||
<UkButton | ||
@color="primary" | ||
@disabled={{not (and this.username this.password)}} | ||
@loading={{this.authFetch.housingStatToken.isRunning}} | ||
{{on "click" this.login}} | ||
> | ||
{{t "ember-gwr.components.loginModal.login"}} | ||
</UkButton> | ||
</Modal.footer> | ||
</UkModal> | ||
|
||
{{#unless this.authFetch.showAuthModal}} | ||
{{yield}} | ||
{{/unless}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { action } from "@ember/object"; | ||
import { inject as service } from "@ember/service"; | ||
import Component from "@glimmer/component"; | ||
import { tracked } from "@glimmer/tracking"; | ||
|
||
export default class LoginModalComponent extends Component { | ||
@service authFetch; | ||
|
||
@tracked username = ""; | ||
@tracked password = ""; | ||
|
||
@action | ||
async login() { | ||
const token = await this.authFetch.housingStatToken.perform( | ||
this.username, | ||
this.password | ||
); | ||
if (token) { | ||
this.authFetch.showAuthModal = false; | ||
} | ||
} | ||
|
||
@action | ||
updateValue({ target: { name, value } = {} }) { | ||
this[name] = value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import Service, { inject as service } from "@ember/service"; | ||
import { tracked } from "@glimmer/tracking"; | ||
import { task, lastValue } from "ember-concurrency-decorators"; | ||
|
||
export default class AuthFetchService extends Service { | ||
@service config; | ||
@service intl; | ||
@service notification; | ||
|
||
@tracked showAuthModal = false; | ||
|
||
constructor(...args) { | ||
super(...args); | ||
this.housingStatToken.perform(); | ||
} | ||
|
||
@lastValue("housingStatToken") token; | ||
@task | ||
*housingStatToken(username, password) { | ||
const response = yield fetch(`/api/v1/housing-stat-token`, { | ||
method: "post", | ||
headers: { | ||
"content-type": "application/json", | ||
}, | ||
...(username && password | ||
? { | ||
body: JSON.stringify({ | ||
username, | ||
password, | ||
}), | ||
} | ||
: {}), | ||
}); | ||
|
||
const json = yield response.json(); | ||
|
||
if (!response.ok) { | ||
if (json["400"]?.source === "internal") { | ||
this.showAuthModal = true; | ||
} | ||
|
||
if (json["401"]?.source === "external") { | ||
this.notification.danger( | ||
this.intl.t("ember-gwr.authentication.loginError") | ||
); | ||
this.showAuthModal = true; | ||
} | ||
} | ||
|
||
return json.token; | ||
} | ||
|
||
async fetch(url, { method = "get", headers = {}, body } = {}, retry = false) { | ||
if (this.housingStatToken.isRunning) { | ||
await this.housingStatToken.lastRunning; | ||
} | ||
let response = await fetch(`${this.config.gwrAPI}${url}`, { | ||
method, | ||
headers: { | ||
token: this.token, | ||
...headers, | ||
}, | ||
body, | ||
}); | ||
if (!response.ok && response.status === 401 && !this.showAuthModal) { | ||
if (!retry) { | ||
await this.housingStatToken.perform(); | ||
response = await this.fetch(url, { method, headers, body }, false); | ||
} | ||
} | ||
return response; | ||
} | ||
} |
Oops, something went wrong.