Skip to content

Commit

Permalink
Finish conversion
Browse files Browse the repository at this point in the history
At least for the tests, just to get the unit tests to pass.
Eventually we can convert the entire addon code.
  • Loading branch information
paulcwatts committed Aug 19, 2020
1 parent a11cb8e commit 3fdd09d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 38 deletions.
71 changes: 35 additions & 36 deletions tests/dummy/app/components/index-auth.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import Component from '@ember/component';
import layout from '../templates/components/index-auth';
import { inject as service } from '@ember/service';
import { computed } from '@ember/object';
import { action, computed } from '@ember/object';
import { readOnly } from '@ember/object/computed';


function attributeEqual(attributeName, value) {
return computed('model.attributes', function() {
const attributes = this.model.attributes;
Expand All @@ -18,67 +17,67 @@ function attributeEqual(attributeName, value) {
})
}

export default Component.extend({
layout,
currentUser: service(),
cognito: service(),
router: service(),
session: service(),
cognitoUser: readOnly('cognito.user'),
export default class IndexAuth extends Component {
layout = layout;
@service currentUser;
@service cognito;
@service router;
@service session;
@readOnly('cognito.user') cognitoUser;

emailVerified: attributeEqual('email_verified', 'true'),
phoneNumberVerified: attributeEqual('phone_number_verified', 'true'),
@attributeEqual('email_verified', 'true') emailVerified;
@attributeEqual('phone_number_verified', 'true') phoneNumberVerified;

init() {
this._super(...arguments);
constructor() {
super(...arguments);
this.getSession();
},
}

getSession() {
async getSession() {
// Fetch the cognito session
let cognitoUser = this.cognitoUser;
if (cognitoUser) {
return cognitoUser.getSession().then((session) => {
// It can happen in acceptance tests that 'session' is falsey
if (session) {
this.set('cognitoSession', session);
}
});
const session = await cognitoUser.getSession();
// It can happen in acceptance tests that 'session' is falsey
if (session) {
this.set('cognitoSession', session);
}
}
},
}

tokenInfo(token) {
return {
expiration: new Date(token.getExpiration() * 1000),
formatted: JSON.stringify(token.payload, undefined, 2)
};
},
}

accessToken: computed('cognitoSession', function() {
@computed('cognitoSession')
get accessToken() {
let session = this.cognitoSession;
if (session) {
return this.tokenInfo(session.getAccessToken());
}
return undefined;
}),
}

idToken: computed('cognitoSession', function() {
@computed('cognitoSession')
get idToken() {
let session = this.cognitoSession;
if (session) {
return this.tokenInfo(session.getIdToken());
}
return undefined;
}),
}

authenticatedData: computed('session.data', function() {
@computed('session.data')
get authenticatedData() {
return JSON.stringify(this.session.data, undefined, 2);
}),
}

actions: {
verifyAttribute(attributeName) {
this.cognitoUser.getAttributeVerificationCode(attributeName).then(() => {
this.router.transitionTo('attribute-verify', { queryParams: { name: attributeName } });
});
}
@action
async verifyAttribute(attributeName) {
await this.cognitoUser.getAttributeVerificationCode(attributeName);
this.router.transitionTo('attribute-verify', { queryParams: { name: attributeName } });
}
});
}
4 changes: 2 additions & 2 deletions tests/dummy/app/templates/components/index-auth.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
Add attribute
</LinkTo>
{{#unless this.emailVerified}}
<button class="btn btn-light btn-block" onclick={{action "verifyAttribute" "email"}} type="button">
<button class="btn btn-light btn-block" {{on "click" (fn this.verifyAttribute "email")}} type="button">
Verify email
</button>
{{/unless}}
{{#unless this.phoneNumberVerified}}
<button class="btn btn-light btn-block" onclick={{action "verifyAttribute" "phone_number"}} type="button">
<button class="btn btn-light btn-block" {{on "click" (fn this.verifyAttribute "phone_number")}} type="button">
Verify phone number
</button>
{{/unless}}
Expand Down

0 comments on commit 3fdd09d

Please sign in to comment.