Skip to content

Commit 89d64f5

Browse files
Merge pull request #42 from zazuko/auth-flow
fix and issue when the auth was invalid but a password was set
2 parents b19e479 + 7e7b4fc commit 89d64f5

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

projects/blueprint/src/app/core/service/auth/auth.service.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,39 @@ const localStorageAuthKey = 'com.zazuko.blueprint.bp-auth';
66
providedIn: 'root',
77
})
88
export class AuthService {
9-
private credentials: Credentials | null = null;
9+
#credentials: Credentials | null = null;
1010

1111
constructor() {
12-
const credentials = this.readSessionStorage();
12+
// this will only happen when the user is reloading the page
13+
const credentials = this.#readSessionStorage();
1314
if (credentials !== null) {
14-
this.credentials = credentials;
15+
this.#credentials = credentials;
1516
}
1617
}
1718

1819
public isAuthenticated(): boolean {
19-
return this.credentials !== null;
20+
return this.#credentials !== null;
2021
}
2122

2223
public getCredentials(): Credentials | null {
23-
return this.credentials;
24+
return this.#credentials;
2425
}
2526

2627
public updateCredentials(credentials: Credentials | null) {
27-
this.credentials = credentials;
28+
this.#credentials = credentials;
2829
if (credentials === null) {
29-
this.removeSessionStorage();
30+
this.#removeSessionStorage();
3031
} else {
31-
this.writeSessionStorage();
32+
this.#writeSessionStorage();
3233
}
3334
}
3435

35-
private readSessionStorage(): Credentials | null {
36+
public clear() {
37+
this.#credentials = null;
38+
this.#removeSessionStorage();
39+
}
40+
41+
#readSessionStorage(): Credentials | null {
3642
const sessionStorageData = sessionStorage.getItem(localStorageAuthKey);
3743
if (sessionStorageData === null) {
3844
return null;
@@ -51,23 +57,17 @@ export class AuthService {
5157
};
5258
}
5359

54-
private writeSessionStorage() {
55-
if (this.credentials === null) {
56-
this.readSessionStorage();
60+
#writeSessionStorage() {
61+
if (this.#credentials === null) {
62+
this.#readSessionStorage();
5763
}
5864
sessionStorage.setItem(localStorageAuthKey, JSON.stringify({
59-
username: this.credentials.username,
60-
password: this.credentials.password
65+
username: this.#credentials.username,
66+
password: this.#credentials.password
6167
}));
6268
}
6369

64-
65-
66-
public signOut() {
67-
this.readSessionStorage();
68-
}
69-
70-
private removeSessionStorage() {
70+
#removeSessionStorage() {
7171
sessionStorage.removeItem(localStorageAuthKey);
7272
}
7373
}

projects/blueprint/src/app/login/login.component.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,33 @@ import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
66
import { InputTextModule } from 'primeng/inputtext';
77
import { ButtonModule } from 'primeng/button';
88

9-
109
import { AuthService } from '@blueprint/service/auth/auth.service';
1110
import { SparqlService } from '@blueprint/service/sparql/sparql.service';
1211

13-
import { LogoComponent } from "../core/layout/logo/logo.component";
1412
import { BrandLogoComponent } from "../core/layout/brand-logo/brand-logo.component";
1513
import { MessageChannelService } from '../core/service/message-channel/message-channel.service';
1614

17-
1815
@Component({
1916
standalone: true,
2017
templateUrl: './login.component.html',
2118
styleUrls: ['./login.component.scss'],
2219
imports: [
23-
ReactiveFormsModule,
24-
LogoComponent,
20+
ReactiveFormsModule, //
2521
InputTextModule,
2622
ButtonModule,
2723
BrandLogoComponent
2824
]
2925
})
3026
export class LoginComponent implements OnInit {
31-
private readonly destroyRef = inject(DestroyRef);
27+
readonly #destroyRef = inject(DestroyRef);
3228

33-
private readonly router = inject(Router);
34-
private readonly route = inject(ActivatedRoute);
29+
readonly #router = inject(Router);
30+
readonly #route = inject(ActivatedRoute);
3531

36-
private readonly messageChannel = inject(MessageChannelService);
32+
readonly #messageChannel = inject(MessageChannelService);
3733

38-
private readonly authService = inject(AuthService);
39-
private readonly sparqlService = inject(SparqlService);
34+
readonly #authService = inject(AuthService);
35+
readonly #sparqlService = inject(SparqlService);
4036

4137
loginForm = new FormGroup({
4238
username: new FormControl<string>('', [Validators.required]),
@@ -47,33 +43,37 @@ export class LoginComponent implements OnInit {
4743
returnUrl = '';
4844

4945
ngOnInit(): void {
50-
if (this.authService.isAuthenticated()) {
51-
this.router.navigate(['search']);
46+
if (this.#authService.isAuthenticated()) {
47+
this.#router.navigate(['search']);
5248
return;
5349
}
54-
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
55-
this.loginForm.valueChanges.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => this.errorMessage = '');
50+
this.returnUrl = this.#route.snapshot.queryParams['returnUrl'] || '/';
51+
52+
this.loginForm.valueChanges.pipe(
53+
takeUntilDestroyed(this.#destroyRef)
54+
).subscribe(() => this.errorMessage = '');
5655
}
5756

5857
onSubmit(): void {
5958
const credentials = {
6059
username: this.loginForm.controls.username.value ?? '',
6160
password: this.loginForm.controls.password.value ?? ''
6261
};
63-
this.authService.updateCredentials(credentials);
62+
this.#authService.updateCredentials(credentials);
6463

65-
this.sparqlService
64+
this.#sparqlService
6665
.select('SELECT * WHERE { ?s ?p ?o . } LIMIT 1')
6766
.subscribe({
6867
next: () => {
69-
this.messageChannel.debug('Login successful');
70-
this.router.navigateByUrl(this.returnUrl);
68+
this.#messageChannel.debug('Login successful');
69+
this.#router.navigateByUrl(this.returnUrl);
7170
},
7271
error: () => {
7372
this.errorMessage = 'Wrong username or password';
74-
this.messageChannel.debug('Wrong username or password');
73+
this.#authService.clear();
74+
this.#messageChannel.debug('Wrong username or password');
7575
},
76-
complete: () => this.messageChannel.debug('Login Test SPARQL Query completed')
76+
complete: () => this.#messageChannel.debug('Login Test SPARQL Query completed')
7777

7878
}
7979
);

0 commit comments

Comments
 (0)