{"id":15015952,"url":"https://github.com/adopted-ember-addons/ember-cognito","last_synced_at":"2025-04-23T15:51:38.785Z","repository":{"id":37443997,"uuid":"88783508","full_name":"adopted-ember-addons/ember-cognito","owner":"adopted-ember-addons","description":"AWS Amplify/Cognito and ember-simple-auth integration","archived":false,"fork":false,"pushed_at":"2024-07-30T15:05:41.000Z","size":2544,"stargazers_count":32,"open_issues_count":22,"forks_count":25,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-04-11T14:19:33.855Z","etag":null,"topics":["addon","aws","aws-cognito","ember","ember-cognito","ember-simple-auth"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adopted-ember-addons.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-19T19:35:57.000Z","updated_at":"2024-05-30T02:40:20.000Z","dependencies_parsed_at":"2024-01-08T02:54:26.571Z","dependency_job_id":"9db22470-e134-4be1-80f9-65a3d0d05613","html_url":"https://github.com/adopted-ember-addons/ember-cognito","commit_stats":{"total_commits":308,"total_committers":12,"mean_commits":"25.666666666666668","dds":0.5194805194805194,"last_synced_commit":"9c46282c2ba828baa74fa09f77bb394d42efaadb"},"previous_names":["paulcwatts/ember-cognito"],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adopted-ember-addons%2Fember-cognito","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adopted-ember-addons%2Fember-cognito/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adopted-ember-addons%2Fember-cognito/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adopted-ember-addons%2Fember-cognito/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adopted-ember-addons","download_url":"https://codeload.github.com/adopted-ember-addons/ember-cognito/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250466815,"owners_count":21435440,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["addon","aws","aws-cognito","ember","ember-cognito","ember-simple-auth"],"created_at":"2024-09-24T19:48:12.143Z","updated_at":"2025-04-23T15:51:38.765Z","avatar_url":"https://github.com/adopted-ember-addons.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ember-cognito\n## An authenticator and library for using ember-simple-auth and AWS Amplify/Cognito\n\n[![Build Status](https://travis-ci.org/paulcwatts/ember-cognito.svg?branch=master)](https://travis-ci.org/paulcwatts/ember-cognito)\n[![Ember Observer Score](https://emberobserver.com/badges/ember-cognito.svg)](https://emberobserver.com/addons/ember-cognito)\n[![npm version](https://badge.fury.io/js/ember-cognito.svg)](https://badge.fury.io/js/ember-cognito)\n\n[*Maintainer Needed!*](https://github.com/paulcwatts/ember-cognito/issues/162)\n\nEmber Cognito is an Ember Addon that integrates \n[ember-simple-auth](https://github.com/simplabs/ember-simple-auth/) \nwith [AWS Amplify](https://aws-amplify.github.io/docs/) and \n[AWS Cognito](https://aws.amazon.com/cognito/) User Pools. \n\nember-simple-auth is a lightweight library for implementing authentication/authorization \nin Ember.js. AWS Amplify is a client framework, developed by Amazon, which uses Amazon \nCognito as a managed authentication system for mobile and web apps on Amazon Web Services.\n\nember-cognito implements an ember-simple-auth custom authenticator that can be used \nin an AWS Amplify application, or any Ember application, to authenticate with \na Cognito User Pool. \n\n## Installation\n\nInstall as a standard Ember Addon:\n\n* `ember install ember-cognito`\n\n## Configure\n\nIn your `config/environment.js`  file:\n\n```js\nvar ENV = {\n  // ..\n  cognito: {\n    poolId: '\u003cyour Cognito User Pool ID\u003e',\n    clientId: '\u003cyour Cognito App Client ID\u003e',\n  }\n};\n```\n\nNote that the Cognito JavaScript SDK requires that your App be created *without* a \nClient Secret.\n\n## Optional Configuration\n\nYou can specify these optional configuration options to the above configuration hash:\n\n* `autoRefreshSession`. Cognito access tokens are only valid for an hour. By default, \nthis addon will refresh expired sessions on application startup. \nSetting `autoRefreshSession` to `true` will enable a timer that will automatically \nrefresh the Cognito session when it expires.\n\n* `authenticationFlowType`. The authentication flow type that should be used. \nDefault value: `USER_SRP_AUTH`\nAllowed values: `USER_SRP_AUTH | USER_PASSWORD_AUTH`\nMore details - [Auth Flow](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html?shortFooter=true#API_InitiateAuth_RequestSyntax)\n\n## Usage\n\n### Cognito Authenticator\n\nThe Cognito Authenticator authenticates an ember-simple-auth session with \nAmplify/Cognito:\n\n```js\nimport Component from '@ember/component';\nimport { action } from '@ember/object';\nimport { inject as service } from '@ember/service';\n\nexport default class LoginComponent extends Component {\n  @service session;\n  \n  @action\n  async authenticate() {\n    const { username, password } = this;\n    const credentials = { username, password };\n    try {\n      await this.session.authenticate('authenticator:cognito', credentials);\n    } catch (error) {\n      set(this, 'errorMessage', error.message || error);\n    }\n  }\n}\n```\n\n#### Integrating with an Ember Data Adapter\n\nThe Cognito Authenticator will put the Cognito ID token in the `access_token` property in the session's\n`authenticated` data. This means integrating with ember-simple-auth's \n[Ember Data Adapter Mixin](https://github.com/simplabs/ember-simple-auth/blob/master/addon/mixins/data-adapter-mixin.js)\nrequires no special configuration.\n\n\n### Cognito Service\n\nThe addon provides a `cognito` service that provides some helpers to access the \nAmplify auth object. The service provides access to the following Amplify/auth methods:\n\n* `signUp(username, password, attributes, validationData)`\n* `confirmSignUp(username, code, options)`\n* `resendSignUp(username)`\n* `forgotPassword(username)`\n* `forgotPasswordSubmit(username, code, newPassword)`\n\nIt also provides a helper to quickly access the current user's Cognito ID token:\n\n* `getIdToken()`\n\n#### Cognito User\n\nThe Cognito service allows you to access the currently authenticated `CognitoUser` \nobject, along with the following helper methods:\n\n* `changePassword(oldPassword, newPassword)`\n* `deleteAttributes(attributeList)`\n* `deleteUser()`\n* `getSession()`\n* `getUserAttributes()`\n* `getUserAttributesHash()`\n* `signOut()`\n* `updateAttributes()`\n* `verifyAttribute()`\n* `getGroups()`\n\nIf you use a `current-user` service using the \n[ember-simple-auth guide](https://github.com/simplabs/ember-simple-auth/blob/master/guides/managing-current-user.md), \nyou can use the Cognito User to fetch user attributes:\n\n```js\nimport Service from '@ember/service';\nimport { inject as service } from '@ember/service';\nimport { readOnly } from '@ember/object/computed';\n\nexport default class CurrentUserService extends Service {\n  @service session;\n  @service cognito;\n  @readOnly('cognito.user') cognitoUser;\n  @readOnly('cognitoUser.username') username;\n  \n  async load() {\n    if (this.session.isAuthenticated) {\n      const userAttributes = await this.cognitoUser.getUserAttributes();\n      userAttributes.forEach((attr) =\u003e {\n        set(this, attr.getName(), attr.getValue());\n      });\n    }\n  }\n}\n```\n\nYou can see examples of usages of these API methods in the \n[full-featured dummy app](https://github.com/paulcwatts/ember-cognito/blob/master/tests/dummy/app).\n\n\n#### Advanced Configuration\n\nIf you don't want to specify the Pool ID and Client ID in the Ember environment, you \ncan override the CognitoService in your own app and provide the configuration there. \n\n```js\n// app/services/cognito.js\nimport BaseCognitoService from 'ember-cognito/services/cognito';\n\nexport default class CognitoService extends BaseCognitoService {\n  poolId = '\u003cmy pool ID\u003e';\n  clientId = '\u003cmy client ID\u003e;\n}\n```\n\nIn this case, you can have the properties be computed or retrieved through some dynamic \nmechanism.\n\n## Testing\n\nember-cognito provides some helpers and utilities that make it easier to work with \nCognito in tests.\n\n### mockCognitoUser\n\nIn acceptance tests, you can use ember-simple-auth's `authenticateSession` to create a \nuser, but you may also need to mock user attributes on the Cognito service. You can do \nthis using `mockCognitoUser`:\n\n```js\nimport { authenticateSession } from 'ember-simple-auth/test-support';\nimport { mockCognitoUser } from 'ember-cognito/test-support';\n\nmodule('Acceptance | authenticated route', function(hooks) {\n  test('authenticated route', async function(assert) {\n    await authenticateSession();\n    await mockCognitoUser({\n      username: 'testuser'\n      // userAttributes...\n    });\n    const authenticator = this.owner.lookup('authenticator:cognito');\n    // Rest of the test\n  });\n});\n```\n\n### mockAuth\n\nIn some cases, you may want to mock the Amplify auth object to test authentication\nscenarios. You can use the `mockAuth` helper to add your own mock class to\nstub certain Amplify functions in tests:\n\n```js\nimport { mockAuth, MockAuth } from 'ember-cognito/test-support';\n\nmodule('Acceptance | login', function(hooks) {\n  setupApplicationTest(hooks);\n  \n  test('login failure', async function(assert) {\n    await mockAuth(MockAuth.extend({\n      signIn() {\n        return reject({ message: 'Username or password incorrect.' });\n      }\n    }));\n     \n    // Attempt to login, \n    await visit('/login');\n    await fillIn('#username', 'testuser');\n    await fillIn('#password', 'password');\n    await click('[type=submit]');\n  \n    assert.dom('[data-test-error]').hasText('Username or password incorrect.');\n  });\n});\n```\n\n## Dummy App\n\nThe [dummy app](https://github.com/paulcwatts/ember-cognito/blob/master/tests/dummy/app)\nincludes many use cases for you to see this addon in action, including new user sign up,\nlogin, logout, and updating/verifying user attributes.\n\n## Support\n\n* Ember versions 3.28+\n* AWS Amplify 1.x\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadopted-ember-addons%2Fember-cognito","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadopted-ember-addons%2Fember-cognito","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadopted-ember-addons%2Fember-cognito/lists"}