Revize 464309d9
Přidáno uživatelem Jakub Hlaváč před téměř 4 roky(ů)
angular.json | ||
---|---|---|
28 | 28 |
"src/assets" |
29 | 29 |
], |
30 | 30 |
"styles": [ |
31 |
"node_modules/primeicons/primeicons.css", |
|
32 |
"node_modules/primeng/resources/themes/bootstrap4-light-blue/theme.css", |
|
33 |
"node_modules/primeng/resources/primeng.min.css", |
|
31 | 34 |
"node_modules/bootstrap/dist/css/bootstrap.css", |
32 | 35 |
"src/styles.scss" |
33 | 36 |
], |
src/app/index/components/index.component.html | ||
---|---|---|
1 |
<app-nav-bar></app-nav-bar> |
|
2 | ||
3 |
<button (click)="getGroups()">GetGroups</button> |
|
4 | ||
5 | ||
6 |
{{ groups | json}} |
src/app/index/components/index.component.ts | ||
---|---|---|
1 |
import {Component, OnInit} from '@angular/core'; |
|
2 |
import {GroupService} from '../../shared/api/endpoints/services/group.service'; |
|
3 |
import {tap} from 'rxjs/operators'; |
|
4 |
import {Group} from '../../shared/api/endpoints/models/group'; |
|
5 | ||
6 |
@Component({ |
|
7 |
selector: 'app-index', |
|
8 |
templateUrl: './index.component.html', |
|
9 |
styleUrls: ['./index.component.scss'] |
|
10 |
}) |
|
11 |
export class IndexComponent implements OnInit { |
|
12 | ||
13 |
groups: Group[]; |
|
14 | ||
15 |
constructor( |
|
16 |
private groupService: GroupService |
|
17 |
) { |
|
18 |
} |
|
19 | ||
20 |
ngOnInit(): void { |
|
21 |
} |
|
22 | ||
23 |
getGroups() { |
|
24 |
this.groupService.getGroups({Operation: 'GetGroups'}).pipe( |
|
25 |
tap(data => this.groups = data) |
|
26 |
).toPromise(); |
|
27 |
} |
|
28 | ||
29 |
} |
src/app/index/index.module.ts | ||
---|---|---|
1 |
import {NgModule} from '@angular/core'; |
|
2 |
import {CommonModule} from '@angular/common'; |
|
3 |
import {IndexComponent} from './components/index.component'; |
|
4 |
import {NavBarModule} from '../shared/nav-bar/nav-bar.module'; |
|
5 | ||
6 | ||
7 |
@NgModule({ |
|
8 |
declarations: [IndexComponent], |
|
9 |
imports: [ |
|
10 |
CommonModule, |
|
11 |
NavBarModule |
|
12 |
] |
|
13 |
}) |
|
14 |
export class IndexModule { |
|
15 |
} |
src/app/shared/nav-bar/components/login/login.component.html | ||
---|---|---|
1 |
<p-dialog [(visible)]="showLogin" (onHide)="hideLoginForm()" [draggable]="false"> |
|
2 |
<ng-template pTemplate="header"> |
|
3 |
Login! |
|
4 |
</ng-template> |
|
5 | ||
6 |
<div class="card-body"> |
|
7 |
<form [formGroup]="loginForm" (ngSubmit)="onSubmit()"> |
|
8 |
<div class="form-group"> |
|
9 |
<label for="username">Login</label> |
|
10 |
<input type="text" formControlName="username" class="form-control" id="username" autocomplete="username"/> |
|
11 |
</div> |
|
12 |
<div class="form-group"> |
|
13 |
<label for="password">Password</label> |
|
14 |
<input type="password" formControlName="password" class="form-control" id="password" |
|
15 |
autocomplete="current-password"/> |
|
16 |
</div> |
|
17 |
<div class="form-group"> |
|
18 |
<button class="btn btn-primary"> |
|
19 |
Log in |
|
20 |
</button> |
|
21 |
</div> |
|
22 |
</form> |
|
23 |
</div> |
|
24 | ||
25 | ||
26 |
<p-footer> |
|
27 | ||
28 |
</p-footer> |
|
29 |
</p-dialog> |
src/app/shared/nav-bar/components/login/login.component.ts | ||
---|---|---|
1 |
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core'; |
|
2 |
import {FormBuilder, FormGroup, Validators} from '@angular/forms'; |
|
3 |
import {LoginForm} from '../../models/login-form'; |
|
4 | ||
5 |
@Component({ |
|
6 |
selector: 'app-login', |
|
7 |
templateUrl: './login.component.html', |
|
8 |
styleUrls: ['./login.component.scss'] |
|
9 |
}) |
|
10 |
export class LoginComponent implements OnInit { |
|
11 | ||
12 |
@Input() showLogin; |
|
13 |
@Output() showLoginChange: EventEmitter<boolean> = new EventEmitter<boolean>(); |
|
14 |
@Output() emitLoginForm: EventEmitter<LoginForm> = new EventEmitter<LoginForm>(); |
|
15 | ||
16 |
submitted: boolean; |
|
17 |
loginForm: FormGroup; |
|
18 |
showError = false; |
|
19 | ||
20 |
constructor( |
|
21 |
private formBuilder: FormBuilder |
|
22 |
) { |
|
23 |
} |
|
24 | ||
25 |
ngOnInit(): void { |
|
26 |
this.loginForm = this.formBuilder.group({ |
|
27 |
username: ['', [Validators.required]], |
|
28 |
password: ['', Validators.required] |
|
29 |
}); |
|
30 |
} |
|
31 | ||
32 |
hideLoginForm(): void { |
|
33 |
this.showLoginChange.emit(false); |
|
34 |
} |
|
35 | ||
36 |
onSubmit(): void { |
|
37 |
if (this.loginForm.valid) { |
|
38 |
this.emitLoginForm.emit({ |
|
39 |
username: this.loginForm.controls.username.value, |
|
40 |
password: this.loginForm.controls.password.value |
|
41 |
}); |
|
42 |
} |
|
43 |
} |
|
44 |
} |
src/app/shared/nav-bar/components/nav-bar.component.html | ||
---|---|---|
1 |
<nav class="navbar navbar-expand-md navbar-light bg-light"> |
|
2 |
<a class="navbar-brand" href="" target="_blank"> |
|
3 |
<img class="logo" src="/assets/images/SensLog.png" alt="SensLog"> |
|
4 |
</a> |
|
5 | ||
6 |
<div class="collapse navbar-collapse" id="navbarSupportedContent"> |
|
7 |
<div class="navbar-collapse collapse w-100 order-1 order-md-0 dual-collapse2"> |
|
8 |
<ul class="navbar-nav me-auto"> |
|
9 |
<li class="nav-item"> |
|
10 |
<a class="nav-link" href="#">Link</a> |
|
11 |
</li> |
|
12 |
<li class="nav-item"> |
|
13 |
<a class="nav-link" href="#">Link</a> |
|
14 |
</li> |
|
15 |
<li class="nav-item"> |
|
16 |
<a class="nav-link" href="#">Link</a> |
|
17 |
</li> |
|
18 |
</ul> |
|
19 |
</div> |
|
20 |
<div class="navbar-collapse collapse w-100 order-3 dual-collapse2"> |
|
21 |
<ul class="navbar-nav ms-auto"> |
|
22 |
<li class="divider-vertical"></li> |
|
23 |
<li class="nav-item"> |
|
24 |
<a *ngIf="!loggedIn" class='nav-link' (click)="showLoginFunction()">Login</a> |
|
25 |
<a *ngIf="loggedIn" class='nav-link' (click)="logOut()">Log out</a> |
|
26 |
</li> |
|
27 |
</ul> |
|
28 |
</div> |
|
29 |
</div> |
|
30 |
</nav> |
|
31 | ||
32 | ||
33 |
<app-login [(showLogin)]="showLogin" (emitLoginForm)="completeLogin($event)"></app-login> |
src/app/shared/nav-bar/components/nav-bar.component.scss | ||
---|---|---|
1 |
.logo { |
|
2 |
height: 3rem; |
|
3 |
} |
|
4 | ||
5 |
.navbar-text { |
|
6 |
color: black; |
|
7 |
font-weight: bold; |
|
8 |
} |
|
9 | ||
10 |
.divider-vertical { |
|
11 |
height: 50px; |
|
12 |
margin: 0 9px; |
|
13 |
border-left: 1px solid #F2F2F2; |
|
14 |
border-right: 1px solid #FFF; |
|
15 |
} |
src/app/shared/nav-bar/components/nav-bar.component.ts | ||
---|---|---|
1 |
import {Component, OnDestroy, OnInit} from '@angular/core'; |
|
2 |
import {LoginForm} from '../models/login-form'; |
|
3 |
import {AuthService} from '../../../auth/services/auth.service'; |
|
4 |
import {Router} from '@angular/router'; |
|
5 |
import {of, Subscription} from 'rxjs'; |
|
6 |
import {switchMap} from 'rxjs/operators'; |
|
7 |
import {UserState} from '../../../auth/states/user.state'; |
|
8 | ||
9 |
@Component({ |
|
10 |
selector: 'app-nav-bar', |
|
11 |
templateUrl: './nav-bar.component.html', |
|
12 |
styleUrls: ['./nav-bar.component.scss'] |
|
13 |
}) |
|
14 |
export class NavBarComponent implements OnInit, OnDestroy { |
|
15 | ||
16 |
showLogin = false; |
|
17 |
subscription: Subscription[] = []; |
|
18 |
loggedIn = false; |
|
19 | ||
20 |
constructor( |
|
21 |
private authService: AuthService, |
|
22 |
private router: Router, |
|
23 |
private userState: UserState |
|
24 |
) { |
|
25 |
} |
|
26 | ||
27 |
ngOnInit(): void { |
|
28 |
this.userState.getLoggedIn$().subscribe(res => { |
|
29 |
this.loggedIn = res; |
|
30 |
}); |
|
31 |
} |
|
32 | ||
33 |
ngOnDestroy(): void { |
|
34 |
this.subscription.forEach(subs => subs.unsubscribe()); |
|
35 |
} |
|
36 | ||
37 |
showLoginFunction(): void { |
|
38 |
this.showLogin = true; |
|
39 |
} |
|
40 | ||
41 |
completeLogin(loginForm: LoginForm): void { |
|
42 |
this.subscription[2] = this.authService.doLogin({ |
|
43 |
username: loginForm.username, |
|
44 |
password: loginForm.password |
|
45 |
}) |
|
46 |
.pipe( |
|
47 |
switchMap(res => { |
|
48 |
if (res) { |
|
49 |
return this.authService.getUserState(); |
|
50 |
} else { |
|
51 |
return of(false); |
|
52 |
} |
|
53 |
}) |
|
54 |
).subscribe( |
|
55 |
res => { |
|
56 |
if (res) { |
|
57 |
console.log(res); |
|
58 |
this.showLogin = false; |
|
59 |
this.userState.setLoggedIn(true); |
|
60 |
this.subscription[2].unsubscribe(); |
|
61 |
} |
|
62 |
} |
|
63 |
); |
|
64 |
} |
|
65 | ||
66 |
logOut(): void { |
|
67 |
this.userState.setLoggedIn(false); |
|
68 |
this.authService.doLogout(); |
|
69 |
} |
|
70 |
} |
src/app/shared/nav-bar/models/login-form.ts | ||
---|---|---|
1 |
export interface LoginForm { |
|
2 |
username?: string; |
|
3 |
password?: string; |
|
4 |
} |
src/app/shared/nav-bar/nav-bar.module.ts | ||
---|---|---|
1 |
import {NgModule} from '@angular/core'; |
|
2 |
import {CommonModule} from '@angular/common'; |
|
3 |
import {NavBarComponent} from './components/nav-bar.component'; |
|
4 |
import {RouterModule} from '@angular/router'; |
|
5 |
import {LoginComponent} from './components/login/login.component'; |
|
6 |
import {DialogModule} from 'primeng/dialog'; |
|
7 |
import {ButtonModule} from 'primeng/button'; |
|
8 |
import {ReactiveFormsModule} from '@angular/forms'; |
|
9 | ||
10 | ||
11 |
@NgModule({ |
|
12 |
declarations: [NavBarComponent, LoginComponent], |
|
13 |
exports: [ |
|
14 |
NavBarComponent |
|
15 |
], |
|
16 |
imports: [ |
|
17 |
CommonModule, |
|
18 |
RouterModule, |
|
19 |
RouterModule, |
|
20 |
DialogModule, |
|
21 |
ButtonModule, |
|
22 |
ReactiveFormsModule |
|
23 |
] |
|
24 |
}) |
|
25 |
export class NavBarModule { |
|
26 |
} |
Také k dispozici: Unified diff
#8364 - Implementace loginu
+ first version of login form
+ requesting API and setting user to state