-
Notifications
You must be signed in to change notification settings - Fork 26
Add guards and interceptors create functions #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
luca-peruzzo
wants to merge
10
commits into
main
Choose a base branch
from
feature/enhance-angular
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0866a38
feat: enhance angular lib with guards and interceptors
luca-peruzzo 01c1c0c
chore: temp usage of dist folder for referencing lib
luca-peruzzo b247d4c
feat: adapt angular examples with new apis
luca-peruzzo 445ad1a
fix: wrong callerName usage
luca-peruzzo 1b1b0ea
fix: wrong conditions position
luca-peruzzo eff81eb
fix: use relative path for fetch
luca-peruzzo c80d811
fix: rebase conflicts
luca-peruzzo 9f8bf1c
feat: add SSR support
luca-peruzzo 77404c9
fix: add early return on provide function
luca-peruzzo 37bc029
Merge pull request #99 from keycloakify/feature/angular-ssr
garronej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { ApplicationConfig, mergeApplicationConfig } from '@angular/core'; | ||
| import { provideServerRendering, withRoutes } from '@angular/ssr'; | ||
| import { appConfig } from './app.config'; | ||
| import { serverRoutes } from './app.routes.server'; | ||
|
|
||
| const serverConfig: ApplicationConfig = { | ||
| providers: [provideServerRendering(withRoutes(serverRoutes))], | ||
| }; | ||
|
|
||
| export const config = mergeApplicationConfig(appConfig, serverConfig); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,23 @@ | ||
| import { provideHttpClient, withInterceptors } from '@angular/common/http'; | ||
| import { | ||
| ApplicationConfig, | ||
| inject, | ||
| provideBrowserGlobalErrorListeners, | ||
| provideZonelessChangeDetection, | ||
| } from '@angular/core'; | ||
| import { HttpClient, provideHttpClient, withInterceptors } from '@angular/common/http'; | ||
| import { provideClientHydration, withEventReplay } from '@angular/platform-browser'; | ||
| import { provideRouter } from '@angular/router'; | ||
| import { routes } from './app.routes'; | ||
| import { todoApiInterceptor } from './services/todo.service'; | ||
| import { Oidc } from './services/oidc.service'; | ||
| import { firstValueFrom } from 'rxjs'; | ||
| import { environment } from '../environments/environment'; | ||
|
|
||
| type RemoteOidcConfig = { | ||
| issuerUri: string; | ||
| clientId: string; | ||
| }; | ||
| import { routes } from './app.routes'; | ||
| import { BearerInterceptor } from './interceptors/bearer.interceptor'; | ||
| import { provideOidc } from './oidc'; | ||
|
|
||
| export const appConfig: ApplicationConfig = { | ||
| providers: [ | ||
| provideBrowserGlobalErrorListeners(), | ||
| provideZonelessChangeDetection(), | ||
| provideHttpClient(withInterceptors([todoApiInterceptor])), | ||
| provideHttpClient(withInterceptors([BearerInterceptor])), | ||
| provideRouter(routes), | ||
| environment.useMockOidc | ||
| ? Oidc.provideMock({ | ||
| isUserInitiallyLoggedIn: true, | ||
| }) | ||
| : Oidc.provide(async () => { | ||
| const http = inject(HttpClient); | ||
| const config = await firstValueFrom(http.get<RemoteOidcConfig>('./oidc-config.json')); | ||
|
|
||
| return { | ||
| issuerUri: config.issuerUri, | ||
| clientId: config.clientId, | ||
| debugLogs: true, | ||
| }; | ||
| }), | ||
| provideOidc(environment.useMockOidc), | ||
| provideClientHydration(withEventReplay()), | ||
| ], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { RenderMode, ServerRoute } from '@angular/ssr'; | ||
|
|
||
| export const serverRoutes: ServerRoute[] = [ | ||
| { path: '', renderMode: RenderMode.Prerender }, | ||
| { path: 'protected', renderMode: RenderMode.Client }, | ||
| { path: 'admin-only', renderMode: RenderMode.Client }, | ||
| { | ||
| path: '**', | ||
| renderMode: RenderMode.Client, | ||
| }, | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
examples/angular-kitchensink/src/app/guards/admin.guard.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { inject } from '@angular/core'; | ||
| import { CanActivateFn, RedirectCommand, Router } from '@angular/router'; | ||
| import { DecodedIdToken, Oidc } from '../services/oidc.service'; | ||
|
|
||
| export const AdminGuard: CanActivateFn = (route, state) => { | ||
| const router = inject(Router); | ||
| return Oidc.createAuthGuard<DecodedIdToken>(({ oidc }) => { | ||
| if ( | ||
| oidc.isUserLoggedIn && | ||
| (oidc.getDecodedIdToken().realm_access?.roles ?? []).includes('admin') | ||
| ) { | ||
| return true; | ||
| } | ||
| alert('Only Admins can access this page'); | ||
|
|
||
| return new RedirectCommand(router.parseUrl('/')); | ||
| })(route, state); | ||
| }; |
6 changes: 6 additions & 0 deletions
6
examples/angular-kitchensink/src/app/interceptors/bearer.interceptor.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { HttpInterceptorFn } from '@angular/common/http'; | ||
| import { Oidc } from '../services/oidc.service'; | ||
|
|
||
| export const BearerInterceptor: HttpInterceptorFn = Oidc.createBasicBearerTokenInterceptor({ | ||
| conditions: [{ urlPattern: /^(https:\/\/jsonplaceholder\.typicode\.com)(\/.*)?$/i }], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { EnvironmentProviders } from '@angular/core'; | ||
| import { Oidc } from './services/oidc.service'; | ||
|
|
||
| type RemoteOidcConfig = { | ||
| issuerUri: string; | ||
| clientId: string; | ||
| }; | ||
|
|
||
| export const provideOidc = (useMockOidc: boolean): EnvironmentProviders => | ||
| useMockOidc | ||
| ? Oidc.provideMock({ | ||
| isUserInitiallyLoggedIn: true, | ||
| }) | ||
| : Oidc.provide(async () => { | ||
| // should be runned outside angular to prevent http interceptor request piping | ||
| const config: RemoteOidcConfig = await fetch('/oidc-config.json').then((res) => res.json()); | ||
|
|
||
| return { | ||
| issuerUri: config.issuerUri, | ||
| clientId: config.clientId, | ||
| debugLogs: true, | ||
| }; | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
temporary, only for testing locally