-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
onboard auth service,Angular template syntax
- Loading branch information
Showing
24 changed files
with
406 additions
and
42 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
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,4 @@ | ||
- Migrate to Angular templates. Get rid of "ngIf=" (in progress) | ||
- Create a dropdown button for sign out (in progress) | ||
- Update all dependencies | ||
- Rebuild dev container using dev container features |
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
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,36 +1,49 @@ | ||
import { Routes } from '@angular/router'; | ||
import { HomeComponent } from './home/home.component'; | ||
import { SigninComponent } from './auth/signin.component'; | ||
import { SigninRedirectCallbackComponent } from './auth/signin-redirect-callback.component'; | ||
import { hasRole } from './auth/hasRole'; | ||
|
||
export enum RouterTokens { | ||
WEEK = 'week', | ||
SIGNIN = 'signin', | ||
SIGNIN_REDIRECT_CALLBACK = 'signin-redirect-callback', | ||
WEEK = '', | ||
MONTH = 'month', | ||
YEAR = 'year', | ||
ALL_TIME = 'all-time', | ||
} | ||
|
||
export const routes: Routes = [ | ||
{ | ||
path: '', | ||
redirectTo: RouterTokens.WEEK, | ||
pathMatch: 'full', | ||
path: RouterTokens.SIGNIN, | ||
component: SigninComponent, | ||
}, | ||
{ | ||
path: RouterTokens.SIGNIN_REDIRECT_CALLBACK, | ||
component: SigninRedirectCallbackComponent, | ||
}, | ||
{ | ||
path: RouterTokens.WEEK, | ||
component: HomeComponent, | ||
pathMatch: 'full', | ||
data: { period: 7 }, | ||
canActivate: [() => hasRole('user')], | ||
}, | ||
{ | ||
path: RouterTokens.MONTH, | ||
component: HomeComponent, | ||
data: { period: 30 }, | ||
canActivate: [() => hasRole('user')], | ||
}, | ||
{ | ||
path: RouterTokens.YEAR, | ||
component: HomeComponent, | ||
data: { period: 365 }, | ||
canActivate: [() => hasRole('user')], | ||
}, | ||
{ | ||
path: RouterTokens.ALL_TIME, | ||
component: HomeComponent, | ||
canActivate: [() => hasRole('user')], | ||
}, | ||
]; |
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,92 @@ | ||
import { LocationStrategy } from '@angular/common'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { Injectable } from '@angular/core'; | ||
import { Router } from '@angular/router'; | ||
import { | ||
BehaviorSubject, | ||
catchError, | ||
map, | ||
of, | ||
shareReplay, | ||
switchMap, | ||
tap, | ||
} from 'rxjs'; | ||
import { RouterTokens } from '../app.routes'; | ||
|
||
type UserInfo = { sub: string; name: string; groups: string[] }; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
redirectUri = | ||
location.origin + | ||
this.locationStrategy.prepareExternalUrl( | ||
RouterTokens.SIGNIN_REDIRECT_CALLBACK | ||
); | ||
signinsAndSignouts = new BehaviorSubject<RouterTokens[] | undefined>( | ||
undefined | ||
); | ||
$userInfo = this.signinsAndSignouts.asObservable().pipe( | ||
switchMap((nextRoute) => | ||
this.http.get<UserInfo>('/auth/user-info').pipe( | ||
catchError(() => { | ||
return of({} as UserInfo); | ||
}), | ||
tap(() => nextRoute && this.router.navigate(nextRoute)) | ||
) | ||
), | ||
shareReplay(1) | ||
); | ||
|
||
constructor( | ||
private readonly http: HttpClient, | ||
private readonly router: Router, | ||
private readonly locationStrategy: LocationStrategy | ||
) {} | ||
|
||
getUserInfo() { | ||
return this.$userInfo; | ||
} | ||
|
||
async signin() { | ||
this.http | ||
.post<{ | ||
authorizationUrl: string; | ||
}>('/auth/authorize', { redirectUri: this.redirectUri }) | ||
.subscribe(({ authorizationUrl }) => { | ||
location.href = authorizationUrl; | ||
}); | ||
} | ||
|
||
async handleSigninRedirectCallback() { | ||
this.http | ||
.post<void>('/auth/get-token', { | ||
callbackUrl: location.href.toString(), | ||
redirectUri: this.redirectUri, | ||
}) | ||
.subscribe(() => this.signinsAndSignouts.next([RouterTokens.WEEK])); | ||
} | ||
|
||
isSignedIn() { | ||
return this.getUserInfo().pipe(map((userInfo) => !!userInfo.sub)); | ||
} | ||
|
||
getUserName() { | ||
return this.getUserInfo().pipe(map((userInfo) => userInfo.name)); | ||
} | ||
|
||
getRoles() { | ||
return this.getUserInfo().pipe( | ||
map((userInfo) => (userInfo.groups ?? []) as string[]) | ||
); | ||
} | ||
|
||
hasRole(role: string) { | ||
return this.getRoles().pipe(map((roles) => roles.includes(role))); | ||
} | ||
|
||
signout() { | ||
this.http | ||
.post('/auth/logout', {}) | ||
.subscribe(() => this.signinsAndSignouts.next([RouterTokens.SIGNIN])); | ||
} | ||
} |
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,20 @@ | ||
import { inject } from '@angular/core'; | ||
import { AuthService } from './auth.service'; | ||
import { Router } from '@angular/router'; | ||
import { tap } from 'rxjs'; | ||
import { RouterTokens } from '../app.routes'; | ||
|
||
export function hasRole(role: string) { | ||
const authService = inject(AuthService); | ||
const router = inject(Router); | ||
|
||
return authService.hasRole(role).pipe( | ||
tap((authorized) => { | ||
if (!authorized) { | ||
router.navigate([RouterTokens.SIGNIN]); | ||
} | ||
|
||
return authorized; | ||
}) | ||
); | ||
} |
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,15 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { AuthService } from './auth.service'; | ||
|
||
@Component({ | ||
standalone: true, | ||
selector: 'signin-redirect-callback', | ||
template: '', | ||
}) | ||
export class SigninRedirectCallbackComponent implements OnInit { | ||
constructor(private readonly authService: AuthService) {} | ||
|
||
ngOnInit(): void { | ||
this.authService.handleSigninRedirectCallback(); | ||
} | ||
} |
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,9 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
standalone: true, | ||
selector: 'signin', | ||
imports: [], | ||
template: '', | ||
}) | ||
export class SigninComponent {} |
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,5 @@ | ||
nav { | ||
display: grid; | ||
grid-template-columns: 1fr auto; | ||
gap: 20px; | ||
} |
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,11 @@ | ||
@if ($vm | async; as vm) { | ||
<nav> | ||
@if (vm.isSignedIn) { | ||
<h1 app-heading>Hello {{ vm.userName }}!</h1> | ||
<button app-button color="red" (click)="onSignout()" type="button">Sign out</button> | ||
} @else { | ||
<h1 app-heading>Demo</h1> | ||
<button app-button (click)="onSignin()" type="button">Sign in</button> | ||
} | ||
</nav> | ||
} |
Oops, something went wrong.