-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c593e8
commit f47a69e
Showing
10 changed files
with
113 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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,17 @@ | ||
<h1 class="text-3xl font-bold">Admin</h1> | ||
<div> | ||
<div>User:</div> | ||
{{ user() | json }} | ||
</div> | ||
|
||
@if (query.isPending()) { | ||
<div>Loading...</div> | ||
} @else if (query.error()) { | ||
<div>Something went wrong...</div> | ||
} | ||
@if (query.data(); as data) { | ||
<div> | ||
<div>Greeting:</div> | ||
{{ data | json }} | ||
</div> | ||
} |
This file contains 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,26 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { Component, inject } from '@angular/core'; | ||
import { AdminService } from '@app/core/modules/openapi/api/admin.service'; | ||
import { SecurityStore } from '@app/core/security/security-store.service'; | ||
import { injectQuery } from '@tanstack/angular-query-experimental'; | ||
import { lastValueFrom } from 'rxjs'; | ||
|
||
@Component({ | ||
selector: 'app-admin', | ||
standalone: true, | ||
imports: [CommonModule], | ||
templateUrl: './admin.component.html' | ||
}) | ||
export class AdminComponent { | ||
adminService = inject(AdminService); | ||
securityStore = inject(SecurityStore); | ||
|
||
signedIn = this.securityStore.signedIn; | ||
user = this.securityStore.loadedUser; | ||
|
||
query = injectQuery(() => ({ | ||
enabled: this.signedIn(), | ||
queryKey: ['admin', 'greeting'], | ||
queryFn: async () => lastValueFrom(this.adminService.getGretting()) | ||
})); | ||
} |
This file contains 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 { type Meta, type StoryObj } from '@storybook/angular'; | ||
import { AdminComponent } from './admin.component'; | ||
|
||
const meta: Meta<AdminComponent> = { | ||
title: 'pages/admin', | ||
component: AdminComponent, | ||
tags: ['autodocs'] | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<AdminComponent>; | ||
|
||
export const Default: Story = { | ||
render: (args) => ({ | ||
props: args, | ||
template: `<app-admin />` | ||
}) | ||
}; |
This file contains 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,8 +1,15 @@ | ||
import { Routes } from '@angular/router'; | ||
import { AboutComponent } from 'app/about/about.component'; | ||
import { HomeComponent } from 'app/home/home.component'; | ||
import { AboutComponent } from '@app/about/about.component'; | ||
import { HomeComponent } from '@app/home/home.component'; | ||
import { AdminComponent } from '@app/admin/admin.component'; | ||
import { AdminGuard } from '@app/core/security/admin.guard'; | ||
|
||
export const routes: Routes = [ | ||
{ path: '', component: HomeComponent }, | ||
{ path: 'about', component: AboutComponent } | ||
{ path: 'about', component: AboutComponent }, | ||
{ | ||
path: 'admin', | ||
component: AdminComponent, | ||
canActivate: [AdminGuard] | ||
} | ||
]; |
This file contains 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,26 @@ | ||
import { inject, Injectable, Injector } from '@angular/core'; | ||
import { toObservable } from '@angular/core/rxjs-interop'; | ||
import { CanActivate, Router, UrlTree } from '@angular/router'; | ||
import { SecurityStore } from '@app/core/security/security-store.service'; | ||
import { filter, map, Observable } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class AdminGuard implements CanActivate { | ||
injector = inject(Injector); | ||
securityStore = inject(SecurityStore); | ||
router = inject(Router); | ||
|
||
canActivate(): Observable<boolean | UrlTree> { | ||
return toObservable(this.securityStore.loadedUser, { injector: this.injector }).pipe( | ||
filter(Boolean), | ||
map((user) => { | ||
if (user && user.roles.includes('ADMIN')) { | ||
return true; | ||
} | ||
return this.router.createUrlTree(['/']); | ||
}) | ||
); | ||
} | ||
} |
This file contains 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 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 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 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 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