This guide walks you through integrating authentication and authorization into an Angular app using the Okta Auth JavaScript SDK (opens new window) (auth.js).
Note: The Okta Angular SDK (opens new window) builds on auth.js to implement many of the features shown in this guide. Our complete Angular Sample Apps (opens new window) are built using the Angular SDK. For a simple integration, Okta generally recommends using the Angular SDK. However, in certain cases it may be preferable to use auth.js directly, as shown here.
Prerequisites
If you don't already have a Developer Edition org, you can create one at https://developer.okta.com/signup/ (opens new window).
Add an OpenID Connect Client
- Sign in to the Okta developer dashboard, and select Create New App
- Choose Single Page App (SPA) as the platform, then populate your new OpenID Connect app with appropriate values for your app. For example:
Setting | Value |
Application Name | OpenID Connect App (must be unique) |
Login redirect URIs | http://localhost:4200/callback |
Logout redirect URIs | http://localhost:4200 |
Allowed grant types | Authorization Code |
Note: It’s important to choose the appropriate app type for apps that are public clients. Failing to do so may result in Okta API endpoints attempting to verify an app's client secret. Public clients aren’t designed to a client secret, hence breaking the sign-in or sign-out flow.
Note: CORS is automatically enabled for the granted login redirect URIs.
Create an Angular App
To create an Angular app, open a terminal and install the Angular CLI:
Now, create an app using the Angular CLI:
When asked Would you like to add Angular routing?
, press "y"
For this example use CSS
as the style engine. If you would like to use SCSS
or another style engine, you may need to make a few adjustments to the code snippets shown in this guide.
After all prompts have been answered, the Angular CLI will create a project in a folder named okta-app
and installs all required dependencies.
Install auth.js (opens new window) using npm
:
Create an Authentication Service
Users can sign in to your Angular app various different ways.
The easiest, and most secure, way is to use the default sign-in page. This page renders the Okta Sign-In Widget, equipped to handle User Lifecycle operations, MFA, and more.
First, create src/app/app.service.ts
as an authorization utility file and use it to bootstrap the required fields to sign in:
Important: We're using the org authorization server to make setup easy, but it's less flexible than a custom authorization server. Many SPAs send access tokens to access APIs. If you're building an API that needs to accept access tokens, create a custom authorization server.
Create an Authorization Guard
Now that you have a shared service to start, control, and end the authentication state, use it to protect the endpoints of an app.
Create src/app/app.guard.ts
that implements CanActivate
(opens new window):
Whenever a user attempts to access a route that is protected by OktaAuthGuard
, it checks to see if the user has been authenticated. If isAuthenticated()
returns false
, the sign-in flow begins.
Add Routes
Let's look at what routes are needed:
/
: A default page to handle basic control of the app. /callback
: Handle the response back from Okta and store the returned tokens. /protected
: A protected route by the OktaAuthGuard
.
/
First, update src/app/app.component.ts
to inject the OktaAuthService
into the component. This makes the service available within the component's template as the variable oktaAuth
. Subscribe to the Observable
exposed by the OktaAuthService
. This keeps the variable isAuthenticated
updated. Use this variable within the template to control the visibility of the sign-in and sign-out buttons.
Next, update src/app/app.component.html
with some buttons to trigger login or logout. Also add a link to the /protected
route. There may be a large block of "placeholder" code in this file generated by the Angular CLI. You can safely remove this.
/callback
To handle the redirect back from Okta, capture the token values from the URL. Use the /callback
route to handle the logic of storing these tokens and redirecting back to the main page.
Create a component src/app/callback.component.ts
:
/protected
This route is protected by OktaAuthGuard
, only permitting authenticated users with a valid accessToken
.
Create a component src/app/protected.component.ts
:
Connect the Routes
Add each of our new routes to src/app/app-routing.module.ts
:
Notice how the path /protected uses the canActivate
parameter to gate access to the route.
Update your @NgModule
in src/app/app.module.ts
:
- Import the newly created components
- Add the components to the array of
declarations
Build and start the app. In the terminal:
After the server starts, this message appears in your terminal:
** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
Conclusion
You have now successfully authenticated with Okta. With a user's id_token
, you have basic claims for the user's identity. You can extend the set of claims by modifying the scopes
to retrieve custom information about the user. This includes locale
, address
, groups
, and more.