Search…
Angular
Integrate Passage into your Angular application
To see a full example of using Passage in an Angular + Express.js application, please visit our sample app on GitHub.

Importing and Using the Passage-Auth Custom Element

The easiest way to add authentication to a web frontend is with a Passage Auth custom element. First you'll need to install the passage-elements package from npm:
1
npm i --save @passageidentity/passage-elements
Copied!
Then import the package in the module where you intend to use the custom element. In an Angular application this can be done at the application level, as in this example in app.module.ts or in a sub-module that contains the login/registration page.
1
import '@passageidentity/passage-elements/passage-auth'
Copied!
Importing this script will register the Passage custom element for use in your Angular components. For more information about custom elements refer to the online documentation.
It's then just a matter of embedding the passage-auth element into your component template that will handle login. This is done in this example in frontend/src/views/Home/home.component.html:
1
<div class="authContainer">
2
<passage-auth [appId]="appId"></passage-auth>
3
</div>
Copied!

Configuring Angular to Recognize Custom Elements

For Angular to recongize a non-Angular component tag as a custom element the Angular module that uses the custom element must be configured with the custom element schema. This example only contains a single app module so the custom element schema is added to app.module.ts:
1
@NgModule({
2
...
3
schemas: [CUSTOM_ELEMENTS_SCHEMA],
4
...
5
})
6
export class AppModule { }
Copied!

Getting Authentication Status and User Information

After the user has logged in with Passage, all requests to your backend need to be authenticated using the JWT provided by Passage. In this example, we set the JWT in an Authorization header to our API server.
This project uses a simple Express backend and the Passage Node.js SDK to authenticate requests and retrieve user data for your application. You can see how that runs in the /backend folder of this repository.
This example handles communication with the backend API in the ngOnInit lifecyle method of frontend/src/views/Dashboard/dashboard.component.ts.