Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some docs for allowing sign up #106

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,93 @@ export default Service.extend({
See [the dummy app](https://github.com/paulcwatts/ember-cognito/blob/master/tests/dummy/app/services/current-user.js)
for an example of this in action.

### Allowing a user to sign up

You can allow a user to create and confirm a new account with a
controller like this:

```js
import { equal } from '@ember/object/computed';
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';

export default Controller.extend({
session: service(),
cognito: service(),

actions: {
signup(e) {
e.preventDefault();
let params = this.getProperties('username', 'password');
this.get('cognito').signUp( params.username, params.password, [], null).then((newUser) => {
this.set('newUser', newUser);
}).catch((error) => {
console.log('caught an error', error);
});
},
signupConfirm(e) {
e.preventDefault();
let { confirmationCode, newUser, username, password, cognito } = this.getProperties('confirmationCode', 'newUser', 'username', 'password', 'cognito');
let _controller = this;
let cognitoUser = newUser.user;
cognitoUser.confirmRegistration(confirmationCode).then(() => {
_controller.authenticate({ username, password });
}).catch((error) => {
console.log('we caught an error', error);
});
}
},

authenticate(params) {
this.get('session').authenticate('authenticator:cognito', params).then(() => {
this.transitionToRoute('/');
}).catch((err) => {
if (err.state && err.state.name === 'newPasswordRequired') {
this.set('errorMessage', '');
this.set('state', err.state);
} else {
this.set('errorMessage', err.message || err);
}
});
}
});
```

And a template like this:

```hbs
{{#if newUser}}
<h1>Your confirmation code</h1>
<form onsubmit={{action 'signupConfirm'}}>
{{#if errorMessage}}
<div>{{errorMessage}}</div>
{{/if}}
<div>
<label for="confirmationCode">Confirmation Code</label>
{{input value=confirmationCode id="confirmationCode" placeholder="Check your email" autofocus=true required=true}}
</div>
<button type="submit">Sign Up</button>
</form>
{{else}}
<h1>Sign Up Today!</h1>
<form onsubmit={{action 'signup'}}>
{{#if errorMessage}}
<div>{{errorMessage}}</div>
{{/if}}
<div>
<label for="username">Username</label>
{{input value=username id="username" placeholder="Username" autofocus=true required=true}}
</div>
<div>
<label for="password">Password</label>
{{input value=password type="password" id="password" placeholder="Password" required=true}}
</div>
<button type="submit">Sign Up</button>
</form>
{{/if}}
```


#### Advanced Configuration

If you don't want to specify the Pool ID and Client ID in the Ember environment, you can override the CognitoService
Expand Down