Chapter 7
security $dialog
login-toolbar login-form
This diagram shows two user interface elements, the login-toolbar and login-
form, both of which rely on the security service. The security service uses the
$dialog service to create a modal dialog box from the login-form element.
Creating a security service
The security service is a component, which we have developed provides
the primary API for our application to manage logging in and out, and to get
information about the current user. We can inject this service into controllers and
directives. These can then attach the following properties and methods of the service
to the scope, to have access to them in templates:
- currentUser: This property contains information about the currently
authenticated user, if any. - getLoginReason(): This method returns a localized message,
explaining why we need to login, for example, The current user
does not have authorization. - showLogin(): This method causes the login form to be shown. This is called
when the user clicks on the login button on the login toolbar, and when an
HTTP 401 unauthorized error response is intercepted. - login(email, password): This method sends the specified credentials to
the server to be authenticated. This is called when the user submits the login
form. If the login is successful, the login form is closed, and any unauthorized
requests are retried (that is, the request is made again). - logout(redirectTo): This method logs out the current user and redirects.
This is called when the user clicks on the logout button on the login toolbar. - cancelLogin(redirectTo): This method gives up trying to login, any
unauthorized requests are discarded, and then the application redirects to
another $route. This is called if the user closes or cancels the login form.