Skip to content

Commit

Permalink
Update user which updates tokens with google (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
garyluu authored Jul 27, 2018
1 parent 71fe6b2 commit 9fabe39
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 38 deletions.
12 changes: 1 addition & 11 deletions src/app/loginComponents/accounts/external/accounts.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 }
]
});
});
Expand Down
20 changes: 11 additions & 9 deletions src/app/loginComponents/accounts/external/accounts.service.ts
Original file line number Diff line number Diff line change
@@ -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, '');
Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 11 additions & 7 deletions src/app/loginComponents/auth/auth.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
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;
let fixture: ComponentFixture<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(() => {
Expand Down
7 changes: 3 additions & 4 deletions src/app/loginComponents/auth/auth.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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) { }

Expand Down Expand Up @@ -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 }`]);
Expand Down
2 changes: 1 addition & 1 deletion src/app/loginComponents/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
2 changes: 2 additions & 0 deletions src/app/test/service-stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ export class LogoutStubService {
export class UserStubService {
userId$ = observableOf(5);
user$ = observableOf({});
updateUser() {
}
}

export class TokenStubService {
Expand Down

0 comments on commit 9fabe39

Please sign in to comment.