From e2a2d6c86c7ca39074fdff3b545947d1d58dc008 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 4 Mar 2017 11:45:47 +0100 Subject: [PATCH] Client: check token valitidy at startup --- client/src/app/account/account.component.ts | 7 +++---- client/src/app/account/account.module.ts | 4 +--- client/src/app/account/index.ts | 1 - client/src/app/app.component.ts | 18 +++++++++++++++--- .../{shared => core}/auth/auth-status.model.ts | 0 .../{shared => core}/auth/auth-user.model.ts | 3 ++- client/src/app/core/auth/auth.service.ts | 4 ++-- client/src/app/core/auth/index.ts | 2 ++ client/src/app/core/menu/menu.component.ts | 3 +-- client/src/app/shared/auth/index.ts | 2 -- client/src/app/shared/shared.module.ts | 4 +++- client/src/app/shared/users/index.ts | 1 + .../users/user.service.ts} | 16 ++++++++++++---- .../videos/video-list/video-list.component.ts | 4 ++-- 14 files changed, 44 insertions(+), 25 deletions(-) rename client/src/app/{shared => core}/auth/auth-status.model.ts (100%) rename client/src/app/{shared => core}/auth/auth-user.model.ts (96%) rename client/src/app/{account/account.service.ts => shared/users/user.service.ts} (57%) diff --git a/client/src/app/account/account.component.ts b/client/src/app/account/account.component.ts index 9b6b5fbf4..14452a73e 100644 --- a/client/src/app/account/account.component.ts +++ b/client/src/app/account/account.component.ts @@ -4,8 +4,7 @@ import { Router } from '@angular/router'; import { NotificationsService } from 'angular2-notifications'; -import { AccountService } from './account.service'; -import { FormReactive, USER_PASSWORD } from '../shared'; +import { FormReactive, UserService, USER_PASSWORD } from '../shared'; @Component({ selector: 'my-account', @@ -29,7 +28,7 @@ export class AccountComponent extends FormReactive implements OnInit { private formBuilder: FormBuilder, private router: Router, private notificationsService: NotificationsService, - private accountService: AccountService + private userService: UserService ) { super(); } @@ -58,7 +57,7 @@ export class AccountComponent extends FormReactive implements OnInit { return; } - this.accountService.changePassword(newPassword).subscribe( + this.userService.changePassword(newPassword).subscribe( () => this.notificationsService.success('Success', 'Password updated.'), err => this.error = err diff --git a/client/src/app/account/account.module.ts b/client/src/app/account/account.module.ts index 53f6ba58e..75f2ee6f9 100644 --- a/client/src/app/account/account.module.ts +++ b/client/src/app/account/account.module.ts @@ -19,8 +19,6 @@ import { SharedModule } from '../shared'; AccountComponent ], - providers: [ - AccountService - ] + providers: [] }) export class AccountModule { } diff --git a/client/src/app/account/index.ts b/client/src/app/account/index.ts index be03713cb..9265fa10a 100644 --- a/client/src/app/account/index.ts +++ b/client/src/app/account/index.ts @@ -1,4 +1,3 @@ export * from './account-routing.module'; export * from './account.component'; export * from './account.module'; -export * from './account.service'; diff --git a/client/src/app/app.component.ts b/client/src/app/app.component.ts index f487e6c48..47d2bfdd2 100644 --- a/client/src/app/app.component.ts +++ b/client/src/app/app.component.ts @@ -1,14 +1,17 @@ -import { Component, ViewContainerRef } from '@angular/core'; +import { Component, OnInit, ViewContainerRef } from '@angular/core'; import { Router } from '@angular/router'; import { MetaService } from 'ng2-meta'; + +import { AuthService } from './core'; +import { UserService } from './shared'; + @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: [ './app.component.scss' ] }) - -export class AppComponent { +export class AppComponent implements OnInit { notificationOptions = { timeOut: 3000, lastOnBottom: true, @@ -25,9 +28,18 @@ export class AppComponent { constructor( private router: Router, private metaService: MetaService, + private authService: AuthService, + private userService: UserService, 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() { return this.router.url.indexOf('/admin/') !== -1; } diff --git a/client/src/app/shared/auth/auth-status.model.ts b/client/src/app/core/auth/auth-status.model.ts similarity index 100% rename from client/src/app/shared/auth/auth-status.model.ts rename to client/src/app/core/auth/auth-status.model.ts diff --git a/client/src/app/shared/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.ts similarity index 96% rename from client/src/app/shared/auth/auth-user.model.ts rename to client/src/app/core/auth/auth-user.model.ts index f560351f4..5d61954d6 100644 --- a/client/src/app/shared/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts @@ -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 { private static KEYS = { diff --git a/client/src/app/core/auth/auth.service.ts b/client/src/app/core/auth/auth.service.ts index a56adbbad..2e7328197 100644 --- a/client/src/app/core/auth/auth.service.ts +++ b/client/src/app/core/auth/auth.service.ts @@ -9,9 +9,9 @@ import 'rxjs/add/observable/throw'; import { NotificationsService } from 'angular2-notifications'; +import { AuthStatus } from './auth-status.model'; +import { AuthUser } from './auth-user.model'; // 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'; @Injectable() diff --git a/client/src/app/core/auth/index.ts b/client/src/app/core/auth/index.ts index cf52c9c7c..67a18cfbb 100644 --- a/client/src/app/core/auth/index.ts +++ b/client/src/app/core/auth/index.ts @@ -1 +1,3 @@ +export * from './auth-status.model'; +export * from './auth-user.model'; export * from './auth.service' diff --git a/client/src/app/core/menu/menu.component.ts b/client/src/app/core/menu/menu.component.ts index f1bf6966d..5ca60e5e0 100644 --- a/client/src/app/core/menu/menu.component.ts +++ b/client/src/app/core/menu/menu.component.ts @@ -1,8 +1,7 @@ import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; -import { AuthService } from '../auth'; -import { AuthStatus } from '../../shared'; +import { AuthService, AuthStatus } from '../auth'; @Component({ selector: 'my-menu', diff --git a/client/src/app/shared/auth/index.ts b/client/src/app/shared/auth/index.ts index ce0bd8adf..c488aed69 100644 --- a/client/src/app/shared/auth/index.ts +++ b/client/src/app/shared/auth/index.ts @@ -1,3 +1 @@ export * from './auth-http.service'; -export * from './auth-status.model'; -export * from './auth-user.model'; diff --git a/client/src/app/shared/shared.module.ts b/client/src/app/shared/shared.module.ts index 0f57ef078..84cc86c64 100644 --- a/client/src/app/shared/shared.module.ts +++ b/client/src/app/shared/shared.module.ts @@ -16,6 +16,7 @@ import { Ng2SmartTableModule } from 'ng2-smart-table'; import { AUTH_HTTP_PROVIDERS } from './auth'; import { RestExtractor, RestService } from './rest'; import { SearchComponent, SearchService } from './search'; +import { UserService } from './users'; import { VideoAbuseService } from './video-abuse'; @NgModule({ @@ -65,7 +66,8 @@ import { VideoAbuseService } from './video-abuse'; RestExtractor, RestService, SearchService, - VideoAbuseService + VideoAbuseService, + UserService ] }) export class SharedModule { } diff --git a/client/src/app/shared/users/index.ts b/client/src/app/shared/users/index.ts index 5a670ce8f..ff009e89b 100644 --- a/client/src/app/shared/users/index.ts +++ b/client/src/app/shared/users/index.ts @@ -1 +1,2 @@ export * from './user.model'; +export * from './user.service'; diff --git a/client/src/app/account/account.service.ts b/client/src/app/shared/users/user.service.ts similarity index 57% rename from client/src/app/account/account.service.ts rename to client/src/app/shared/users/user.service.ts index 046690347..4cf100f0d 100644 --- a/client/src/app/account/account.service.ts +++ b/client/src/app/shared/users/user.service.ts @@ -2,11 +2,12 @@ import { Injectable } from '@angular/core'; import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/map'; -import { AuthService } from '../core'; -import { AuthHttp, RestExtractor } from '../shared'; +import { AuthService } from '../../core'; +import { AuthHttp } from '../auth'; +import { RestExtractor } from '../rest'; @Injectable() -export class AccountService { +export class UserService { private static BASE_USERS_URL = '/api/v1/users/'; constructor( @@ -15,8 +16,15 @@ export class AccountService { 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) { - const url = AccountService.BASE_USERS_URL + this.authService.getUser().id; + const url = UserService.BASE_USERS_URL + this.authService.getUser().id; const body = { password: newPassword }; diff --git a/client/src/app/videos/video-list/video-list.component.ts b/client/src/app/videos/video-list/video-list.component.ts index b3780f8b6..844e14567 100644 --- a/client/src/app/videos/video-list/video-list.component.ts +++ b/client/src/app/videos/video-list/video-list.component.ts @@ -9,8 +9,8 @@ import { Video, VideoService } from '../shared'; -import { AuthService } from '../../core'; -import { AuthUser, RestPagination, Search, SearchField } from '../../shared'; +import { AuthService, AuthUser } from '../../core'; +import { RestPagination, Search, SearchField } from '../../shared'; import { SearchService } from '../../shared'; @Component({ -- 2.25.1