AuthConfig
interface which will contain various configuration values. Place this interface in a file called auth0-variables.ts
.
WebAuth
object from auth0.js can be created in the service.
authorize
from auth0.js which initiates parseHash
method from auth0.jshandleAuthentication
method in the AuthService
is responsible for processing the hash.
Call handleAuthentication
in your app’s root component so that the authentication hash fragment can be processed when the app first loads after the user is redirected back to it.
CallbackComponent
and populate it with a loading indicator.
assets
directory. See the downloadable sample for a demonstration.
After authentication, users will be taken to the /callback
route for a brief time where they will be shown a loading indicator. During this time, their client-side session will be set, after which they will be redirected to the /home
route.
AuthService
class. Add a getProfile
function which will extract the user’s Access Token from local storage, and then pass that call the userInfo
function to retrieve the user’s information.
scope
returned in the authResult
is not empty, it means that a user was issued a different set of scopes than what was initially requested, and we should therefore use authResult.scope
to determine the scopes granted to the user.
If the scope
returned in authResult
is issued is empty, it means the user was granted all the scopes that were requested, and we can therefore use the requested scopes to determine the scopes granted to the user.
Here is the code we wrote earlier for the setSession
function that does that check:
AuthService
class which we can call to determine if a user was granted a specific scope:
approve:timesheets
scope. Note in the code below that we added a call to the userHasScopes
function to determine whether that link should be displayed or not.
ScopeGuardService
service class:
ScopeGuardService
in the definition for the approval
route below:
AuthHttp
class which is a wrapper over Angular’s Http
class.
Install angular2-jwt
:
angular2-jwt
and add it to the providers
array in your application’s @NgModule
. The factory function should have a tokenGetter
function which fetches the access_token
from local storage.
angular2-jwt
is configured, you can use the AuthHttp
class to make secure calls to your API from anywhere in the application. To do so, inject AuthHttp
in any component or service where it is needed and use it just as you would use Angular’s regular Http
class.
AuthService
which calls the checkSession
method from auth0.js. If the renewal is successful, use the existing setSession
method to set the new tokens in local storage.
AuthService
class, add a method called scheduleRenewal
to set up a time at which authentication should be silently renewed. In the sample below this is set up to happen 30 seconds before the actual token expires. Also add a method called unscheduleRenewal
which will unsubscribe from the Observable.
scheduleRenewal
inside your AppComponent
which will happen when the page is loaded. This will occur after every authentication flow, either when the user explicitly logs in, or when the silent authentication happens.