Spaces:
Running
Running
File size: 2,118 Bytes
b39afbe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
/**
* Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
* All rights reserved.
*/
import './styles/main.scss';
import { createClient, AuthService } from 'omni-client-services';
import { OmnitoolClient } from './client';
import { loginComponent, doLogin, isLogin } from './components/Login.js';
import Alpine from 'alpinejs';
import { omnilog } from 'omni-shared';
window.Alpine = Alpine;
const client = (window.client = createClient('vite-frontend', { logger: { level: 11 } }, OmnitoolClient));
// register the authentication service
client.use(
AuthService,
{
id: 'auth',
host: '',
authUrl: '/api/v1/auth/login',
logoutUrl: '/api/v1/auth/logout',
userUrl: '/api/v1/auth/user'
},
'service'
);
document.addEventListener('alpine:init', () => {
omnilog.status_start('Alpine Init');
Alpine.store('toasts', {
counter: 0,
list: {},
isJobRunning: false,
createToast(message, jobId, type = 'info', timer = 2000) {
omnilog.info('createToast', message, jobId, type, timer);
this.list[jobId] = {
id: this.counter++,
message,
type,
visible: true
};
if (timer > 0) {
setTimeout(() => {
this.destroyToast(jobId);
}, timer);
}
},
destroyToast(jobId) {
if (this.list[jobId]) {
this.list[jobId].visible = false;
}
}
});
Alpine.data('login', () => ({
currentUser: null,
isSubmitting: false,
isLogin() {
return isLogin(this.currentUser);
},
loginComponent,
async tryLogin(payload) {
const { username, password, newUser } = payload;
try {
const user = await doLogin({ username, password, newUser });
if (user) {
this.currentUser = user;
}
} catch (e) {
if (this.isSubmitting) {
Alpine.store('toasts').createToast(e.message, 'login', 'error');
this.isSubmitting = false;
}
}
},
async init() {
// Attempt autoLogin
await this.tryLogin({ username: null, password: null, newUser: false });
}
}));
});
Alpine.start();
|