Skip to content

Commit 761435b

Browse files
committed
feat(frontend): store jwt in local storage
1 parent 79b08b0 commit 761435b

File tree

7 files changed

+57
-10
lines changed

7 files changed

+57
-10
lines changed

src/main/typescript/app/app.component.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {ClarityModule} from 'clarity-angular';
55
import {RouterTestingModule} from '@angular/router/testing';
66
import {ReactiveFormsModule, FormsModule} from '@angular/forms';
77
import {CoreModule} from './core/core.module';
8+
import {BaseRequestOptions, Http, ConnectionBackend, RequestOptions} from '@angular/http';
9+
import {MockBackend} from '@angular/http/testing';
10+
import {AuthService} from './core/store/auth/auth.service';
811

912
describe('AppComponent', () => {
1013

@@ -23,6 +26,18 @@ describe('AppComponent', () => {
2326
ReactiveFormsModule,
2427
ClarityModule.forRoot(),
2528
RouterTestingModule
29+
],
30+
providers: [
31+
BaseRequestOptions,
32+
MockBackend,
33+
{
34+
provide: Http,
35+
useFactory: function (backend: ConnectionBackend, defaultOptions: RequestOptions) {
36+
return new Http(backend, defaultOptions);
37+
},
38+
deps: [MockBackend, BaseRequestOptions]
39+
},
40+
AuthService
2641
]
2742
});
2843

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Component, OnInit} from '@angular/core';
2+
import {AuthService} from './core/store/auth/auth.service';
23

34
@Component({
45
selector: 'shardis-root',
@@ -7,10 +8,11 @@ import {Component, OnInit} from '@angular/core';
78
})
89
export class AppComponent implements OnInit {
910

10-
constructor() {
11+
constructor(private authService: AuthService) {
1112
}
1213

1314
ngOnInit() {
15+
this.authService.dispatchToken();
1416
}
1517

1618
}

src/main/typescript/app/core/store/auth/auth.service.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import {RootState} from '../index';
55
import {Observable} from 'rxjs/Observable';
66
import {LogoutAction, ProcessTokenAction, AuthErrorAction} from './auth.actions';
77
import {go} from '@ngrx/router-store';
8+
import {LocalStorage} from 'ng2-webstorage';
89

910

1011
@Injectable()
1112
export class AuthService {
1213

14+
@LocalStorage("token")
15+
private jwtToken: string;
16+
1317
private authenticated$: Observable<boolean>;
1418

1519
public static decodeAccessToken(access_token: string) {
@@ -20,8 +24,10 @@ export class AuthService {
2024
this.authenticated$ = store.select(s => s.auth.authenticated);
2125
}
2226

23-
public isAuthenticated(): Observable<boolean> {
24-
return this.authenticated$;
27+
public dispatchToken() {
28+
if (!!this.jwtToken) {
29+
this.store.dispatch(new ProcessTokenAction(this.jwtToken));
30+
}
2531
}
2632

2733
public authenticate(username: string, password: string): void {
@@ -46,9 +52,8 @@ export class AuthService {
4652
.post('/api/authentication', payload, {headers: headers})
4753
.subscribe(
4854
data => {
49-
const jwtToken = data.text();
50-
localStorage.setItem('tokenData', jwtToken);
51-
this.store.dispatch(new ProcessTokenAction(jwtToken));
55+
this.jwtToken = data.text();
56+
this.dispatchToken();
5257
this.store.dispatch(go('/'));
5358
},
5459
err => {
@@ -61,7 +66,7 @@ export class AuthService {
6166
}
6267

6368
public logout(): void {
64-
localStorage.removeItem('tokenData');
69+
this.jwtToken = null;
6570
this.store.dispatch(new LogoutAction());
6671
}
6772

src/main/typescript/app/core/store/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const reducers = {
2424

2525
const developmentReducer: ActionReducer<RootState> = compose(
2626
storeFreeze,
27-
localStorageSync(['counter', 'auth', 'router'], true),
27+
localStorageSync(['counter', 'router'], true),
2828
combineReducers)(reducers);
2929

3030
const productionReducer: ActionReducer<RootState> = compose(combineReducers)(reducers);

src/main/typescript/app/main/main-view/main-view.component.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
<li class="nav-item">
1414
<a class="nav-link" href="#" [routerLink]="['/']" [routerLinkActive]="'/active'">Home</a>
1515
</li>
16-
<li class="nav-item">
16+
<li class="nav-item" *ngIf="!(authenticated$ | async)">
1717
<a class="nav-link" href="#" [routerLink]="['/login']" [routerLinkActive]="'/login'">Login</a>
1818
</li>
19+
<li class="nav-item" *ngIf="authenticated$ | async">
20+
<a class="nav-link" href="#" (click)="logout()">Logout</a>
21+
</li>
1922
</ul>
2023
</nav>
2124
<div class="content-container">

src/main/typescript/app/main/main-view/main-view.component.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import {FormsModule, ReactiveFormsModule} from '@angular/forms';
66
import {CoreModule} from '../../core/core.module';
77
import {CounterModule} from '../../counter/counter.module';
88
import {ClarityModule} from 'clarity-angular';
9+
import {BaseRequestOptions, Http, RequestOptions, ConnectionBackend} from '@angular/http';
10+
import {MockBackend} from '@angular/http/testing';
11+
import {AuthService} from '../../core/store/auth/auth.service';
912

1013
describe('MainViewComponent', () => {
1114
let fixture: ComponentFixture<MainViewComponent>;
@@ -22,6 +25,18 @@ describe('MainViewComponent', () => {
2225
ReactiveFormsModule,
2326
ClarityModule.forRoot(),
2427
RouterTestingModule
28+
],
29+
providers: [
30+
BaseRequestOptions,
31+
MockBackend,
32+
{
33+
provide: Http,
34+
useFactory: function (backend: ConnectionBackend, defaultOptions: RequestOptions) {
35+
return new Http(backend, defaultOptions);
36+
},
37+
deps: [MockBackend, BaseRequestOptions]
38+
},
39+
AuthService
2540
]
2641
})
2742
.compileComponents();

src/main/typescript/app/main/main-view/main-view.component.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {Component, OnInit} from '@angular/core';
22
import {Store} from '@ngrx/store';
33
import {RootState} from '../../core/store/index';
44
import {Observable} from 'rxjs/Observable';
5+
import {AuthService} from '../../core/store/auth/auth.service';
56

67
@Component({
78
selector: 'shardis-main-view',
@@ -11,13 +12,19 @@ import {Observable} from 'rxjs/Observable';
1112
export class MainViewComponent implements OnInit {
1213

1314
title = 'shardis works!';
15+
authenticated$: Observable<boolean>;
1416
userData$: Observable<any>;
1517

16-
constructor(private store: Store<RootState>) {
18+
constructor(private store: Store<RootState>, private authService: AuthService) {
19+
this.authenticated$ = store.select(s => s.auth.authenticated);
1720
this.userData$ = store.select(s => s.auth.userData);
1821
}
1922

2023
ngOnInit() {
2124
}
2225

26+
public logout() {
27+
this.authService.logout();
28+
}
29+
2330
}

0 commit comments

Comments
 (0)