private authChannel: any;
- private activeUser: EgAuthUser;
-
- // opChangeUser refers to the user that has been superseded during
- // an op-change event. opChangeUser resumes its status as the
- // activeUser once the op-change cycle has completed.
- private opChangeUser: EgAuthUser = null;
+ private activeUser: EgAuthUser = null;
workstationState: EgAuthWsState = EgAuthWsState.PENDING;
// Returns true if we are currently in op-change mode.
opChangeIsActive(): boolean {
- return this.opChangeUser !== null;
+ return Boolean(this.store.getLoginSessionItem('eg.auth.time.oc'));
}
// - Accessor functions always refer to the active user.
};
// NOTE: EgNetService emits an event if the auth session has expired.
+ // This only rejects when no authtoken is found.
testAuthToken(): Promise<any> {
- this.activeUser = new EgAuthUser(
- this.store.getLoginSessionItem('eg.auth.token'),
- this.store.getLoginSessionItem('eg.auth.time')
- );
+ if (!this.activeUser) {
+ // Only necessary on new page loads. During op-change,
+ // for example, we already have an activeUser.
+ this.activeUser = new EgAuthUser(
+ this.store.getLoginSessionItem('eg.auth.token'),
+ this.store.getLoginSessionItem('eg.auth.time')
+ );
+ }
if (!this.token()) return Promise.reject('no authtoken');
switch (evt.textcode) {
case 'SUCCESS':
- this.handleLoginOk(args, evt, isOpChange);
- return Promise.resolve();
+ return this.handleLoginOk(args, evt, isOpChange);
case 'WORKSTATION_NOT_FOUND':
console.error(`No such workstation "${args.workstation}"`);
}
// Stash the login data
- handleLoginOk(args: EgAuthLoginArgs, evt: EgEvent, isOpChange: boolean): void {
+ handleLoginOk(args: EgAuthLoginArgs, evt: EgEvent, isOpChange: boolean): Promise<void> {
if (isOpChange) {
this.store.setLoginSessionItem('eg.auth.token.oc', this.token());
this.store.setLoginSessionItem('eg.auth.time.oc', this.authtime());
- this.opChangeUser = this.activeUser;
}
this.activeUser = new EgAuthUser(
this.store.setLoginSessionItem('eg.auth.token', this.token());
this.store.setLoginSessionItem('eg.auth.time', this.authtime());
+
+ return Promise.resolve();
}
undoOpChange(): Promise<any> {
- if (this.opChangeUser) {
+ if (this.opChangeIsActive()) {
this.deleteSession();
- this.activeUser = this.opChangeUser;
- this.opChangeUser = null;
+ this.activeUser = new EgAuthUser(
+ this.store.getLoginSessionItem('eg.auth.token.oc'),
+ this.store.getLoginSessionItem('eg.auth.time.oc'),
+ this.activeUser.workstation
+ );
this.store.removeLoginSessionItem('eg.auth.token.oc');
this.store.removeLoginSessionItem('eg.auth.time.oc');
this.store.setLoginSessionItem('eg.auth.token', this.token());
this.store.setLoginSessionItem('eg.auth.time', this.authtime());
}
+ // Re-fetch the user.
return this.testAuthToken();
}
// Remove/reset session data
cleanup(): void {
this.activeUser = null;
- this.opChangeUser = null;
if (this.pollTimeout) {
clearTimeout(this.pollTimeout);
this.pollTimeout = null;
}
ngOnInit() {
- this.strings.register({
- key: this.key,
- resolver: (ctx:any) => this.current(ctx)
- });
+ // No key means it's an unregistered (likely static) string
+ // that does not need interpolation.
+ if (this.key) {
+ this.strings.register({
+ key: this.key,
+ resolver: (ctx:any) => this.current(ctx)
+ });
+ }
}
this.strings[assn.key] = assn;
}
- interpolate(key: string, ctx: any): Promise<string> {
+ interpolate(key: string, ctx?: any): Promise<string> {
if (!this.strings[key])
return Promise.reject('No Such String');
return this.strings[key].resolver(ctx);
<div class="navbar-nav mr-auto"></div>
- <div class="navbar-nav" *ngIf="user">
- <span i18n>{{user}} @ {{workstation}}</span>
+ <div class="navbar-nav" *ngIf="user()">
+ <span i18n>{{user()}} @ {{workstation()}}</span>
</div>
- <div class="navbar-nav" *ngIf="user">
+ <div class="navbar-nav" *ngIf="user()">
<div ngbDropdown class="nav-item dropdown" placement="bottom-right">
<a ngbDropdownToggle i18n
i18n-title
<i class="material-icons">list</i>
</a>
<div class="dropdown-menu" ngbDropdownMenu>
- <eg-op-change #navOpChange></eg-op-change>
+ <eg-op-change #navOpChange
+ i18n-failMessage
+ i18n-successMessage
+ failMessage="Operator Change Failed"
+ successMessage="Operator Change Succeeded">
+ </eg-op-change>
<a class="dropdown-item" *ngIf="!opChangeActive()"
(click)="navOpChange.open()">
<span class="material-icons">transform</span>
<span i18n>Change Operator</span>
</a>
<a *ngIf="opChangeActive()" class="dropdown-item"
- (click)="restoreOperator()">
+ (click)="navOpChange.restore()">
<span class="material-icons">transform</span>
<span i18n>Restore Operator</span>
</a>
-import {Component, OnInit} from '@angular/core';
+import {Component, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {EgAuthService} from '@eg/core/auth.service';
export class EgStaffNavComponent implements OnInit {
- user: string;
- workstation: string;
-
constructor(
private router: Router,
private auth: EgAuthService
) {}
ngOnInit() {
- if (this.auth.user()) {
- this.user = this.auth.user().usrname();
- this.workstation = this.auth.workstation();
- }
}
- opChangeActive(): boolean {
- return this.auth.opChangeIsActive();
+ user() {
+ return this.auth.user() ? this.auth.user().usrname() : '';
}
- restoreOperator() {
- this.auth.undoOpChange().then(
- ok => {
- console.log('OP change undo succeeded');
- },
-
- err => {
- console.warn('OP change undo auth expired');
- this.router.navigate(['/staff/login']);
- }
- );
+ workstation() {
+ return this.auth.user() ? this.auth.workstation() : '';
+ }
+
+ opChangeActive(): boolean {
+ return this.auth.opChangeIsActive();
}
// Broadcast to all tabs that we're logging out.
import {Component, OnInit, Input, Renderer} from '@angular/core';
+import {EgToastService} from '@eg/share/toast/toast.service';
import {EgAuthService} from '@eg/core/auth.service';
import {EgDialogComponent} from '@eg/share/dialog/dialog.component';
import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
@Input() password: string;
@Input() loginType: string = 'temp';
+ @Input() successMessage: string;
+ @Input() failMessage: string;
+
constructor(
private modal: NgbModal, // required for passing to parent
private renderer: Renderer,
+ private toast: EgToastService,
private auth: EgAuthService) {
super(modal);
}
this.login();
}
- login() {
- if (!(this.username && this.password)) return;
+ login(): Promise<any> {
+ if (!(this.username && this.password))
+ return Promise.reject('Missing Params');
- this.auth.login(
+ return this.auth.login(
{ username : this.username,
password : this.password,
workstation : this.auth.workstation(),
}, true // isOpChange
).then(
ok => {
- console.log('OP change succeeded'),
- this.close('login OK');
+ this.password = '';
+ this.username = '';
+
+ // Fetch the user object
+ this.auth.testAuthToken().then(
+ ok => {
+ this.close();
+ this.toast.success(this.successMessage);
+ }
+ );
},
notOk => {
- console.warn('OP change failed')
+ this.password = '';
+ this.toast.danger(this.failMessage);
}
);
}
+
+ restore(): Promise<any> {
+ return this.auth.undoOpChange().then(
+ ok => this.toast.success(this.successMessage),
+ err => this.toast.danger(this.failMessage)
+ );
+ }
}