diff --git a/projects/globalnav/src/lib/globalnav.component.ts b/projects/globalnav/src/lib/globalnav.component.ts index ec24677..6bd6dc9 100644 --- a/projects/globalnav/src/lib/globalnav.component.ts +++ b/projects/globalnav/src/lib/globalnav.component.ts @@ -1,6 +1,6 @@ import { Component, Input, OnInit } from '@angular/core'; import { MediaMatcher } from '@angular/cdk/layout'; -import { NavItem, PrimaryNavItem } from './globalnav.service'; +import { Observable } from 'rxjs'; import { faBars, faLightbulb, @@ -14,6 +14,14 @@ import { faUsers } from '@fortawesome/free-solid-svg-icons'; +import { + expireTokenCookies, + NavItem, + PrimaryNavItem, + setLoginStatus, + User +} from './globalnav.service'; + @Component({ selector: 'globalnav-sidenav', templateUrl: './globalnav.component.html', @@ -22,25 +30,47 @@ import { export class GlobalnavComponent implements OnInit { @Input() mycroftUrls: any; - @Input() userName: string; + @Input() user$: Observable; public footerItems: NavItem[]; public isLoggedIn: boolean; public signInIcon = faSignInAlt; - public signOutIcon = faSignInAlt; + public signOutIcon = faSignOutAlt; public menuIcon = faBars; public mobileQuery: MediaQueryList; public navigationItems: PrimaryNavItem[]; + public userName: string; - constructor(media: MediaMatcher) { + constructor(private media: MediaMatcher) { this.mobileQuery = media.matchMedia('(max-width: 600px)'); } ngOnInit() { + this.isLoggedIn = setLoginStatus(); + this.getUser(); this.buildNavigationItems(); - this.setLoginStatus(); this.buildAccountNav(); } + getUser() { + if (this.isLoggedIn) { + this.user$.subscribe( + (user) => { + if (user.name) { + this.userName = user.name; + } else { + this.userName = 'Logged In'; + } + }, + (response) => { + if (response.status === 401) { + expireTokenCookies(); + this.isLoggedIn = setLoginStatus(); + } + } + ); + } + } + buildNavigationItems(): void { const aboutMycroftNav: PrimaryNavItem = { children: [ @@ -108,13 +138,6 @@ export class GlobalnavComponent implements OnInit { ]; } - setLoginStatus(): void { - const cookies = document.cookie; - const seleneTokenExists = cookies.includes('seleneToken'); - const seleneTokenEmpty = cookies.includes('seleneToken=""'); - this.isLoggedIn = seleneTokenExists && !seleneTokenEmpty; - } - buildAccountNav() { const accountNav: PrimaryNavItem = { children: [ diff --git a/projects/globalnav/src/lib/globalnav.module.ts b/projects/globalnav/src/lib/globalnav.module.ts index 4e922af..a47ec7f 100644 --- a/projects/globalnav/src/lib/globalnav.module.ts +++ b/projects/globalnav/src/lib/globalnav.module.ts @@ -12,7 +12,6 @@ import { MatToolbarModule } from '@angular/material/toolbar'; import { FontAwesomeModule } from '@fortawesome/angular-fontawesome'; import { GlobalnavComponent } from './globalnav.component'; -import { GlobalnavService } from './globalnav.service'; import { NavItemComponent } from './nav-item/nav-item.component'; import { PrimaryNavItemComponent } from './primary-nav-item/primary-nav-item.component'; import { FooterComponent } from './footer/footer.component'; @@ -39,8 +38,6 @@ import { FooterComponent } from './footer/footer.component'; exports: [ GlobalnavComponent ], - providers: [ - GlobalnavService - ] + providers: [] }) export class GlobalnavModule { } diff --git a/projects/globalnav/src/lib/globalnav.service.ts b/projects/globalnav/src/lib/globalnav.service.ts index 127c583..e5cf7a7 100644 --- a/projects/globalnav/src/lib/globalnav.service.ts +++ b/projects/globalnav/src/lib/globalnav.service.ts @@ -14,12 +14,25 @@ export interface PrimaryNavItem { url?: string; } - -@Injectable({ - providedIn: 'root' -}) -export class GlobalnavService { - - constructor() { - } +export interface User { + name: string; +} + +export function expireTokenCookies(): void { + const expiration = new Date(); + const cookieDomain: string = document.domain.replace('market.', ''); + + document.cookie = 'seleneToken=""' + + '; expires=' + expiration.toUTCString() + + '; domain=' + cookieDomain; + document.cookie = 'tartarusToken=""' + + '; expires=' + expiration.toUTCString() + + '; domain=' + cookieDomain; +} + +export function setLoginStatus(): boolean { + const cookies = document.cookie; + const seleneTokenExists = cookies.includes('seleneToken'); + const seleneTokenEmpty = cookies.includes('seleneToken=""'); + return seleneTokenExists && !seleneTokenEmpty; } diff --git a/projects/market/src/app/app.component.html b/projects/market/src/app/app.component.html index 6504fed..9641929 100644 --- a/projects/market/src/app/app.component.html +++ b/projects/market/src/app/app.component.html @@ -1,6 +1,6 @@