app/shared/auth-bar/auth-bar.component.ts
selector | [auth-bar] |
styleUrls | ./auth-bar.component.scss |
templateUrl | ./auth-bar.component.html |
Properties |
|
Methods |
|
HostBindings |
constructor(auth: AuthService, labels: LabelService, keycloakService: KeycloakService, resizer: ResizeContentService, constants: ConstantsService, idGenerator: IdGeneratorService, doc)
|
||||||||||||||||||||||||
Defined in app/shared/auth-bar/auth-bar.component.ts:33
|
||||||||||||||||||||||||
Parameters :
|
class |
Defined in app/shared/auth-bar/auth-bar.component.ts:33
|
actOnResize |
actOnResize()
|
Defined in app/shared/auth-bar/auth-bar.component.ts:95
|
Returns :
void
|
Public login |
login()
|
Defined in app/shared/auth-bar/auth-bar.component.ts:78
|
Returns :
boolean
|
Public logout |
logout()
|
Defined in app/shared/auth-bar/auth-bar.component.ts:84
|
Returns :
boolean
|
ngOnDestroy |
ngOnDestroy()
|
Defined in app/shared/auth-bar/auth-bar.component.ts:70
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Defined in app/shared/auth-bar/auth-bar.component.ts:45
|
Returns :
void
|
Public auth |
Type : AuthService
|
Defined in app/shared/auth-bar/auth-bar.component.ts:36
|
Private componentId |
Default value : this.idGenerator.getId()
|
Defined in app/shared/auth-bar/auth-bar.component.ts:31
|
isDesktop |
Type : boolean
|
Defined in app/shared/auth-bar/auth-bar.component.ts:26
|
Public isLoggedIn |
Defined in app/shared/auth-bar/auth-bar.component.ts:23
|
keycloak |
Defined in app/shared/auth-bar/auth-bar.component.ts:18
|
logInLabel |
Type : string
|
Defined in app/shared/auth-bar/auth-bar.component.ts:29
|
Private logInLabelKey |
Type : string
|
Default value : 'login'
|
Defined in app/shared/auth-bar/auth-bar.component.ts:28
|
loginUrl |
Defined in app/shared/auth-bar/auth-bar.component.ts:19
|
logoutUrl |
Defined in app/shared/auth-bar/auth-bar.component.ts:20
|
subscriptions |
Type : []
|
Default value : []
|
Defined in app/shared/auth-bar/auth-bar.component.ts:24
|
Public userFirstName |
Defined in app/shared/auth-bar/auth-bar.component.ts:22
|
import { Component, OnInit, OnDestroy, Inject, HostBinding } from '@angular/core';
import { LabelService } from 'src/app/core/services/label-service/label-service.service';
import { AuthService } from 'src/app/core/services/auth.service';
import { KeycloakService } from 'keycloak-angular';
import { ResizeContentService } from 'src/app/core/services/resize-service/resize-content.service';
import { ConstantsService } from '../constants/constants.service';
import { DOCUMENT } from '@angular/common';
import { IdGeneratorService } from 'src/app/core/services/id-generator.service';
import { AppConfig } from '../../app.config';
@Component({
selector: '[auth-bar]',
templateUrl: './auth-bar.component.html',
styleUrls: ['./auth-bar.component.scss']
})
export class AuthBarComponent implements OnInit, OnDestroy {
keycloak;
loginUrl;
logoutUrl;
// Login user details
public userFirstName;
public isLoggedIn;
subscriptions = [];
isDesktop: boolean;
private logInLabelKey = 'login';
logInLabel: string;
private componentId = this.idGenerator.getId();
@HostBinding('class') get class() { return 'navbar navbar-tools bg-off-white'; }
constructor(
public auth: AuthService,
private labels: LabelService,
private keycloakService: KeycloakService,
private resizer: ResizeContentService,
private constants: ConstantsService,
private idGenerator: IdGeneratorService,
@Inject(DOCUMENT) private doc
) { }
ngOnInit() {
this.resizer.registerCallback(this.componentId, () => this.actOnResize());
this.actOnResize();
this.labels.getLabel(this.logInLabelKey).then(label => this.logInLabel = label);
this.subscriptions.push(this.auth.keycloak.subscribe(x => {
this.keycloak = x;
this.loginUrl = this.keycloak.createLoginUrl();
this.logoutUrl = this.keycloak.createLogoutUrl();
}));
this.keycloakService.isLoggedIn().then(res => {
this.isLoggedIn = res;
if (this.isLoggedIn) {
this.keycloakService.loadUserProfile(false).then(data => {
this.userFirstName = `${data['firstName']}`;
});
document.cookie = `SKFCOMLOGGEDIN=yes; domain=${AppConfig.settings.cookiesConfig.domain}; path=${AppConfig.settings.cookiesConfig.path}`;
}
});
}
ngOnDestroy() {
this.subscriptions.forEach(x => {
if (x) { x.unsubscribe(); }
});
this.resizer.destroyCallback(this.componentId);
}
public login() {
this.keycloakService.login();
return false;
}
public logout() {
this.userFirstName = '';
this.keycloakService.logout();
if (document.cookie.includes('SKFCOMLOGGEDIN=')) {
document.cookie = `SKFCOMLOGGEDIN=no; domain=${AppConfig.settings.cookiesConfig.domain}; path=${AppConfig.settings.cookiesConfig.path}`;
}
return false;
}
actOnResize(): void {
const width = (window.innerWidth || this.doc.documentElement.clientWidth);
this.isDesktop = width >= this.constants.BOOTSTRAP_BREAKPOINTS.LG;
}
}
<div [ngClass]="{'container': isDesktop, 'container-fluid': !isDesktop}">
<ul class="navbar-nav">
<li class="nav-item">
<a href="#" class="nav-link"><i class="fas fa-globe-americas pr-1"></i> LOCATION</a>
</li>
<li class="nav-item">
<a href="#" *ngIf="!isLoggedIn" (click)="login()" class="loginlogout nav-link"><i class="fas fa-user pr-1"></i> {{logInLabel}}</a>
<div login-dropdown *ngIf="isLoggedIn" [userName]="userFirstName" (loggingOut)="logout()"></div>
</li>
</ul>
</div>
./auth-bar.component.scss
@import "src/app/styles/helpers";
:host {
&.bg-off-white .container,
&.bg-off-white .container-fluid {
justify-content: flex-end;
@include font-scale(14, light);
text-transform: uppercase;
letter-spacing: calc-rem(0.4);
}
}
.container-fluid {
padding-left: 0;
padding-right: 0;
}
.navbar-nav {
flex-direction: row;
.nav-item {
margin-right: calc-rem(10);
&:last-child {
margin-right: 0;
}
}
.nav-link {
padding: calc-rem(6) calc-rem(12);
}
}