Skip to content
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

added user name #66

Merged
merged 5 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@ngxs/store": "^3.7.1",
"@types/leaflet": "1",
"angular-auth-oidc-client": "^11.5.0",
"jwt-decode": "^3.1.2",
"leaflet": "^1.7.1",
"rxjs": "~6.6.0",
"tslib": "^2.0.0",
Expand Down
8 changes: 4 additions & 4 deletions src/app/header/header.component.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="header-cover" fxLayout="row" fxLayoutAlign="space-between">
<div>Позашкілля</div>
<div *ngIf="(isAuthorized$ | async) then isAuthorized; else isUnauthorized">

</div>
<ng-template #isUnauthorized>
<button mat-raised-button (click)="login()">
Expand All @@ -13,13 +13,13 @@
<div class="authorization">
<button mat-raised-button color="primary"
matBadge="2" matBadgePosition="before" matBadgeColor="accent">
<i>
<i>
<mat-icon class="icon-message"> email</mat-icon>
</i>
</button>

<button mat-button [matMenuTriggerFor]="log">
<h1>{{user.firstName}}</h1>
<h1>{{userName$ | async}}</h1>
</button>
<mat-menu #log="matMenu">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add role support

<button mat-menu-item>Гуртки</button>
Expand All @@ -28,6 +28,6 @@ <h1>{{user.firstName}}</h1>
</mat-menu>

</div>

</ng-template>
</div>
26 changes: 13 additions & 13 deletions src/app/header/header.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MatDialog } from '@angular/material/dialog';
import { Actions, ofAction, Select, Store } from '@ngxs/store';
import { UserRegistrationState } from '../shared/store/user-registration.state';
import { Observable } from 'rxjs';
import { Logout, CheckAuth, AuthFail, Login } from '../shared/store/user-registration.actions';
import {Logout, CheckAuth, AuthFail, Login, UserName, Role} from '../shared/store/user-registration.actions';
import { MatSnackBar } from '@angular/material/snack-bar';
import { Router } from '@angular/router';

Expand All @@ -16,35 +16,35 @@ import { Router } from '@angular/router';
})
export class HeaderComponent implements OnInit {

user = {
firstName: 'Іванов В. М'
}

@Select(UserRegistrationState.isAuthorized)
isAuthorized$: Observable<boolean>;
@Select(UserRegistrationState.userName)
userName$: Observable<string>;
@Select(UserRegistrationState.role)
role$: Observable<string>;

constructor(private modalDialog: MatDialog,
public store: Store,
private actions$: Actions,
public snackBar: MatSnackBar,
private router: Router) {
public store: Store,
private actions$: Actions,
public snackBar: MatSnackBar,
private router: Router) {
actions$.pipe(
ofAction(AuthFail)
).subscribe(action => {
this.snackBar.open('Check your connection', 'Try again!', {
duration: 5000,
panelClass: ['red-snackbar'],
});
})
});
}

ngOnInit(): void {
this.store.dispatch(new CheckAuth())
this.store.dispatch(new CheckAuth());
}
logout(): void {
this.store.dispatch(new Logout())
this.store.dispatch(new Logout());
}
login(): void{
this.store.dispatch(new Login())
this.store.dispatch(new Login());
}
}
10 changes: 9 additions & 1 deletion src/app/shared/store/user-registration.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ export class CheckAuth {
export class AuthFail {
static readonly type = '[user] has auth failed';
constructor() {}
}
}
export class UserName {
static readonly type = '[user] name added';
constructor() {}
}
export class Role {
static readonly type = '[role] added';
constructor() {}
}
27 changes: 22 additions & 5 deletions src/app/shared/store/user-registration.state.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { Injectable } from '@angular/core';
import { State, Action, StateContext, Selector } from '@ngxs/store';
import { Login, Logout, CheckAuth, AuthFail } from './user-registration.actions';
import {Login, Logout, CheckAuth, AuthFail} from './user-registration.actions';

import { HttpClient } from '@angular/common/http';
import { OidcSecurityService } from 'angular-auth-oidc-client';
import jwt_decode from 'jwt-decode';

export interface UserRegistrationStateModel {
isAuthorized: boolean;
userName: string;
role: string;
}

@State<UserRegistrationStateModel>({
name: 'user',
defaults: {
isAuthorized: false,
userName: '',
role: ''
}
})
@Injectable()
Expand All @@ -22,9 +27,17 @@ export class UserRegistrationState {
public http: HttpClient) {}

@Selector()
static isAuthorized(state: UserRegistrationStateModel) {
static isAuthorized(state: UserRegistrationStateModel): boolean {
return state.isAuthorized;
}
@Selector()
static userName(state: UserRegistrationStateModel): string {
return state.userName;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string

}
@Selector()
static role(state: UserRegistrationStateModel): string {
return state.role;
}

@Action(Login)
Login({ dispatch }: StateContext<UserRegistrationStateModel>): void {
Expand All @@ -36,16 +49,20 @@ export class UserRegistrationState {
dispatch(new CheckAuth());
}
@Action(CheckAuth)
CheckAuth({ patchState }: StateContext<UserRegistrationStateModel>): void {
CheckAuth({ patchState }: StateContext<UserRegistrationStateModel>, { dispatch }: StateContext<UserRegistrationStateModel>): void {
this.oidcSecurityService
.checkAuth()
.subscribe((auth) => {
console.log('is authenticated', auth)
console.log('is authenticated', auth);
patchState({ isAuthorized: auth});
if (auth) {
patchState({role: jwt_decode(this.oidcSecurityService.getToken()).role});
patchState({userName: jwt_decode(this.oidcSecurityService.getToken()).name});
}
});
}
@Action(AuthFail)
AuthFail(): void {
console.log("Authorization failed");
console.log('Authorization failed');
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6147,6 +6147,11 @@ jszip@^3.1.3:
readable-stream "~2.3.6"
set-immediate-shim "~1.0.1"

jwt-decode@^3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/jwt-decode/-/jwt-decode-3.1.2.tgz#3fb319f3675a2df0c2895c8f5e9fa4b67b04ed59"
integrity sha512-UfpWE/VZn0iP50d8cz9NrZLM9lSWhcJ+0Gt/nm4by88UL+J1SiKN8/5dkjMmbEzwL2CAe+67GsegCbIKtbp75A==

karma-coverage@~2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/karma-coverage/-/karma-coverage-2.0.3.tgz#c10f4711f4cf5caaaa668b1d6f642e7da122d973"
Expand Down