Skip to content
This repository was archived by the owner on Oct 26, 2024. It is now read-only.

Commit 5ac6dc1

Browse files
committed
fix PR issues
1 parent ad769db commit 5ac6dc1

File tree

7 files changed

+63
-52
lines changed

7 files changed

+63
-52
lines changed

src/_constants/unions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import {InjectionToken} from '@angular/core';
22

33
export const unions: UnionMap = {
44
mydata: {key: 'mydata', name: 'My Data', url: 'https://mydata.webtree.org/applyToken'},
5-
imprint: {key: 'imprint', name: 'Imprint', url: 'https://imprint.webtree.org/applyToken'}
5+
imprint: {key: 'imprint', name: 'Imprint', url: 'https://imprint.webtree.org/applyToken'},
6+
unions: {key: 'unions', name: 'Unions', url: 'https://unions.webtree.org/!/applyToken'}
67
};
78

89
export interface Union {

src/_helpers/login.guard.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Injectable} from '@angular/core';
2-
import {ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot} from '@angular/router';
2+
import {ActivatedRouteSnapshot, CanActivate} from '@angular/router';
33
import {Observable} from 'rxjs';
44
import {map} from 'rxjs/operators';
55
import {AuthenticationService} from '../_services/authentication.service';
@@ -13,15 +13,16 @@ export class LoginGuard implements CanActivate {
1313
) {
1414
}
1515

16-
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
16+
canActivate(route: ActivatedRouteSnapshot): Observable<boolean> | boolean {
1717
if (!this.tokenService.tokenExists()) {
1818
return true;
1919
}
2020

2121
return this.tokenService.isTokenValid().pipe(
2222
map(isValid => {
23-
if (isValid) {
24-
return !this.authenticationService.redirectToUnionIfNeeded(route);
23+
if (isValid && this.authenticationService.shouldRedirect(route.queryParamMap)) {
24+
this.authenticationService.redirect(route.queryParamMap);
25+
return true;
2526
}
2627
return true;
2728
})
Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
22
import {Inject, Injectable} from '@angular/core';
3-
import {Observable, of} from 'rxjs';
3+
import {Observable, throwError} from 'rxjs';
44
import {catchError, map} from 'rxjs/operators';
55
import {TokenService} from './token.service';
66
import {environment} from '../environments/environment';
77
import {User} from '../_models/User';
8-
import {AlertService} from './alert.service';
9-
import {ActivatedRouteSnapshot} from '@angular/router';
8+
import {ParamMap} from '@angular/router';
109
import {Union, UnionMap, UNIONS_TOKEN} from '../_constants/unions';
1110
import {DOCUMENT} from '@angular/common';
1211

1312
@Injectable({providedIn: 'root'})
1413
export class AuthenticationService {
1514
constructor(private http: HttpClient,
16-
private alertService: AlertService,
1715
private tokenService: TokenService,
1816
@Inject(UNIONS_TOKEN) private unions: UnionMap,
1917
@Inject(DOCUMENT) private document: Document,
2018
) {
2119
}
2220

23-
login(user: User): Observable<string | null> {
21+
login(user: User): Observable<Login | null> {
2422
return this.http.post<{ token: string }>(environment.backendUrl + 'token/new', user)
2523
.pipe(
2624
map(res => {
2725
if ('token' in res) {
28-
return res.token;
26+
return { token: res.token };
2927
}
30-
return null;
28+
throw new Error(`Valid token is not returned. Got: ${res}`);
3129
}),
32-
catchError((error: HttpErrorResponse) => {
33-
console.log(error);
34-
if (error.status === 401) {
35-
this.alertService.error(error.error);
36-
return of(null);
30+
catchError((error: Error | HttpErrorResponse) => {
31+
if ('status' in error) {
32+
if (error.status === 401) {
33+
return throwError('Invalid username or password');
34+
}
35+
return throwError(error.message || error.error);
3736
}
37+
return throwError(error);
3838
})
3939
);
4040
}
@@ -43,25 +43,32 @@ export class AuthenticationService {
4343
this.tokenService.removeToken();
4444
}
4545

46-
redirectToUnionIfNeeded(route: ActivatedRouteSnapshot): boolean {
47-
const returnUnion = route.queryParamMap.get('returnUnion');
46+
shouldRedirect(queryParamMap: ParamMap): boolean {
47+
const returnUnion = this.getReturnUnion(queryParamMap);
4848

4949
if (typeof returnUnion === 'string') {
5050
if (returnUnion in this.unions) {
51-
this.redirectToUnion(this.unions[returnUnion]);
5251
return true;
5352
} else {
54-
this.alertService.error(`Unknown union: ${returnUnion}`);
53+
throw new Error(`Unknown union: ${returnUnion}`);
5554
}
5655
}
5756

5857
return false;
5958
}
6059

61-
redirectToUnion({url}: Union): void {
60+
redirect(queryParamMap: ParamMap): void {
61+
const returnUnion = this.getReturnUnion(queryParamMap);
62+
this.redirectToUnion(this.unions[returnUnion]);
63+
}
64+
65+
redirectToUnion({ url }: Union): void {
6266
const token = this.tokenService.getToken();
63-
const tokenizedUrl = `${url}#token=${token}`;
6467

65-
this.document.location.href = tokenizedUrl;
68+
this.document.location.href = `${url}#token=${token}`;
69+
}
70+
71+
getReturnUnion(queryParamMap: ParamMap): string | null {
72+
return queryParamMap.get('returnUnion');
6673
}
6774
}

src/_services/user.service.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,19 @@ import {HttpClient, HttpErrorResponse} from '@angular/common/http';
55
import {Observable} from 'rxjs/Observable';
66
import {environment} from '../environments/environment';
77
import {catchError} from 'rxjs/operators';
8-
import {of, throwError} from 'rxjs';
8+
import {throwError} from 'rxjs';
99

1010
@Injectable({providedIn: 'root'})
1111
export class UserService {
1212
constructor(private http: HttpClient) {
1313
}
1414

15-
create(user: User): Observable<User | null> {
15+
create(user: User): Observable<User> {
1616
const url = environment.backendUrl + 'user/register';
1717

1818
return this.http.post<User | null>(url, user).pipe(
19-
catchError((error: HttpErrorResponse) => {
20-
console.error(error);
21-
if (error.status === 400) {
22-
return of(null);
23-
} else {
24-
return throwError(error);
25-
}
19+
catchError(({message}: HttpErrorResponse) => {
20+
return throwError(`${message}`);
2621
})
2722
);
2823
}

src/login/login.component.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {FormGroup, FormControl, Validators} from '@angular/forms';
66
import {AlertService} from '../_services/alert.service';
77
import {AuthenticationService} from '../_services/authentication.service';
88
import {TokenService} from '../_services/token.service';
9+
import {finalize} from 'rxjs/operators';
910

1011
@Component({
1112
selector: 'app-login',
@@ -32,19 +33,21 @@ export class LoginComponent {
3233
this.loading = true;
3334
const user: User = {username, password: sha512(password)};
3435
return this.authenticationService.login(user)
35-
.subscribe(token => {
36-
this.loading = false;
37-
if (token === null) {
38-
this.alertService.error('Invalid username or password');
39-
return;
40-
}
41-
36+
.pipe(finalize(() => {
37+
this.loading = false;
38+
}))
39+
.subscribe(({ token }) => {
4240
this.tokenService.saveToken(token);
4341
this.alertService.success('Logged in successfully');
44-
if (!this.authenticationService.redirectToUnionIfNeeded(this.route.snapshot)) {
42+
43+
const queryParamMap = this.route.snapshot.queryParamMap;
44+
if (this.authenticationService.shouldRedirect(queryParamMap)) {
45+
this.authenticationService.redirect(queryParamMap);
46+
} else {
4547
this.router.navigate(['/select-union']);
4648
}
47-
}
48-
);
49+
}, (error: string) => {
50+
this.alertService.error(error);
51+
});
4952
}
5053
}

src/register/register.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<h2>Login</h2>
1+
<h2>Register</h2>
22
<form [formGroup]="form" (ngSubmit)="onSubmit(form.value)">
33
<p>
44
<mat-form-field>

src/register/register.component.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {UserService} from '../_services/user.service';
44
import {User} from '../_models/User';
55
import {sha512} from 'js-sha512';
66
import {FormControl, FormGroup, Validators} from '@angular/forms';
7+
import {finalize} from 'rxjs/operators';
8+
import {Router} from '@angular/router';
79

810
@Component({
911
selector: 'app-register',
@@ -20,21 +22,23 @@ export class RegisterComponent {
2022
loading = false;
2123

2224
constructor(private userService: UserService,
23-
private alertService: AlertService) {
25+
private alertService: AlertService,
26+
private router: Router) {
2427
}
2528

2629
onSubmit({username, password}) {
2730
this.loading = true;
2831
const user: User = {username, password: sha512(password)};
2932
this.userService.create(user)
33+
.pipe(finalize(() => {
34+
this.loading = false;
35+
}))
3036
.subscribe(
31-
data => {
32-
if (data === null) {
33-
this.alertService.error('Registration unsuccessful');
34-
} else {
35-
this.alertService.success('Registration successful');
36-
}
37-
// this.router.navigate(['/login']);
37+
() => {
38+
this.alertService.success('Registration successful');
39+
this.router.navigate(['/login']);
40+
}, (error: string) => {
41+
this.alertService.error(error);
3842
});
3943
}
4044
}

0 commit comments

Comments
 (0)