Client: check token valitidy at startup

pull/61/head
Chocobozzz 2017-03-04 11:45:47 +01:00
parent a7449e74f9
commit e2a2d6c86c
14 changed files with 44 additions and 25 deletions

View File

@ -4,8 +4,7 @@ import { Router } from '@angular/router';
import { NotificationsService } from 'angular2-notifications'; import { NotificationsService } from 'angular2-notifications';
import { AccountService } from './account.service'; import { FormReactive, UserService, USER_PASSWORD } from '../shared';
import { FormReactive, USER_PASSWORD } from '../shared';
@Component({ @Component({
selector: 'my-account', selector: 'my-account',
@ -29,7 +28,7 @@ export class AccountComponent extends FormReactive implements OnInit {
private formBuilder: FormBuilder, private formBuilder: FormBuilder,
private router: Router, private router: Router,
private notificationsService: NotificationsService, private notificationsService: NotificationsService,
private accountService: AccountService private userService: UserService
) { ) {
super(); super();
} }
@ -58,7 +57,7 @@ export class AccountComponent extends FormReactive implements OnInit {
return; return;
} }
this.accountService.changePassword(newPassword).subscribe( this.userService.changePassword(newPassword).subscribe(
() => this.notificationsService.success('Success', 'Password updated.'), () => this.notificationsService.success('Success', 'Password updated.'),
err => this.error = err err => this.error = err

View File

@ -19,8 +19,6 @@ import { SharedModule } from '../shared';
AccountComponent AccountComponent
], ],
providers: [ providers: []
AccountService
]
}) })
export class AccountModule { } export class AccountModule { }

View File

@ -1,4 +1,3 @@
export * from './account-routing.module'; export * from './account-routing.module';
export * from './account.component'; export * from './account.component';
export * from './account.module'; export * from './account.module';
export * from './account.service';

View File

@ -1,14 +1,17 @@
import { Component, ViewContainerRef } from '@angular/core'; import { Component, OnInit, ViewContainerRef } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { MetaService } from 'ng2-meta'; import { MetaService } from 'ng2-meta';
import { AuthService } from './core';
import { UserService } from './shared';
@Component({ @Component({
selector: 'my-app', selector: 'my-app',
templateUrl: './app.component.html', templateUrl: './app.component.html',
styleUrls: [ './app.component.scss' ] styleUrls: [ './app.component.scss' ]
}) })
export class AppComponent implements OnInit {
export class AppComponent {
notificationOptions = { notificationOptions = {
timeOut: 3000, timeOut: 3000,
lastOnBottom: true, lastOnBottom: true,
@ -25,9 +28,18 @@ export class AppComponent {
constructor( constructor(
private router: Router, private router: Router,
private metaService: MetaService, private metaService: MetaService,
private authService: AuthService,
private userService: UserService,
viewContainerRef: ViewContainerRef viewContainerRef: ViewContainerRef
) {} ) {}
ngOnInit() {
if (this.authService.isLoggedIn()) {
// The service will automatically redirect to the login page if the token is not valid anymore
this.userService.checkTokenValidity();
}
}
isInAdmin() { isInAdmin() {
return this.router.url.indexOf('/admin/') !== -1; return this.router.url.indexOf('/admin/') !== -1;
} }

View File

@ -1,4 +1,5 @@
import { User } from '../users'; // Do not use the barrel (dependency loop)
import { User } from '../../shared/users/user.model';
export class AuthUser extends User { export class AuthUser extends User {
private static KEYS = { private static KEYS = {

View File

@ -9,9 +9,9 @@ import 'rxjs/add/observable/throw';
import { NotificationsService } from 'angular2-notifications'; import { NotificationsService } from 'angular2-notifications';
import { AuthStatus } from './auth-status.model';
import { AuthUser } from './auth-user.model';
// Do not use the barrel (dependency loop) // Do not use the barrel (dependency loop)
import { AuthStatus } from '../../shared/auth/auth-status.model';
import { AuthUser } from '../../shared/auth/auth-user.model';
import { RestExtractor } from '../../shared/rest'; import { RestExtractor } from '../../shared/rest';
@Injectable() @Injectable()

View File

@ -1 +1,3 @@
export * from './auth-status.model';
export * from './auth-user.model';
export * from './auth.service' export * from './auth.service'

View File

@ -1,8 +1,7 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { AuthService } from '../auth'; import { AuthService, AuthStatus } from '../auth';
import { AuthStatus } from '../../shared';
@Component({ @Component({
selector: 'my-menu', selector: 'my-menu',

View File

@ -1,3 +1 @@
export * from './auth-http.service'; export * from './auth-http.service';
export * from './auth-status.model';
export * from './auth-user.model';

View File

@ -16,6 +16,7 @@ import { Ng2SmartTableModule } from 'ng2-smart-table';
import { AUTH_HTTP_PROVIDERS } from './auth'; import { AUTH_HTTP_PROVIDERS } from './auth';
import { RestExtractor, RestService } from './rest'; import { RestExtractor, RestService } from './rest';
import { SearchComponent, SearchService } from './search'; import { SearchComponent, SearchService } from './search';
import { UserService } from './users';
import { VideoAbuseService } from './video-abuse'; import { VideoAbuseService } from './video-abuse';
@NgModule({ @NgModule({
@ -65,7 +66,8 @@ import { VideoAbuseService } from './video-abuse';
RestExtractor, RestExtractor,
RestService, RestService,
SearchService, SearchService,
VideoAbuseService VideoAbuseService,
UserService
] ]
}) })
export class SharedModule { } export class SharedModule { }

View File

@ -1 +1,2 @@
export * from './user.model'; export * from './user.model';
export * from './user.service';

View File

@ -2,11 +2,12 @@ import { Injectable } from '@angular/core';
import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map'; import 'rxjs/add/operator/map';
import { AuthService } from '../core'; import { AuthService } from '../../core';
import { AuthHttp, RestExtractor } from '../shared'; import { AuthHttp } from '../auth';
import { RestExtractor } from '../rest';
@Injectable() @Injectable()
export class AccountService { export class UserService {
private static BASE_USERS_URL = '/api/v1/users/'; private static BASE_USERS_URL = '/api/v1/users/';
constructor( constructor(
@ -15,8 +16,15 @@ export class AccountService {
private restExtractor: RestExtractor private restExtractor: RestExtractor
) {} ) {}
checkTokenValidity() {
const url = UserService.BASE_USERS_URL + 'me';
// AuthHttp will redirect us to the login page if the oken is not valid anymore
this.authHttp.get(url).subscribe(() => { ; });
}
changePassword(newPassword: string) { changePassword(newPassword: string) {
const url = AccountService.BASE_USERS_URL + this.authService.getUser().id; const url = UserService.BASE_USERS_URL + this.authService.getUser().id;
const body = { const body = {
password: newPassword password: newPassword
}; };

View File

@ -9,8 +9,8 @@ import {
Video, Video,
VideoService VideoService
} from '../shared'; } from '../shared';
import { AuthService } from '../../core'; import { AuthService, AuthUser } from '../../core';
import { AuthUser, RestPagination, Search, SearchField } from '../../shared'; import { RestPagination, Search, SearchField } from '../../shared';
import { SearchService } from '../../shared'; import { SearchService } from '../../shared';
@Component({ @Component({