Providing a service using a factory
You can dynamically choose providers during load time, so instead of changing code to switch between authentication methods, you can parametrize environment variables so that different kinds of builds can have different authentication methods. This is especially useful when writing automated UI tests against your application, where real authentication can be difficult, if not impossible, to deal with.
First, we will create an enum in environment.ts to help define our options, and then we will use that enum to choose an auth provider during our application’s bootstrap process.
Let’s get started:
- Create a new
enumcalledAuthMode:src/app/auth/auth.enum.ts export enum AuthMode { InMemory = 'In Memory', CustomServer = 'Custom Server', CustomGraphQL = 'Custom GraphQL', Firebase = 'Firebase', } - Add an
authModeproperty inenvironment.ts:src...