A Demo Angular2 app showcase multiple client-side technologies.
GIT repository: https://github.com/frank-meng/quicktrade
The app’s skeleton is based on the book Angular Development With Typescript 2nd Edition. I want the app to be reactive, using technology such as RxJS6, NGRX, Forms API , OAuth2, JWT, Reactive UI ,Flex Layout, Material UI.
My NodeJS version is 8. After the project is created, I add style and themes following the book’s instructions. Then I make changes in Angular.json to use the style files.
Commands:
ng new quicktrade --prefix qt --routing --style=scss
npm i --save @angular/cdk@8.2.3 @angular/material@8.2.3
npm i @angular/flex-layout@8.0.0-beta.27
The project folder structure is :
--src
------styles
------environments
----- app
------components
------shared
------services
-------models
-------store
At beginning, the app has two pages: Home (showing accounts) and Order (include quote). There are many back and forth decisions on where to put the User info and navigation info:
So finally, I decided to put nav and menu in the App component, but user info in the Home component. Although I include Material and flex libs, I do not use them much. The UI design is put off for the next phase.
Commands:
ng g c home
ng g c accountgrid
...
ng g s service/user
ng g s service/accounts
ng g s service/trade
The real backend is a SpringBoot REST server. Angular has a nice HTTPClient tool to connect to rest servers. But I don’t want to spend time on this at the beginning. So I did this in three steps:
Server endpoint URLs are in environment.
Now the whole solution is on AWS. S3 service holds the UI pice. The UI client can talk to a public KeyCloak server (running in an AWS EC2 container), and application server ( another docker container in AWS).
At begining, I created Login component, SignUp component, authGuard, and authentication service. This is to simulate the self managed authentication/authorization style. But soon I decided to dedicate the CIAM piece to KeyCloak server, and leverage the OpenID protocol for authentication.
Now, the app doesn’t manage login, signUp any more. KeyCloak can even do MFA during signin.
I installed KeyCloak standalone verson on my local, and created a simple client using Angular App to test it. Everything works fine. Meanwhile, I implemented the JWT verification on sprintboot server side. Some of the considerations:
Store tokens. I tried to store tokens on Cookie and LocalStorage.
Inject token. The JWT token injection uses HttpInterceptor. The logic is configurable based on the URL pattern. I can choose not inject JWT for some URL requests.
All JWT token handling code is in UserService.
KeyClaok offers a container based server. I have deployed it on AWS and everything works well. The KeyCloak endpoint URL is defined in environment.
NGRX is very fast and clean. All data objects have been moved to ngrx store, including tokens, User, Accounts, Quote, Order.
npm i @ngrx/store@8.6.0
npm i @ngrx/effects@8.6.0
npm i @ngrx/store-devtools@8.6.0
I had a hard time to make the production build working with NGRX. I have multiple states object and everything works well in dev mode. But in production build, I can only register state oject, otherwise the whole thing falls apart. It is very frustrating that dev and production have different behavior. I treid to make each object seperate module and use lazy loading to laod them one by one. But this still does not work in production build. Eventually, I combined all state objects into one, and slice them later in my selector and reducer.
Need to spend some time later to make the lazy loaing working in prod build.