Files
threshold-ui/ui/admin/app/services/session.js
T

56 lines
1.7 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
2024-02-05 15:59:58 -05:00
import BaseSessionService from 'ember-simple-auth/services/session';
import { service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
2025-09-02 16:31:49 -04:00
import { formatDbName } from 'api/services/sqlite';
2024-02-05 15:59:58 -05:00
export default class SessionService extends BaseSessionService {
2025-08-06 11:09:14 -04:00
@service sqlite;
2024-02-05 15:59:58 -05:00
@service('browser/window') window;
@service store;
@tracked username;
2024-02-05 15:59:58 -05:00
/**
* Extend ember simple auth's handleAuthentication method
* so we can hook in and setup the DB after a successful authentication
*/
async handleAuthentication() {
2024-02-05 15:59:58 -05:00
const userId = this.data?.authenticated?.user_id;
const hostUrl = this.window.location?.host;
2024-02-05 15:59:58 -05:00
if (userId && hostUrl) {
2025-08-06 11:09:14 -04:00
await this.sqlite.setup(formatDbName(userId, hostUrl));
2024-02-05 15:59:58 -05:00
}
await this.loadAuthenticatedAccount();
// We let ember-simple-auth handle transitioning back to the index after authentication.
// This route can be configured in our environment config.
super.handleAuthentication(...arguments);
2024-02-05 15:59:58 -05:00
}
/**
* 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);
}
}
}
2024-02-05 15:59:58 -05:00
}