Web JS with Angular
Prerequisites
Required client settings
As this library is designed for SPAs, make sure you have selected
Application Type in your client settings as Single Page Application.
For a fully functional integration ensure that Callback redirect URIs and Signout redirect URIs are configured.
Install web-js package
Custom webpack config
As web-js requires Node.js Crypto and Stream libraries, you will need to use custom webpack builder and config for your Angular project.
- First install Angular custom webpack builder and dotenv for webpack
- Create custom webpack config in project root
- Update your
angular.json. Basically updatebuilderto use@angular-builders/custom-webpack:browserand inoptionsaddcustomWebpackConfigwithpathtoextra-webpack.config.js
Angular client configuration
- Grab the client key. Now, in your Angular
appaddauth.service.tsfile and create web-jsAuthobject which looks like below.
- In
appdirectory addtyping.d.ts
- Next add a
.envfile in your Angular project root, and populate following values,
Here is an example version of .env
Using web-js functions
In your component files access various web-js JavaScript functions using
AuthService object and use constructor to create class variable $auth.
- Using
AuthServicein your components
- To login with redirect,
- For logout with redirect,
- To check if user is authenticated,
Add view for redirect URI
- Using Angular CLI Create
callbackcomponent, add authorization response processing logic, and register as route/callback.
In component file add,
- Register redirect URI in your client's settings and also add in
.env. URI should behttps://{host}/callbackorhttp://{host}/callbackwhere host can be localhost i.e.localhost:8080or domain hosting your Angular app i.e.app.my-domain.com.
Add view to initiate login
- Using Angular CLI Create
logincomponent, add logic to initiate login, and register as route/login.
In component file add,
- Your can use login endpoint
https://{host}/loginorhttp://{host}/loginas post logout uri in your client's settings and also add in.envwhere where host can be localhost i.e.localhost:8080or domain hosting your Angular app i.e.app.my-domain.com.
Add view to initiate logout
- Using Angular CLI Create
logoutcomponent, add logic to initiate logout, and register as route/logout.
In component file add,
Add Angular router guard
Using web-js, you can guard your Angular router for un-authenticated users and redirect them to login. This authentication guard will utilise canActivate attribute of defined routes.
Update Angular routing module
Add Angular routes
Angular Sample
To learn more download our Angular sample from our Github repository. In less than 10 minutes you can deploy Angular sample to Netlify.