From c97ebaa41e57c0fd1fd18e0a7af614c7f13f0557 Mon Sep 17 00:00:00 2001 From: Charles Zhao Date: Tue, 2 Jul 2024 10:31:34 +0800 Subject: [PATCH] refactor(console): update angular integration guide (#6157) * refactor(console): update angular integration guide * refactor(console): improve content --------- Co-authored-by: Gao Sun --- .../assets/docs/guides/spa-angular/README.mdx | 62 ++++++++----------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/packages/console/src/assets/docs/guides/spa-angular/README.mdx b/packages/console/src/assets/docs/guides/spa-angular/README.mdx index fd7f2d05c2c..51ba02ef06d 100644 --- a/packages/console/src/assets/docs/guides/spa-angular/README.mdx +++ b/packages/console/src/assets/docs/guides/spa-angular/README.mdx @@ -6,12 +6,13 @@ import Steps from '@/mdx-components/Steps'; import Step from '@/mdx-components/Step'; import Checkpoint from '../../fragments/_checkpoint.md'; +import RedirectUrisWeb, {defaultRedirectUri, defaultPostSignOutUri} from '../../fragments/_redirect-uris-web.mdx'; @@ -21,44 +22,34 @@ npm i @logto/js angular-auth-oidc-client ``` - + ```bash -yarn add @logto/js angular-auth-oidc-client +pnpm add @logto/js angular-auth-oidc-client ``` - + ```bash -pnpm add @logto/js angular-auth-oidc-client +yarn add @logto/js angular-auth-oidc-client ``` - + - - In the following steps, we assume your app is running on http://localhost:3000. - + -### Configure redirect URIs - -First, let's enter your redirect URI. E.g. `http://localhost:3000/callback`. [Redirect URI](https://www.oauth.com/oauth2-servers/redirect-uris/) is an OAuth 2.0 concept which implies the location should redirect after authentication. - - - -After signing out, it'll be great to redirect user back to your website. For example, add `http://localhost:3000` as the post sign-out redirect URI below. - - + -### Configure Angular application + -Back to your Angular project, add the auth provider your `app.config.ts`: +In your Angular project, add the auth provider your `app.config.ts`: - + {`import { UserScope, buildAngularAuthConfig } from '@logto/js'; import { provideAuth } from 'angular-auth-oidc-client'; @@ -69,8 +60,8 @@ export const appConfig: ApplicationConfig = { config: buildAngularAuthConfig({ endpoint: '${props.endpoint}', appId: '${props.app.id}', - redirectUri: '${props.redirectUris[0] ?? 'http://localhost:3000/callback'}', - postLogoutRedirectUri: '${props.postLogoutRedirectUris[0] ?? 'http://localhost:3000'}', + redirectUri: '${props.redirectUris[0] || defaultRedirectUri}', + postLogoutRedirectUri: '${props.postLogoutRedirectUris[0] || defaultPostSignOutUri}', }), }), // ...other providers @@ -82,9 +73,9 @@ export const appConfig: ApplicationConfig = { -In the component where you want to implement sign-in and sign-out (for example, `app.component.ts`), inject the `OidcSecurityService` and use it to sign in and sign out. +In the component where you want to implement sign-in and sign-out, inject the `OidcSecurityService` and use it to sign in and sign out. -```ts +```ts title="app/app.component.ts" import { OidcSecurityService } from 'angular-auth-oidc-client'; export class AppComponent implements OnInit { @@ -112,11 +103,17 @@ Then, in the template, add buttons to sign in and sign out: - + + + + + + + The `OidcSecurityService` provides a convenient way to subscribe to the authentication state: -```ts +```ts title="app/app.component.ts" import { OidcSecurityService } from 'angular-auth-oidc-client'; import type { UserInfoResponse } from '@logto/js'; @@ -146,10 +143,11 @@ export class AppComponent implements OnInit { And use it in the template: -```html +```html title="app/app.component.html"
{{ userData | json }}
+

ID token: {{ idToken }}

Access token: {{ accessToken }}

@@ -158,12 +156,4 @@ And use it in the template:
- - - - - -