-
Notifications
You must be signed in to change notification settings - Fork 1
Create custom authentication components
The ember-cli-gatekeeper addon provides a default component for to sign in
a user into their account. This default sign in component uses Material Design.
If you have an application that does not use Material Design, then the default
sign in component cannot be used as is in your application. Instead, you must
customize the sign in component for your application.
It is better to customize the sign in component and not implement one from scratch because you lose out on reusing the implementation from the default sign in component.
Let's assume our project is named Solid. First, create a new sign in component.
ember g component solid-sign-in
This will create a new component (including handlebars template) with code similar to the following:
// app/components/solid-sign-in.js
import Component from "@ember/component";
export default Component.extend ({
});Next, replace extending the Component class with the SignInComponent
from ember-cli-gatekeeper.
// app/components/solid-sign-in.js
import SignInComponent from "ember-cli-gatekeeper/components/gatekeeper-sign-in";
export default SignInComponent.extend ({
classNames: ['solid-sign-in']
});Notice that we added add a class name to the classNames attribute. By default, the
SignInComponent includes the gatekeeper-sign-in class name. We want our component
to include the its own class name as well, which we can reference when styling this
component. If we did not provide the custom class name, then we can just rely on the
gatekeeper-sign-in class name for styling purposes.
The last step is to implement the sign in component's handlebars template. When
implementing the template, we want to make sure we bind attribute variables and actions
in our template the following attributes and actions the SignInComponent from
ember-cli-gatekeeper expects.
| Identifier | Type | Description |
|---|---|---|
username |
Attribute | The variable bound to the username input |
password |
Attribute | The variable bound to the password input |
signIn |
Action | The action to submit username and password for sign in |
Here is an example template for the solid-sign-in component that uses native
input controls.
{{#mdc-form submit=(action "signIn") as |form|}}
{{input type="text" value=username required=true}}
{{input type="password" value=password required=true}}
<button type="submit" disabled={{form.isInvalid}}>Sign In</button>
{{/mdc-form}}As shown in the example above, we have two native input controls. One control is
bound to the username variable and the other is bound to the password variable.
We then use the {{mdc-form}} component to bind the submit property to the
signIn. This means that whenever the user clicks a button type="submit", then
it will fire the signIn action. You should also notice that we are enabling/disabling
the sign in button based on the form's validity.
To use our custom sign in component, we just replace the gatekeeper-sign-in component
with the solid-sign-in component in the sign in route template.
{{solid-sign-in complete=(action "complete")}}Now, just style the solid-sign-in component as desired in CSS/SASS, and we have our
custom sign in component for the application.