File

app/shared/auth-bar/auth-bar.component.ts

Implements

OnInit OnDestroy

Metadata

selector [auth-bar]
styleUrls ./auth-bar.component.scss
templateUrl ./auth-bar.component.html

Index

Properties
Methods
HostBindings

Constructor

constructor(auth: AuthService, labels: LabelService, keycloakService: KeycloakService, resizer: ResizeContentService, constants: ConstantsService, idGenerator: IdGeneratorService, doc)
Parameters :
Name Type Optional
auth AuthService No
labels LabelService No
keycloakService KeycloakService No
resizer ResizeContentService No
constants ConstantsService No
idGenerator IdGeneratorService No
doc No

HostBindings

class

Methods

actOnResize
actOnResize()
Returns : void
Public login
login()
Returns : boolean
Public logout
logout()
Returns : boolean
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void

Properties

Public auth
Type : AuthService
Private componentId
Default value : this.idGenerator.getId()
isDesktop
Type : boolean
Public isLoggedIn
keycloak
logInLabel
Type : string
Private logInLabelKey
Type : string
Default value : 'login'
loginUrl
logoutUrl
subscriptions
Type : []
Default value : []
Public userFirstName
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);
    }
}
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""