This project is a minimal Angular Single-Page Application that verifies the Okta redirect-based sign-in flow using OAuth 2.0 Authorization Code with PKCE. The app redirects the user to Okta's hosted sign-in page, authenticates them, and then returns to the Angular app at the callback route.
This sample demonstrates the recommended SPA pattern for Okta authentication:
- Authorization Code + PKCE for browser-based apps
- Okta redirect flow to the hosted sign-in page
- Callback handling with
OktaCallbackComponent - Access-token attachment using an HTTP interceptor
- Route protection with
canActivateAuthGuard - User profile retrieval after login
When the user clicks Login, the app calls signInWithRedirect(). Okta then redirects the browser to the Okta-hosted sign-in page where the user enters credentials. After successful authentication, Okta redirects the browser back to:
http://localhost:4200/login/callback
The app processes the callback and completes the token exchange. If authentication succeeds, the Angular routes behind canActivateAuthGuard become available.
This sample uses the Authorization Code flow with PKCE, which is the standard secure method for single-page applications running in a browser.
The sequence is:
- The Angular app creates an
OktaAuthinstance. - The user clicks Login.
- The browser is redirected to Okta.
- Okta shows the hosted sign-in page.
- The user authenticates.
- Okta redirects back to the configured callback URL.
- The app exchanges the code for tokens on the client side using PKCE.
- The app stores the tokens and uses them for API access.
In this repository, the flow is configured in src/app/app.config.ts and the callback route is registered in src/app/app.routes.ts.
Before running the app, you need:
- An Okta Developer org
- A Single-Page Application app created in Okta
- A valid Okta issuer URL
- A valid Okta client ID
- An Angular app running locally on
http://localhost:4200
Create an app in Okta as follows:
- Sign in to your Okta Admin Console.
- Go to Applications > Applications.
- Click Create App Integration.
- Choose:
- Sign-in method: OIDC - OpenID Connect
- Application type: Single-Page Application
- Click Next.
- Give the app a name such as
Angular PKCE SPA. - Under Grant type, enable:
- Authorization Code
- Refresh Token
- Add redirect URIs:
- Sign-in redirect URI:
http://localhost:4200/login/callback - Sign-out redirect URI:
http://localhost:4200
- Sign-in redirect URI:
- Save the app.
If you use a custom authorization server, make sure it has a policy and rule that includes Authorization Code grant. The default authorization server is usually sufficient for development unless your org has custom restrictions.
Update the Okta configuration in src/app/app.config.ts:
const oktaAuth = new OktaAuth({
clientId: '{yourClientId}',
issuer: 'https://{yourOktaDomain}/oauth2/default',
redirectUri: 'http://localhost:4200/login/callback',
scopes: ['openid', 'profile', 'email', 'offline_access'],
});After creating the app, open the app’s General tab to collect:
- Client ID: under Client Credentials
- Issuer: from the Authorization Server / Issuer URI
You may also need to add http://localhost:4200 as a Trusted Origin in Okta if your org requires it.
Clone the project and install dependencies:
git clone <your-repo-url>
cd okta-angular-sample
npm installStart the Angular app:
npm startThen open:
http://localhost:4200
The home page is served by src/app/home/home.component.ts and includes a Login action. Clicking it triggers:
await this.oktaAuth.signInWithRedirect();That command redirects the browser to the Okta login page.
The route login/callback is configured in src/app/app.routes.ts with OktaCallbackComponent:
{ path: 'login/callback', component: OktaCallbackComponent }This callback component finishes the login response and processes the tokens returned from Okta.
Protected pages use canActivateAuthGuard:
{ path: 'profile', component: ProfileComponent, canActivate: [canActivateAuthGuard] }If the user is not authenticated, the guard redirects them to Okta.
The app uses an HTTP interceptor in src/app/auth.interceptor.ts to attach the bearer token to safe local API requests:
const accessToken = oktaAuth.getAccessToken();The interceptor adds the Authorization: Bearer <token> header when the request is to a local origin.
src/
app/
app.config.ts # OktaAuth configuration
app.routes.ts # Angular routes and callback registration
auth.interceptor.ts # Adds access token to outgoing requests
home/
home.component.ts # Login trigger and auth state
profile/
messages/
protected/
To verify this setup works end-to-end:
- Start the app with
npm start. - Open
http://localhost:4200. - Click Login.
- You should be redirected to Okta’s hosted sign-in page.
- Sign in with a valid Okta user.
- After successful authentication, Okta redirects back to
http://localhost:4200/login/callback. - The app should complete the login and show authenticated state.
- Navigate to a protected route such as
/profileor/messages. - Confirm the route loads only after authentication succeeds.
- A logged-out user sees the home page and login option.
- After successful login, the app shows the authenticated user state.
- Protected routes are accessible only after the auth flow completes.
- The access token is sent in the Authorization header for allowed requests.
If login fails with an OAuth redirect error, verify the redirect URI exactly matches:
http://localhost:4200/login/callback
It must match the value configured in Okta and the app configuration.
If Okta rejects the request, confirm that:
clientIdis correctissuerpoints to the correct Okta authorization server- the app is created as a Single-Page Application
Ensure that:
- the Angular app is running on the correct port
- no other route is intercepting
/login/callback - the browser is not blocking the redirect
If outgoing requests do not include a bearer token:
- confirm the user is authenticated
- check
oktaAuth.getAccessToken()is returning a value - ensure the target URL matches an allowed origin in the interceptor
This project includes Angular specs for basic auth-related behavior. To run them:
npm run test- Okta Developer Docs: OAuth 2.0 and OIDC
- Okta Angular SDK documentation
- Authorization Code with PKCE for SPAs
This sample uses the Okta-hosted redirect model rather than an embedded Sign-In Widget. In other words, the user is redirected to Okta for authentication instead of embedding the widget directly inside the Angular page. The redirect approach is the most common and recommended setup for SPAs using PKCE.
If you want, this project can also be extended with:
- logout handling
- refresh token management
- custom login UX
- backend protected API integration