diff --git a/addon/authenticators/cognito.js b/addon/authenticators/cognito.js index b2b37f2..89063e3 100644 --- a/addon/authenticators/cognito.js +++ b/addon/authenticators/cognito.js @@ -1,24 +1,22 @@ import { readOnly } from '@ember/object/computed'; import { inject as service } from '@ember/service'; import Base from 'ember-simple-auth/authenticators/base'; -import { reject } from 'rsvp'; -export default Base.extend({ - cognito: service(), - auth: readOnly('cognito.auth'), - poolId: readOnly('cognito.poolId'), - clientId: readOnly('cognito.clientId'), - authenticationFlowType: readOnly('cognito.authenticationFlowType'), +export default class CognitoAuthenticator extends Base { + @service cognito; + @readOnly('cognito.auth') auth; + @readOnly('cognito.poolId') poolId; + @readOnly('cognito.clientId') clientId; + @readOnly('cognito.authenticationFlowType') authenticationFlowType; - restore({ poolId, clientId }) { + async restore({ poolId, clientId }) { this.cognito.configure({ userPoolId: poolId, userPoolWebClientId: clientId }); - return this.auth.currentAuthenticatedUser().then((user) => { - return this._resolveAuth(user); - }); - }, + const user = await this.auth.currentAuthenticatedUser(); + return this._resolveAuth(user); + } _makeAuthData(user, session) { return { @@ -26,18 +24,16 @@ export default Base.extend({ clientId: user.pool.getClientId(), access_token: session.getIdToken().getJwtToken() }; - }, + } - _resolveAuth(user) { + async _resolveAuth(user) { const { cognito } = this; cognito._setUser(user); // Now pull out the (promisified) user - return cognito.user.getSession().then((session) => { - /* eslint-disable camelcase */ - cognito.startRefreshTask(session); - return this._makeAuthData(user, session); - }); - }, + const session = await cognito.user.getSession(); + cognito.startRefreshTask(session); + return this._makeAuthData(user, session); + } _handleSignIn(user) { // console.log(user); @@ -51,31 +47,26 @@ export default Base.extend({ } else { return this._resolveAuth(user); } - }, + } - _handleNewPasswordRequired({ password, state: { user } }) { - return this.auth.completeNewPassword(user, password).then((user) => { - return this._handleSignIn(user); - }); - }, + async _handleNewPasswordRequired({ password, state: { user } }) { + const user2 = await this.auth.completeNewPassword(user, password); + return this._handleSignIn(user2); + } - _handleRefresh() { + async _handleRefresh() { 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 auth.currentAuthenticatedUser().then((awsUser) => { - return this._makeAuthData(awsUser, session); - }); - } else { - return reject('session is invalid'); - } - }); - }, + const session = await user.getSession(); + if (session.isValid()) { + cognito.startRefreshTask(session); + const awsUser = await auth.currentAuthenticatedUser(); + return this._makeAuthData(awsUser, session); + } else { + throw new Error('session is invalid'); + } + } _handleState(name, params) { if (name === 'refresh') { @@ -85,9 +76,9 @@ export default Base.extend({ } else { throw new Error('invalid state'); } - }, + } - authenticate(params) { + async authenticate(params) { const { username, password, state } = params; if (state) { return this._handleState(state.name, params); @@ -96,15 +87,13 @@ export default Base.extend({ const { auth, authenticationFlowType, cognito } = this; cognito.configure({ authenticationFlowType }); - return auth.signIn(username, password).then((user) => { - return this._handleSignIn(user); - }); - }, + const user = await auth.signIn(username, password); + return this._handleSignIn(user); + } - invalidate(data) { - return this.cognito.user.signOut().then(() => { - this.set('cognito.user', undefined); - return data; - }); + async invalidate(data) { + await this.cognito.user.signOut(); + this.set('cognito.user', undefined); + return data; } -}); +} diff --git a/addon/services/cognito.js b/addon/services/cognito.js index c4deea6..7b755fd 100644 --- a/addon/services/cognito.js +++ b/addon/services/cognito.js @@ -10,14 +10,14 @@ import { reject } from 'rsvp'; * This is a container for easily accessing the logged-in CognitoUser object, * as well as creating others using signUp(). */ -export default Service.extend({ - session: service(), - auth: Auth, +export default class CognitoService extends Service { + @service session; + auth = Auth; willDestroy() { - this._super(...arguments); + super.willDestroy(...arguments); this.stopRefreshTask(); - }, + } /** * Configures the Amplify library with the pool & client IDs, and any additional @@ -32,7 +32,7 @@ export default Service.extend({ }, awsconfig); this.auth.configure(params); - }, + } /** * Method for signing up a user. @@ -42,20 +42,18 @@ export default Service.extend({ * @param attributes New user attributes. * @param validationData Application metadata. */ - signUp(username, password, attributes, validationData) { + async signUp(username, password, attributes, validationData) { this.configure(); - - return this.auth.signUp({ + const result = await this.auth.signUp({ username, password, attributes: normalizeAttributes(attributes), validationData - }).then((result) => { - // Replace the user with a wrapped user. - result.user = this._setUser(result.user); - return result; }); - }, + // Replace the user with a wrapped user. + result.user = this._setUser(result.user); + return result; + } /** * Confirm signup for user. @@ -66,7 +64,7 @@ export default Service.extend({ confirmSignUp(username, code, options) { this.configure(); return this.auth.confirmSignUp(username, code, options); - }, + } /** * Resends the sign up code. @@ -76,7 +74,7 @@ export default Service.extend({ resendSignUp(username) { this.configure(); return this.auth.resendSignUp(username); - }, + } /** * Sends a user a code to reset their password. @@ -86,7 +84,7 @@ export default Service.extend({ forgotPassword(username) { this.configure(); return this.auth.forgotPassword(username); - }, + } /** * Submits a new password. @@ -98,7 +96,7 @@ export default Service.extend({ forgotPasswordSubmit(username, code, newPassword) { this.configure(); return this.auth.forgotPasswordSubmit(username, code, newPassword); - }, + } /** * Enable the token refresh timer. @@ -114,7 +112,7 @@ export default Service.extend({ const duration = (exp - adjusted) * 1000 + 100; this.set('_taskDuration', duration); this.set('task', later(this, 'refreshSession', duration)); - }, + } /** * Disable the token refresh timer. @@ -123,28 +121,27 @@ export default Service.extend({ cancel(this.task); this.set('task', undefined); this.set('_taskDuration', undefined); - }, + } refreshSession() { let user = this.user; if (user) { return this.session.authenticate('authenticator:cognito', { state: { name: 'refresh' } }); } - }, + } /** * A helper that resolves to the logged in user's id token. */ - getIdToken() { + async getIdToken() { const user = this.user; if (user) { - return user.getSession().then((session) => { - return session.getIdToken().getJwtToken(); - }) + const session = await user.getSession(); + return session.getIdToken().getJwtToken(); } else { return reject('user not authenticated'); } - }, + } _setUser(awsUser) { // Creates and sets the Cognito user. @@ -152,4 +149,4 @@ export default Service.extend({ this.set('user', user); return user; } -}); +} diff --git a/addon/utils/cognito-user.js b/addon/utils/cognito-user.js index 99f09b5..5c1a1d5 100644 --- a/addon/utils/cognito-user.js +++ b/addon/utils/cognito-user.js @@ -7,11 +7,12 @@ import { normalizeAttributes } from "./utils"; // // Wraps an AWS CognitoUser. // -export default EmberObject.extend({ - username: computed('user', function() { +export default class CognitoUser extends EmberObject { + @computed('user') + get username() { return this.user.getUsername(); - }), - attributes: readOnly("user.attributes"), + } + @readOnly('user.attributes') attributes; _callback(method, ...args) { return new Promise((resolve, reject) => { @@ -27,12 +28,12 @@ export default EmberObject.extend({ reject(error); } }, `cognito-user#${method}`); - }, + } changePassword(oldPassword, newPassword) { const { auth, user } = this; return auth.changePassword(user, oldPassword, newPassword); - }, + } confirmPassword(verificationCode, newPassword) { deprecate( @@ -43,7 +44,7 @@ export default EmberObject.extend({ const { auth, username } = this; return auth.forgotPasswordSubmit(username, verificationCode, newPassword); - }, + } confirmRegistration(confirmationCode, forceAliasCreation) { deprecate( @@ -55,15 +56,15 @@ export default EmberObject.extend({ const { auth, username } = this; const options = forceAliasCreation ? { forceAliasCreation : true } : undefined; return auth.confirmSignUp(username, confirmationCode, options); - }, + } deleteAttributes(attributeList) { return this._callback('deleteAttributes', attributeList); - }, + } deleteUser() { return this._callback('deleteUser'); - }, + } forgotPassword() { deprecate( @@ -74,28 +75,27 @@ export default EmberObject.extend({ const { auth, username } = this; return auth.forgotPassword(username); - }, + } getAttributeVerificationCode(attributeName) { const { auth, user } = this; return auth.verifyUserAttribute(user, attributeName); - }, + } getSession() { return this.auth.currentSession(); - }, + } getUserAttributes() { const { auth, user } = this; return auth.userAttributes(user); - }, + } - getUserAttributesHash() { + async getUserAttributesHash() { const { auth, user } = this; - return auth.userAttributes(user).then((result) => { - return normalizeAttributes(result, false); - }); - }, + const result = await auth.userAttributes(user); + return normalizeAttributes(result, false); + } resendConfirmationCode() { deprecate( @@ -106,30 +106,29 @@ export default EmberObject.extend({ const { auth, username } = this; return auth.resendSignUp(username); - }, + } signOut() { return this.auth.signOut(); - }, + } updateAttributes(attributes) { const { auth, user } = this; const normalized = normalizeAttributes(attributes); return auth.updateUserAttributes(user, normalized); - }, + } verifyAttribute(attributeName, confirmationCode) { const { auth, user } = this; return auth.verifyUserAttributeSubmit(user, attributeName, confirmationCode); - }, + } // Non-AWS method - getGroups() { - return this.getSession().then((session) => { - let payload = session.getIdToken().payload || {}; - return payload['cognito:groups'] || []; - }); - }, + async getGroups() { + const session = await this.getSession(); + let payload = session.getIdToken().payload || {}; + return payload['cognito:groups'] || []; + } getStorageData() { deprecate( @@ -139,4 +138,4 @@ export default EmberObject.extend({ ); return {}; } -}); +} diff --git a/tests/dummy/app/routes/attribute.js b/tests/dummy/app/routes/attribute.js index 537d406..251cee6 100644 --- a/tests/dummy/app/routes/attribute.js +++ b/tests/dummy/app/routes/attribute.js @@ -1,16 +1,15 @@ import Route from '@ember/routing/route'; import { inject as service } from '@ember/service'; -export default Route.extend({ - cognito: service(), +export default class AttributeRoute extends Route { + @service cognito; - model({ name }) { + async model({ name }) { if (name) { - return this.cognito.user.getUserAttributesHash().then((attrs) => { - const value = attrs[name]; - return { name, value }; - }); + const attrs = await this.cognito.user.getUserAttributesHash(); + const value = attrs[name]; + return { name, value }; } return { isNew: true }; } -}); +} diff --git a/tests/dummy/app/routes/index.js b/tests/dummy/app/routes/index.js index 9f1870c..bb26ed4 100644 --- a/tests/dummy/app/routes/index.js +++ b/tests/dummy/app/routes/index.js @@ -2,20 +2,19 @@ import { inject as service } from '@ember/service'; import Route from '@ember/routing/route'; import { readOnly } from '@ember/object/computed'; -export default Route.extend({ - cognito: service(), - cognitoUser: readOnly('cognito.user'), - session: service(), +export default class IndexRoute extends Route { + @service cognito; + @service session; + @readOnly('cognito.user') cognitoUser; - model() { + async model() { if (this.session.isAuthenticated) { - return this.cognitoUser.getUserAttributes().then((cognitoAttrs) => { - let attributes = []; - cognitoAttrs.forEach((attr) => { - attributes.push({ name: attr.getName(), value: attr.getValue() }); - }); - return { attributes }; + const cognitoAttrs = await this.cognitoUser.getUserAttributes(); + let attributes = []; + cognitoAttrs.forEach((attr) => { + attributes.push({ name: attr.getName(), value: attr.getValue() }); }); + return { attributes }; } } -}); +} diff --git a/tests/dummy/app/services/current-user.js b/tests/dummy/app/services/current-user.js index dc4f927..662434e 100644 --- a/tests/dummy/app/services/current-user.js +++ b/tests/dummy/app/services/current-user.js @@ -1,22 +1,18 @@ import { readOnly } from '@ember/object/computed'; -import { resolve } from 'rsvp'; import Service, { inject as service } from '@ember/service'; -export default Service.extend({ - session: service(), - cognito: service(), - cognitoUser: readOnly('cognito.user'), - username: readOnly('cognitoUser.username'), +export default class CurrentUserService extends Service { + @service session; + @service cognito; + @readOnly('cognito.user') cognitoUser; + @readOnly('cognitoUser.username') username; - load() { + async load() { if (this.session.isAuthenticated) { - return this.cognitoUser.getUserAttributes().then((userAttributes) => { - userAttributes.forEach((attr) => { - this.set(attr.getName(), attr.getValue()); - }); + const userAttributes = await this.cognitoUser.getUserAttributes(); + userAttributes.forEach((attr) => { + this.set(attr.getName(), attr.getValue()); }); - } else { - return resolve(); } } -}); +}