Skip to content

Commit

Permalink
JS Lint fixes
Browse files Browse the repository at this point in the history
This mainly converts to ES5 getters, which we can do since we no longer support anything less than Ember 3.12.
  • Loading branch information
paulcwatts committed Aug 19, 2020
1 parent 136cf77 commit b2b96c8
Show file tree
Hide file tree
Showing 23 changed files with 94 additions and 98 deletions.
6 changes: 3 additions & 3 deletions addon-test-support/utils/-mock-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ export default EmberObject.extend({
},

signUp() {
const user = this.get('_authenticatedUser');
const user = this._authenticatedUser;
return resolve({ user, userConfirmed: false, userSub: "xxxx" });
},

_resolveAuthedUser(msg) {
const user = this.get('_authenticatedUser');
const user = this._authenticatedUser;
if (user) {
return resolve(user);
} else {
Expand All @@ -65,7 +65,7 @@ export default EmberObject.extend({
},

currentSession() {
const user = this.get('_authenticatedUser');
const user = this._authenticatedUser;
if (user) {
return resolve(newSession());
} else {
Expand Down
16 changes: 8 additions & 8 deletions addon-test-support/utils/ember-cognito.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CognitoUserAttribute } from 'amazon-cognito-identity-js';
import EmberObject, { get } from '@ember/object';
import EmberObject from '@ember/object';
import { typeOf } from '@ember/utils';
import { resolve } from 'rsvp';
import { newSession } from "./-mock-auth";
Expand Down Expand Up @@ -35,17 +35,17 @@ const MockUser = EmberObject.extend({
},

getSession() {
return resolve(this.get('session') || newSession());
return resolve(this.session || newSession());
},

getUserAttributes() {
return resolve(get(this, 'userAttributes').map(({ name, value }) => {
return resolve(this.userAttributes.map(({ name, value }) => {
return new CognitoUserAttribute({ Name: name, Value: value });
}));
},

getUserAttributesHash() {
return resolve(get(this, 'userAttributes').reduce((acc, { name, value }) => {
return resolve(this.userAttributes.reduce((acc, { name, value }) => {
acc[name] = value;
return acc;
}, {}));
Expand All @@ -60,7 +60,7 @@ const MockUser = EmberObject.extend({
},

_updateAttrsList(attributesList) {
let attrs = this.get('userAttributes');
let attrs = this.userAttributes;
attributesList.forEach((updated) => {
let found = false;
attrs.forEach((existing) => {
Expand All @@ -77,7 +77,7 @@ const MockUser = EmberObject.extend({
},

_updateAttrsHash(attributes) {
let attrs = this.get('userAttributes');
let attrs = this.userAttributes;
Object.keys(attributes).forEach((name) => {
let found = false;
attrs.forEach((existing) => {
Expand Down Expand Up @@ -110,12 +110,12 @@ const MockUser = EmberObject.extend({

// Non-AWS method
getGroups() {
return resolve(get(this, 'groups'));
return resolve(this.groups);
},

// Non-AWS method
getStorageData() {
return get(this, 'storageData');
return this.storageData;
}
});

Expand Down
24 changes: 11 additions & 13 deletions addon/authenticators/cognito.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { get } from '@ember/object';
import { readOnly } from '@ember/object/computed';
import { inject as service } from '@ember/service';
import Base from 'ember-simple-auth/authenticators/base';
Expand All @@ -12,11 +11,11 @@ export default Base.extend({
authenticationFlowType: readOnly('cognito.authenticationFlowType'),

restore({ poolId, clientId }) {
this.get('cognito').configure({
this.cognito.configure({
userPoolId: poolId,
userPoolWebClientId: clientId
});
return this.get('auth').currentAuthenticatedUser().then((user) => {
return this.auth.currentAuthenticatedUser().then((user) => {
return this._resolveAuth(user);
});
},
Expand All @@ -30,10 +29,10 @@ export default Base.extend({
},

_resolveAuth(user) {
const cognito = this.get('cognito');
const { cognito } = this;
cognito._setUser(user);
// Now pull out the (promisified) user
return get(cognito, 'user').getSession().then((session) => {
return cognito.user.getSession().then((session) => {
/* eslint-disable camelcase */
cognito.startRefreshTask(session);
return this._makeAuthData(user, session);
Expand All @@ -55,21 +54,21 @@ export default Base.extend({
},

_handleNewPasswordRequired({ password, state: { user } }) {
return this.get('auth').completeNewPassword(user, password).then((user) => {
return this.auth.completeNewPassword(user, password).then((user) => {
return this._handleSignIn(user);
});
},

_handleRefresh() {
const cognito = this.get('cognito');
const user = get(cognito, 'user');
const { cognito } = this;
const { auth, user } = cognito;
// Get the session, which will refresh it if necessary
return user.getSession().then((session) => {
if (session.isValid()) {
/* eslint-disable camelcase */

cognito.startRefreshTask(session);
return get(cognito, 'auth').currentAuthenticatedUser().then((awsUser) => {
return auth.currentAuthenticatedUser().then((awsUser) => {
return this._makeAuthData(awsUser, session);
});
} else {
Expand All @@ -94,17 +93,16 @@ export default Base.extend({
return this._handleState(state.name, params);
}

const { auth, authenticationFlowType } =
this.getProperties('auth', 'authenticationFlowType');
this.get('cognito').configure({ authenticationFlowType });
const { auth, authenticationFlowType, cognito } = this;
cognito.configure({ authenticationFlowType });

return auth.signIn(username, password).then((user) => {
return this._handleSignIn(user);
});
},

invalidate(data) {
return this.get('cognito.user').signOut().then(() => {
return this.cognito.user.signOut().then(() => {
this.set('cognito.user', undefined);
return data;
});
Expand Down
26 changes: 13 additions & 13 deletions addon/services/cognito.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ export default Service.extend({
* @param awsconfig Extra AWS configuration.
*/
configure(awsconfig) {
const { poolId, clientId } = this.getProperties('poolId', 'clientId');
const { poolId, clientId } = this;
const params = Object.assign({
userPoolId: poolId,
userPoolWebClientId: clientId,
}, awsconfig);

this.get('auth').configure(params);
this.auth.configure(params);
},

/**
Expand All @@ -45,7 +45,7 @@ export default Service.extend({
signUp(username, password, attributes, validationData) {
this.configure();

return this.get('auth').signUp({
return this.auth.signUp({
username,
password,
attributes: normalizeAttributes(attributes),
Expand All @@ -65,7 +65,7 @@ export default Service.extend({
*/
confirmSignUp(username, code, options) {
this.configure();
return this.get('auth').confirmSignUp(username, code, options);
return this.auth.confirmSignUp(username, code, options);
},

/**
Expand All @@ -75,7 +75,7 @@ export default Service.extend({
*/
resendSignUp(username) {
this.configure();
return this.get('auth').resendSignUp(username);
return this.auth.resendSignUp(username);
},

/**
Expand All @@ -85,7 +85,7 @@ export default Service.extend({
*/
forgotPassword(username) {
this.configure();
return this.get('auth').forgotPassword(username);
return this.auth.forgotPassword(username);
},

/**
Expand All @@ -97,14 +97,14 @@ export default Service.extend({
*/
forgotPasswordSubmit(username, code, newPassword) {
this.configure();
return this.get('auth').forgotPasswordSubmit(username, code, newPassword);
return this.auth.forgotPasswordSubmit(username, code, newPassword);
},

/**
* Enable the token refresh timer.
*/
startRefreshTask(session) {
if (!this.get('autoRefreshSession')) {
if (!this.autoRefreshSession) {
return;
}
// Schedule a task for just past when the token expires.
Expand All @@ -120,23 +120,23 @@ export default Service.extend({
* Disable the token refresh timer.
*/
stopRefreshTask() {
cancel(this.get('task'));
cancel(this.task);
this.set('task', undefined);
this.set('_taskDuration', undefined);
},

refreshSession() {
let user = this.get('user');
let user = this.user;
if (user) {
return this.get('session').authenticate('authenticator:cognito', { state: { name: 'refresh' } });
return this.session.authenticate('authenticator:cognito', { state: { name: 'refresh' } });
}
},

/**
* A helper that resolves to the logged in user's id token.
*/
getIdToken() {
const user = this.get('user');
const user = this.user;
if (user) {
return user.getSession().then((session) => {
return session.getIdToken().getJwtToken();
Expand All @@ -148,7 +148,7 @@ export default Service.extend({

_setUser(awsUser) {
// Creates and sets the Cognito user.
const user = CognitoUser.create({ auth: this.get('auth'), user: awsUser });
const user = CognitoUser.create({ auth: this.auth, user: awsUser });
this.set('user', user);
return user;
}
Expand Down
28 changes: 14 additions & 14 deletions addon/utils/cognito-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import { normalizeAttributes } from "./utils";
//
export default EmberObject.extend({
username: computed('user', function() {
return this.get('user').getUsername();
return this.user.getUsername();
}),
attributes: readOnly("user.attributes"),

_callback(method, ...args) {
return new Promise((resolve, reject) => {
try {
this.get('user')[method](...args, (err, result) => {
this.user[method](...args, (err, result) => {
if (err) {
reject(err);
} else {
Expand All @@ -30,7 +30,7 @@ export default EmberObject.extend({
},

changePassword(oldPassword, newPassword) {
const { auth, user } = this.getProperties('auth', 'user');
const { auth, user } = this;
return auth.changePassword(user, oldPassword, newPassword);
},

Expand All @@ -41,7 +41,7 @@ export default EmberObject.extend({
{ id: 'ember-cognito-confirm-password', until: '1.0' }
);

const { auth, username } = this.getProperties('auth', 'username');
const { auth, username } = this;
return auth.forgotPasswordSubmit(username, verificationCode, newPassword);
},

Expand All @@ -52,7 +52,7 @@ export default EmberObject.extend({
{ id: 'ember-cognito-confirm-registration', until: '1.0' }
);

const { auth, username } = this.getProperties('auth', 'username');
const { auth, username } = this;
const options = forceAliasCreation ? { forceAliasCreation : true } : undefined;
return auth.confirmSignUp(username, confirmationCode, options);
},
Expand All @@ -72,26 +72,26 @@ export default EmberObject.extend({
{ id: 'ember-cognito-forgot-password', until: '1.0' }
);

const { auth, username } = this.getProperties('auth', 'username');
const { auth, username } = this;
return auth.forgotPassword(username);
},

getAttributeVerificationCode(attributeName) {
const { auth, user } = this.getProperties('auth', 'user');
const { auth, user } = this;
return auth.verifyUserAttribute(user, attributeName);
},

getSession() {
return this.get('auth').currentSession();
return this.auth.currentSession();
},

getUserAttributes() {
const { auth, user } = this.getProperties('auth', 'user');
const { auth, user } = this;
return auth.userAttributes(user);
},

getUserAttributesHash() {
const { auth, user } = this.getProperties('auth', 'user');
const { auth, user } = this;
return auth.userAttributes(user).then((result) => {
return normalizeAttributes(result, false);
});
Expand All @@ -104,22 +104,22 @@ export default EmberObject.extend({
{ id: 'ember-cognito-resend-confirmation-code', until: '1.0' }
);

const { auth, username } = this.getProperties('auth', 'username');
const { auth, username } = this;
return auth.resendSignUp(username);
},

signOut() {
return this.get('auth').signOut();
return this.auth.signOut();
},

updateAttributes(attributes) {
const { auth, user } = this.getProperties('auth', 'user');
const { auth, user } = this;
const normalized = normalizeAttributes(attributes);
return auth.updateUserAttributes(user, normalized);
},

verifyAttribute(attributeName, confirmationCode) {
const { auth, user } = this.getProperties('auth', 'user');
const { auth, user } = this;
return auth.verifyUserAttributeSubmit(user, attributeName, confirmationCode);
},

Expand Down
8 changes: 4 additions & 4 deletions tests/dummy/app/components/attribute-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export default Component.extend({
actions: {
save(e) {
e.preventDefault();
const { name, value } = this.get('model');
const { name, value } = this.model;

this.get('cognito.user').updateAttributes({ [name]: value }).then(() => {
this.cognito.user.updateAttributes({ [name]: value }).then(() => {
this.onSave();
}).catch((err) => {
this.set('errorMessage', err.message);
Expand All @@ -21,9 +21,9 @@ export default Component.extend({

deleteAttr(e) {
e.preventDefault();
const { name } = this.get('model');
const { name } = this.model;

this.get('cognito.user').deleteAttributes([ name ]).then(() => {
this.cognito.user.deleteAttributes([ name ]).then(() => {
this.onDelete();
}).catch((err) => {
this.set('errorMessage', err.message);
Expand Down
Loading

0 comments on commit b2b96c8

Please sign in to comment.