Tutorial: How to Integrate Passkeys into Angular
Implementing Passkey Authentication in Angular with TypeScript
In this guide, we’ll walk through the process of integrating passkey authentication into an Angular application using TypeScript. Passkeys provide a secure and scalable way to manage user authentication, removing the need for traditional passwords.
View full tutorial in our original blog post here
Prerequisites
Before starting, ensure you’re familiar with Angular, HTML, CSS, and TypeScript. Additionally, make sure you have Node.js and NPM installed on your machine. Installing the Angular CLI is recommended for this tutorial:
npm install -g @angular/cli
Setting Up the Angular Project
First, let’s create a new Angular project. In this example, we’re using Angular version 15.2.9:
ng new passkeys-demo-angular
During setup, choose the following options:
- Share pseudonymous usage data: No
- Angular routing: Yes
- Stylesheet format: CSS
- Enable SSR: No (Choose Yes if your application requires server-side rendering)
Once the setup is complete, run the application to ensure everything is functioning:
ng serve
Integrating Corbado for Passkey Authentication
1. Set Up Your Corbado Account
To start, create an account on the Corbado developer panel. This step allows you to experience passkey signup firsthand. After registering, create a project within Corbado by selecting “Corbado Complete” as your product. Specify “Web app” as the application type, and for the framework, select Angular. In your application settings, use the following details:
- Application URL: http://localhost:4200
- Relying Party ID: localhost
2. Embedding the Corbado UI Component
Next, you’ll need to install the required packages for Corbado integration. Navigate to your project’s root directory and install the necessary packages:
npm i @corbado/web-js npm i -D @corbado/types @types/react @types/ua-parser-js
Modify the app.component.ts to initialize Corbado when the application starts:
import { Component, OnInit } from '@angular/core'; import Corbado from "@corbado/web-js"; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit { title = 'passkeys-demo-angular'; isInitialized = false; constructor() { } ngOnInit(): void { this.initialize(); } async initialize() { try { await Corbado.load({ projectId: "<Your Corbado Project ID>", darkMode: 'off' }); this.isInitialized = true; } catch (error) { console.error('Initialization failed:', error); } } }
3. Creating Login and Profile Components
Generate two components: one for displaying the passkey login UI and another for showing basic user information upon successful authentication:
ng generate component login ng generate component profile
Update your app-routing.module.ts to define routes for the login and profile components:
// src/app/app-routing.module.ts import { NgModule } from '@angular/core'; import { ProfileComponent } from "./profile/profile.component"; import { RouterModule, Routes } from "@angular/router"; import { LoginComponent } from "./login/login.component"; const routes: Routes = [ { path: 'profile', component: ProfileComponent }, { path: 'login', component: LoginComponent }, { path: '', component: LoginComponent }, { path: '**', redirectTo: '/' } ] @NgModule({ imports: [ RouterModule.forRoot(routes) ], exports: [RouterModule] }) export class AppRoutingModule { }
In login.component.ts, set up the passkey authentication UI and define the behavior after a successful login:
import { Component, OnInit, ViewChild, ElementRef, AfterViewInit } from '@angular/core'; import { Router } from '@angular/router'; import Corbado from "@corbado/web-js"; @Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: ['./login.component.css'] }) export class LoginComponent implements OnInit, AfterViewInit { @ViewChild('authElement', { static: false }) authElement!: ElementRef; // Access the element constructor(private router: Router) { } ngOnInit() { if (Corbado.user) { this.router.navigate(['/profile']); } } ngAfterViewInit() { // Mount the Corbado auth UI after the view initializes Corbado.mountAuthUI(this.authElement.nativeElement, { onLoggedIn: () => this.router.navigate(['/profile']), // Use Angular's router instead of window.location }); } }
And in the login.component.html, add the following:
<div #authElement></div>
4. Setting Up the Profile Page
The profile page will display basic user information (user ID and email) and provide a logout button. If the user isn’t logged in, the page will prompt them to return to the home page:
import { Component } from '@angular/core'; import { Router } from "@angular/router"; import Corbado from "@corbado/web-js"; @Component({ selector: 'app-profile', templateUrl: './profile.component.html', styleUrls: ['./profile.component.css'] }) export class ProfileComponent { user = Corbado.user constructor(private router: Router) { } async handleLogout() { await Corbado.logout() await this.router.navigate(['/']); } }
In profile.component.html, conditionally render the user’s information based on their authentication state:
<div> <ng-container *ngIf="user; else notLoggedIn"> <div> <h1>Profile Page</h1> <div> <p> User-ID: {{user.sub}} <br /> Email: {{user.email}} </p> </div> <button (click)="handleLogout()">Logout</button> </div> </ng-container> <ng-template #notLoggedIn> <div> <p>You're not logged in.</p> <p>Please go back to <a routerLink="/">home</a> to log in.</p> </div> </ng-template> </div>
Running the Application
Once everything is set up, run the application:
ng serve
Visit http://localhost:4200 to view the login screen, and after successful authentication, you will be redirected to the profile page.
Conclusion
This tutorial demonstrated how to integrate passkey authentication into an Angular application using Corbado. With Corbado’s tools, implementing passwordless authentication is straightforward and secure. For more details on session management and other advanced features, refer to Corbado’s documentation or check the detailed blog post.
The above is the detailed content of Tutorial: How to Integrate Passkeys into Angular. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing
