From 9fabe39b2fb535d54561af295ea2b6d0f7011382 Mon Sep 17 00:00:00 2001 From: Gary Luu Date: Fri, 27 Jul 2018 10:52:27 -0400 Subject: [PATCH] Update user which updates tokens with google (#364) --- .../accounts/external/accounts.component.ts | 12 +---------- .../external/accounts.service.spec.ts | 13 ++++++------ .../accounts/external/accounts.service.ts | 20 ++++++++++--------- .../auth/auth.component.spec.ts | 18 ++++++++++------- .../loginComponents/auth/auth.component.ts | 7 +++---- src/app/loginComponents/token.service.ts | 2 +- src/app/test/service-stubs.ts | 2 ++ 7 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/app/loginComponents/accounts/external/accounts.component.ts b/src/app/loginComponents/accounts/external/accounts.component.ts index eca6484efa..39eb0d75a8 100644 --- a/src/app/loginComponents/accounts/external/accounts.component.ts +++ b/src/app/loginComponents/accounts/external/accounts.component.ts @@ -99,15 +99,6 @@ export class AccountsExternalComponent implements OnInit, OnDestroy { } } - // Unlink account in accountsInfo - private unlinkToken(source) { - for (const account of this.accountsInfo) { - if (account.source === source) { - account.isLinked = false; - } - } - } - link(source: string): void { this.accountsService.link(source); } @@ -116,8 +107,7 @@ export class AccountsExternalComponent implements OnInit, OnDestroy { unlink(source: string) { this.deleteToken(source).pipe( first()).subscribe(() => { - this.tokenService.updateTokens(); - this.unlinkToken(source); + this.userService.updateUser(); }); } diff --git a/src/app/loginComponents/accounts/external/accounts.service.spec.ts b/src/app/loginComponents/accounts/external/accounts.service.spec.ts index 8aff90ddc3..f5d071af19 100644 --- a/src/app/loginComponents/accounts/external/accounts.service.spec.ts +++ b/src/app/loginComponents/accounts/external/accounts.service.spec.ts @@ -1,17 +1,18 @@ -/* tslint:disable:no-unused-variable */ +import { inject, TestBed } from '@angular/core/testing'; -import { TestBed, async, inject } from '@angular/core/testing'; -import { AccountsService } from './accounts.service'; import { LoginService } from '../../../login/login.service'; -import { LoginStubService, TokenStubService } from '../../../test/service-stubs'; +import { LoginStubService, TokenStubService, UserStubService } from '../../../test/service-stubs'; import { TokenService } from '../../token.service'; +import { UserService } from '../../user.service'; +import { AccountsService } from './accounts.service'; describe('Service: Accounts', () => { beforeEach(() => { TestBed.configureTestingModule({ providers: [AccountsService, - { provide: LoginService, useClass: LoginStubService}, - {provide: TokenService, useClass: TokenStubService} + { provide: LoginService, useClass: LoginStubService }, + { provide: TokenService, useClass: TokenStubService }, + { provide: UserService, useClass: UserStubService } ] }); }); diff --git a/src/app/loginComponents/accounts/external/accounts.service.ts b/src/app/loginComponents/accounts/external/accounts.service.ts index e95758e9d0..01ed4a7e14 100644 --- a/src/app/loginComponents/accounts/external/accounts.service.ts +++ b/src/app/loginComponents/accounts/external/accounts.service.ts @@ -1,14 +1,16 @@ -import {first} from 'rxjs/operators'; -import {Links} from './links.model'; -import {TokenSource} from './../../../shared/enum/token-source.enum'; -import {Injectable} from '@angular/core'; -import {LoginService} from '../../../login/login.service'; -import {TokenService} from '../../token.service'; +import { Injectable } from '@angular/core'; +import { first } from 'rxjs/operators'; + +import { LoginService } from '../../../login/login.service'; +import { TokenService } from '../../token.service'; +import { UserService } from '../../user.service'; +import { TokenSource } from './../../../shared/enum/token-source.enum'; +import { Links } from './links.model'; @Injectable() export class AccountsService { - constructor(private loginService: LoginService, private tokenService: TokenService) { } + constructor(private loginService: LoginService, private tokenService: TokenService, private userService: UserService) { } private stripSpace(url: string): string { return url.replace(/\s/g, ''); @@ -43,8 +45,8 @@ export class AccountsService { }, error => { // TODO: Hook up to snackbar }, () => { - // Always refresh tokens - this.tokenService.updateTokens(); + // Also update user to get the new profile, which causes the token service to trigger and update the tokens too + this.userService.updateUser(); }); break; } diff --git a/src/app/loginComponents/auth/auth.component.spec.ts b/src/app/loginComponents/auth/auth.component.spec.ts index 53c40e093b..3c4fb9c704 100644 --- a/src/app/loginComponents/auth/auth.component.spec.ts +++ b/src/app/loginComponents/auth/auth.component.spec.ts @@ -1,9 +1,10 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; -import { TokensStubService, TokenStubService } from './../../test/service-stubs'; + import { TokenService } from '../token.service'; +import { UserService } from '../user.service'; +import { TokenStubService, UserStubService } from './../../test/service-stubs'; import { AuthComponent } from './auth.component'; -import { TestBed, ComponentFixture, async } from '@angular/core/testing'; - describe('AuthComponent', () => { let component: AuthComponent; @@ -11,11 +12,14 @@ describe('AuthComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ - declarations: [ AuthComponent], - imports: [ RouterTestingModule.withRoutes([{path: '**', component: AuthComponent}]) ], - providers: [{provide: TokenService, useClass: TokenStubService}] + declarations: [AuthComponent], + imports: [RouterTestingModule.withRoutes([{ path: '**', component: AuthComponent }])], + providers: [ + { provide: TokenService, useClass: TokenStubService }, + { provide: UserService, useClass: UserStubService } + ] }) - .compileComponents(); + .compileComponents(); })); beforeEach(() => { diff --git a/src/app/loginComponents/auth/auth.component.ts b/src/app/loginComponents/auth/auth.component.ts index 962204fbcf..4f591ba10b 100644 --- a/src/app/loginComponents/auth/auth.component.ts +++ b/src/app/loginComponents/auth/auth.component.ts @@ -6,6 +6,7 @@ import { Provider } from '../../shared/enum/provider.enum'; import { Component, OnInit, OnDestroy } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { TokenService } from '../token.service'; +import { UserService } from '../user.service'; @Component({ selector: 'app-auth', @@ -15,7 +16,7 @@ export class AuthComponent implements OnInit, OnDestroy { tokenSubscription: ISubscription; - constructor(private tokenService: TokenService, + constructor(private tokenService: TokenService, private userService: UserService, private activatedRoute: ActivatedRoute, private router: Router) { } @@ -69,9 +70,7 @@ export class AuthComponent implements OnInit, OnDestroy { const prevPage = localStorage.getItem('page'); this.tokenSubscription = this.addToken().subscribe(token => { - if (token) { - this.tokenService.updateTokens(); - } + this.userService.updateUser(); this.router.navigate([`${ prevPage }`]); }, error => { this.router.navigate([`${ prevPage }`]); diff --git a/src/app/loginComponents/token.service.ts b/src/app/loginComponents/token.service.ts index 125b66f4b7..2e91595cf2 100644 --- a/src/app/loginComponents/token.service.ts +++ b/src/app/loginComponents/token.service.ts @@ -66,7 +66,7 @@ export class TokenService { this.tokens$.next(tokens); } - updateTokens() { + private updateTokens() { this.usersService.getUserTokens(this.user.id).subscribe(token => this.setTokens(token)); } diff --git a/src/app/test/service-stubs.ts b/src/app/test/service-stubs.ts index 20abcf2cdb..2ebdcec103 100644 --- a/src/app/test/service-stubs.ts +++ b/src/app/test/service-stubs.ts @@ -413,6 +413,8 @@ export class LogoutStubService { export class UserStubService { userId$ = observableOf(5); user$ = observableOf({}); + updateUser() { + } } export class TokenStubService {