Skip to content

Commit df83f9a

Browse files
committed
feat(frontend): add store for authorization (basic stub)
1 parent a24eed8 commit df83f9a

File tree

9 files changed

+118
-7
lines changed

9 files changed

+118
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"e2e": "protractor",
7777
"e2e:live": "protractor --elementExplorer",
7878
"lint": "ng lint",
79+
"lint:fix": "tslint --fix \"src/main/typescript/**/*.ts\" --project src/main/typescript/tsconfig.json --type-check && tslint --fix \"e2e/**/*.ts\" --project e2e/tsconfig.json --type-check",
7980
"pretest": "rimraf coverage && yarn run lint",
8081
"test": "ng test -w false --code-coverage --progress false",
8182
"test:live": "ng test --progress false",

src/main/typescript/app/core/core.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {RouterStoreModule} from '@ngrx/router-store';
44
import {StoreDevtoolsModule} from '@ngrx/store-devtools';
55
import {StoreModule} from '@ngrx/store';
66
import {reducer} from './store/index';
7+
import {AuthService} from './store/auth/auth.service';
78

89
const store = StoreModule.provideStore(reducer);
910

@@ -14,7 +15,9 @@ const store = StoreModule.provideStore(reducer);
1415
RouterStoreModule.connectRouter(),
1516
StoreDevtoolsModule.instrumentOnlyWithExtension(),
1617
],
17-
declarations: []
18+
providers: [
19+
AuthService
20+
]
1821
})
1922
export class CoreModule {
2023
constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {Action} from '@ngrx/store';
2+
import {uniqueActionType} from '../shared/unique-action-type';
3+
4+
export const ActionTypes = {
5+
LOGOUT: uniqueActionType('[Auth] Logout'),
6+
};
7+
8+
export class LogoutAction implements Action {
9+
type = ActionTypes.LOGOUT;
10+
11+
constructor() {
12+
}
13+
}
14+
15+
16+
export type Actions
17+
= LogoutAction;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import '@ngrx/core/add/operator/select';
2+
import 'rxjs/add/operator/map';
3+
import 'rxjs/add/operator/let';
4+
import {AuthState, initialState} from './auth.state';
5+
import {Actions, ActionTypes} from './auth.actions';
6+
7+
export function authReducer(state = initialState, action: Actions): AuthState {
8+
switch (action.type) {
9+
case ActionTypes.LOGOUT:
10+
return initialState;
11+
default:
12+
return state;
13+
}
14+
}
15+
16+
17+
export const isAuthenticated = (state: AuthState) => state.authenticated;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* tslint:disable:no-unused-variable */
2+
import {TestBed, inject} from '@angular/core/testing';
3+
import {AuthService} from './auth.service';
4+
import {Http, BaseRequestOptions, ConnectionBackend, RequestOptions} from '@angular/http';
5+
import {MockBackend} from '@angular/http/testing';
6+
import {CoreModule} from '../../core.module';
7+
8+
describe('Service: Auth', () => {
9+
10+
beforeEach(() => {
11+
TestBed.configureTestingModule({
12+
imports: [
13+
CoreModule
14+
],
15+
providers: [
16+
BaseRequestOptions,
17+
MockBackend,
18+
{
19+
provide: Http,
20+
useFactory: function (backend: ConnectionBackend, defaultOptions: RequestOptions) {
21+
return new Http(backend, defaultOptions);
22+
},
23+
deps: [MockBackend, BaseRequestOptions]
24+
},
25+
AuthService
26+
]
27+
});
28+
});
29+
30+
it('should exist', inject([AuthService], (service: AuthService) => {
31+
expect(service).toBeTruthy();
32+
}));
33+
34+
35+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {Injectable} from '@angular/core';
2+
import {Http} from '@angular/http';
3+
import {Store} from '@ngrx/store';
4+
import {RootState} from '../index';
5+
import {Observable} from 'rxjs/Observable';
6+
7+
8+
@Injectable()
9+
export class AuthService {
10+
11+
private authenticated$: Observable<boolean>;
12+
13+
constructor(public http: Http, private store: Store<RootState>) {
14+
this.authenticated$ = store.select(s => s.auth.authenticated);
15+
}
16+
17+
public isAuthenticated(): Observable<boolean> {
18+
return this.authenticated$;
19+
}
20+
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export interface AuthState {
2+
authenticated: boolean;
3+
tokenExpirationDate: Date;
4+
userData: any;
5+
jwtToken: string;
6+
}
7+
8+
export const initialState: AuthState = {
9+
authenticated: false,
10+
tokenExpirationDate: null,
11+
userData: null,
12+
jwtToken: null
13+
};
14+
15+

src/main/typescript/app/core/store/counter/counter.reducer.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'rxjs/add/operator/let';
44
import * as counter from './counter.actions';
55
import {CounterState, initialCounter} from './counter.state';
66

7-
export function reducer(state = initialCounter, action: counter.Actions): CounterState {
7+
export function counterReducer(state = initialCounter, action: counter.Actions): CounterState {
88
switch (action.type) {
99
case counter.ActionTypes.INCREMENT_COUNTER:
1010
return {value: state.value + 1};
@@ -15,5 +15,3 @@ export function reducer(state = initialCounter, action: counter.Actions): Counte
1515
}
1616
}
1717

18-
19-
export const getValue = (state: CounterState) => state.value;

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
import {compose} from '@ngrx/core/compose';
22
import {storeFreeze} from 'ngrx-store-freeze';
33
import {combineReducers, ActionReducer} from '@ngrx/store';
4-
import * as fromCounter from './counter/counter.reducer';
54
import {CounterState} from './counter/counter.state';
65
import {environment} from '../../../environments/environment';
76
import {localStorageSync} from 'ngrx-store-localstorage';
87
import {RouterState, routerReducer} from '@ngrx/router-store';
8+
import {counterReducer} from './counter/counter.reducer';
9+
import {authReducer} from './auth/auth.reducer';
10+
import {AuthState} from './auth/auth.state';
911

1012

1113
export interface RootState {
14+
auth: AuthState;
1215
counter: CounterState;
1316
router: RouterState;
1417
}
1518

1619
const reducers = {
17-
counter: fromCounter.reducer,
20+
auth: authReducer,
21+
counter: counterReducer,
1822
router: routerReducer
1923
};
2024

2125
const developmentReducer: ActionReducer<RootState> = compose(
2226
storeFreeze,
23-
localStorageSync(['counter'], true),
27+
localStorageSync(['counter', 'auth'], true),
2428
combineReducers)(reducers);
2529

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

0 commit comments

Comments
 (0)