2024-01-22 12:12:44 -05:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2024-05-31 11:29:24 -07:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2024-01-22 12:12:44 -05:00
|
|
|
*/
|
|
|
|
|
|
2025-03-12 08:29:54 -07:00
|
|
|
import { service } from '@ember/service';
|
2023-11-09 13:20:20 -05:00
|
|
|
import BaseSessionService from 'ember-simple-auth/services/session';
|
2024-02-05 14:21:13 -05:00
|
|
|
import { notifyError } from 'core/decorators/notify';
|
2025-10-30 12:49:30 -07:00
|
|
|
import { tracked } from '@glimmer/tracking';
|
2023-11-09 13:20:20 -05:00
|
|
|
|
|
|
|
|
export default class SessionService extends BaseSessionService {
|
|
|
|
|
@service ipc;
|
2025-10-30 12:49:30 -07:00
|
|
|
@service store;
|
|
|
|
|
|
|
|
|
|
@tracked username;
|
2023-11-09 13:20:20 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extend ember simple auth's handleAuthentication method
|
2024-06-17 17:16:07 -04:00
|
|
|
* so we can hook in and add the user's token to the cache daemon
|
2023-11-09 13:20:20 -05:00
|
|
|
*/
|
2024-02-05 14:21:13 -05:00
|
|
|
@notifyError(({ message }) => message, { catch: true })
|
|
|
|
|
async handleAuthentication() {
|
2023-11-09 13:20:20 -05:00
|
|
|
super.handleAuthentication(...arguments);
|
|
|
|
|
|
|
|
|
|
if (this.session.isAuthenticated) {
|
|
|
|
|
const sessionData = this.data?.authenticated;
|
2024-05-28 15:24:43 -04:00
|
|
|
await this.ipc.invoke('addTokenToDaemons', {
|
2023-11-09 13:20:20 -05:00
|
|
|
tokenId: sessionData?.id,
|
|
|
|
|
token: sessionData?.token,
|
|
|
|
|
});
|
2025-10-30 12:49:30 -07:00
|
|
|
await this.loadAuthenticatedAccount();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Loads account used to authenticate so that it can be used to display
|
|
|
|
|
* the authenticated username.
|
|
|
|
|
*/
|
|
|
|
|
async loadAuthenticatedAccount() {
|
|
|
|
|
if (this.data?.authenticated?.account_id) {
|
|
|
|
|
try {
|
|
|
|
|
const account = await this.store.findRecord(
|
|
|
|
|
'account',
|
|
|
|
|
this.data.authenticated.account_id,
|
|
|
|
|
);
|
|
|
|
|
this.username = account.accountName;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// We are purposefully ignoring errors since loading the
|
|
|
|
|
// authenticated account should not block a user because
|
|
|
|
|
// account information is for populating a username.
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
2023-11-09 13:20:20 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|