Home > Web Front-end > JS Tutorial > Build an Angular App with Okta's Sign-in Widget in 15 Minutes

Build an Angular App with Okta's Sign-in Widget in 15 Minutes

William Shakespeare
Release: 2025-02-16 11:25:10
Original
693 people have browsed it

Build an Angular App with Okta’s Sign-in Widget in 15 Minutes

Key Highlights:

  • Despite a lack of backward compatibility, Angular 2 and 4 remain highly popular, securing third place among UI frameworks (after React and HTML5).
  • Okta offers a user-friendly API for streamlined user account management, seamlessly integrating with Angular applications. Its Sign-In Widget provides a customizable JavaScript solution encompassing password reset, forgotten password recovery, and robust authentication features.
  • Integrating Okta's Sign-In Widget into an Angular project is straightforward using npm and minimal code, allowing customization to match application aesthetics.
  • Thorough testing within the Angular application is facilitated by Okta, with any issues easily resolved by defining Okta as a provider. The complete application is accessible on GitHub.

While AngularJS once dominated JavaScript MVC frameworks, the announcement of non-backward compatibility for subsequent versions caused some community upheaval, benefiting competitors like React and Vue.js. However, Angular 2 and 4, leveraging TypeScript, have proven remarkably resilient, maintaining a strong position as the third most popular UI framework.

This article demonstrates a rapid integration of Okta's Sign-In Widget for user authentication in a basic Angular application. For Angular beginners, a supplementary Angular tutorial is recommended. The source code is available on GitHub.

Why Choose Okta for User Authentication?

Okta provides a comprehensive API for managing user accounts, securely storing data, and connecting to multiple applications. This simplifies account management, enhances security, and accelerates deployment. The Okta Sign-In Widget offers an easily customizable, embeddable JavaScript login solution mirroring the functionality of Okta's standard sign-in page, including password reset, forgotten password features, and strong authentication—all managed through Okta's policies, requiring no custom code. Social logins are also supported for consumer-facing applications.

Building an Angular Application

With Angular 4 and Angular CLI 1.0, creating a new application is simple:

  1. Install Angular CLI: npm install -g @angular/cli
  2. Create a new application: ng new angular-okta-example

Verify functionality with ng e2e.

Integrating Okta's Sign-In Widget

  1. Install the widget: npm install --save @okta/okta-signin-widget
  2. Add CSS to src/styles.css:
@import '~https://ok1static.oktacdn.com/assets/js/sdk/okta-signin-widget/2.1.0/css/okta-sign-in.min.css';
@import '~https://ok1static.oktacdn.com/assets/js/sdk/okta-signin-widget/2.1.0/css/okta-theme.css';
Copy after login
  1. Create src/app/shared/okta/okta.service.ts to manage the widget:
import { Injectable } from '@angular/core';
import * as OktaSignIn from '@okta/okta-signin-widget/dist/js/okta-sign-in.min.js';

@Injectable()
export class Okta {
  widget;

  constructor() {
    this.widget = new OktaSignIn({
      baseUrl: 'https://{yourOktaDomain}.com',
      clientId: '{clientId}',
      redirectUri: 'http://localhost:4200'
    });
  }

  getWidget() {
    return this.widget;
  }
}
Copy after login
  1. Add Okta as a provider in app.module.ts.

  2. Create an OpenID Connect application in your Okta account, replacing placeholders in okta.service.ts with your ClientId and Okta Domain.

  3. Implement login/logout functionality in app.component.ts and adjust app.component.html to display the widget and user information.

Testing and Customization

Run ng serve to test the application. Address potential test failures by adding Okta as a provider in src/app/app.component.spec.ts. Customize the widget's appearance by creating custom CSS. The completed application is available on GitHub.

Frequently Asked Questions (FAQs) (These are summarized for brevity; refer to the original for complete answers.)

  • Okta Integration with Angular: Install the Okta Angular SDK, configure it with your Okta domain and client ID, and use its methods for authentication, session management, and route security.
  • OktaAuthService: Manages user authentication, including login, logout, authentication checks, and session handling.
  • Securing Routes: Use OktaAuthGuard to restrict access to authenticated users.
  • Okta Callback Component: Handles the authentication callback from Okta.
  • Customizing the Sign-In Page: Customize through the Okta admin dashboard or with custom HTML/CSS.
  • Session Management: Use Okta's methods for creating, checking, renewing, and closing sessions.
  • Okta Token Manager: Manages tokens securely.
  • Error Handling: The SDK provides error handling capabilities.
  • Testing: Use unit tests with mock services.
  • Updating the SDK: Use npm update.

Okta simplifies authentication, allowing developers to focus on application features. A free developer account is available for experimentation. For questions, refer to Stack Overflow, Twitter, or GitHub.

The above is the detailed content of Build an Angular App with Okta's Sign-in Widget in 15 Minutes. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template