Skip to content

Commit 017abc3

Browse files
committed
added AuthModule ( ! )
1 parent 3c8035a commit 017abc3

File tree

7 files changed

+101
-5
lines changed

7 files changed

+101
-5
lines changed

src/app/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {BrowserModule} from '@angular/platform-browser';
55
import {RouterModule} from '@angular/router';
66

77
import {CoreModule} from './core/core.module';
8+
import {AuthModule} from './auth/auth.module';
89

910
import {AppComponent} from './core/containers/app/app.component';
1011

@@ -17,6 +18,7 @@ import {routes} from './routes';
1718
HttpClientModule,
1819
RouterModule.forRoot(routes, { useHash: true }),
1920
CoreModule.forRoot(),
21+
AuthModule.forRoot(),
2022
],
2123
bootstrap: [AppComponent]
2224
})

src/app/auth/auth.module.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {NgModule, ModuleWithProviders} from '@angular/core';
2+
import {CommonModule} from '@angular/common';
3+
import {RouterModule} from '@angular/router';
4+
import {ReactiveFormsModule} from '@angular/forms';
5+
6+
import {LoginPageComponent} from './containers/login-page/login-page.component';
7+
8+
import {AuthService} from './services/auth.service';
9+
import {AuthGuard} from './services/auth-guard.service';
10+
11+
export const COMPONENTS = [
12+
LoginPageComponent,
13+
];
14+
15+
@NgModule({
16+
imports: [
17+
CommonModule,
18+
ReactiveFormsModule
19+
],
20+
declarations: COMPONENTS,
21+
exports: COMPONENTS,
22+
})
23+
export class AuthModule {
24+
static forRoot():ModuleWithProviders {
25+
return {
26+
ngModule: RootAuthModule,
27+
providers: [AuthService, AuthGuard],
28+
};
29+
}
30+
}
31+
32+
@NgModule({
33+
imports: [
34+
AuthModule,
35+
RouterModule.forChild([{path: 'login', component: LoginPageComponent}]),
36+
],
37+
})
38+
export class RootAuthModule {
39+
}

src/app/auth/models/user.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export interface Authenticate {
2+
username: string;
3+
password: string;
4+
}
5+
6+
export interface User {
7+
name: string;
8+
}
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 {CanActivate, Router} from '@angular/router';
3+
import {of} from 'rxjs/observable/of';
4+
import {Observable} from 'rxjs/Observable';
5+
6+
import {AuthService} from './auth.service';
7+
8+
@Injectable()
9+
export class AuthGuard implements CanActivate {
10+
constructor(private router:Router,
11+
private authService:AuthService) {
12+
}
13+
14+
canActivate():Observable<boolean> {
15+
if (this.authService.authorized) {
16+
return of(true)
17+
} else {
18+
this.router.navigate(['/login']);
19+
}
20+
}
21+
}

src/app/auth/services/auth.service.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {Injectable} from '@angular/core';
2+
import {of} from 'rxjs/observable/of';
3+
import {_throw} from 'rxjs/observable/throw';
4+
import {User, Authenticate} from '../models/user';
5+
6+
@Injectable()
7+
export class AuthService {
8+
public authorized = true;
9+
10+
constructor() {
11+
}
12+
13+
login({username, password}: Authenticate) {
14+
/**
15+
* Simulate a failed login to display the error
16+
* message for the login form.
17+
*/
18+
if (username !== 'test') {
19+
return _throw('Invalid username or password');
20+
}
21+
22+
return of({name: 'User'});
23+
}
24+
25+
logout() {
26+
return of(true);
27+
}
28+
}

src/app/main-wall/main-wall.module.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import {OfferResponsePageComponent} from './containers/offer-response-page/offer
66
import {MainWallPageComponent} from './containers/main-wall-page/main-wall-page.component';
77
import {AddOfferPageComponent} from './containers/add-offer-page/add-offer-page.component';
88

9-
10-
11-
129
export const COMPONENTS = [
1310
OfferResponsePageComponent,
1411
MainWallPageComponent,

src/app/routes.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import { Routes } from '@angular/router';
22
import {NotFoundPageComponent} from './core/containers/not-found-page/not-found-page.component';
3+
import {AuthGuard} from './auth/services/auth-guard.service';
34

45
export const routes: Routes = [
56
{ path: '', redirectTo: '/main-wall', pathMatch: 'full' },
67
{
78
path: 'main-wall',
89
loadChildren: './main-wall/main-wall.module#MainWallModule',
9-
// canActivate: [AuthGuard],
10+
canActivate: [AuthGuard],
1011
},
1112
{
1213
path: 'user-panel',
1314
loadChildren: './user-panel/user-panel.module#UserPanelModule',
14-
// canActivate: [AuthGuard],
15+
canActivate: [AuthGuard],
1516
},
1617
{ path: '**', component: NotFoundPageComponent },
1718
];

0 commit comments

Comments
 (0)