dbgate/packages/web/src/LoginPage.svelte

323 lines
10 KiB
Svelte
Raw Normal View History

2022-11-25 12:36:18 +00:00
<script lang="ts">
import { onMount } from 'svelte';
2022-11-26 10:21:37 +00:00
import { internalRedirectTo } from './clientAuth';
2022-11-25 12:36:18 +00:00
import FormPasswordField from './forms/FormPasswordField.svelte';
import FormSubmit from './forms/FormSubmit.svelte';
import FormTextField from './forms/FormTextField.svelte';
2024-08-05 15:00:29 +00:00
import { apiCall, enableApi, strmid } from './utility/api';
2024-07-27 09:26:03 +00:00
import { useConfig } from './utility/metadataLoaders';
import ErrorInfo from './elements/ErrorInfo.svelte';
2024-08-02 10:25:19 +00:00
import FormSelectField from './forms/FormSelectField.svelte';
import { writable } from 'svelte/store';
import FormProviderCore from './forms/FormProviderCore.svelte';
2024-08-05 15:00:29 +00:00
import { openWebLink } from './utility/exportFileTools';
import FontIcon from './icons/FontIcon.svelte';
import createRef from './utility/createRef';
2022-11-25 12:36:18 +00:00
2024-07-26 14:30:01 +00:00
export let isAdminPage;
2024-07-27 09:26:03 +00:00
const config = useConfig();
2024-08-02 10:25:19 +00:00
let availableConnections = null;
2024-08-07 11:58:44 +00:00
let availableProviders = [];
2024-08-05 15:00:29 +00:00
let isTesting = false;
const testIdRef = createRef(0);
let sqlConnectResult;
2024-08-02 10:25:19 +00:00
2024-08-07 11:58:44 +00:00
let serversLoadedForAmoId = null;
const values = writable({ amoid: null, databaseServer: null });
2024-08-05 15:00:29 +00:00
$: selectedConnection = availableConnections?.find(x => x.conid == $values.databaseServer);
2024-08-02 10:25:19 +00:00
2024-08-07 12:47:33 +00:00
$: selectedProvider = availableProviders?.find(x => x.amoid == $values.amoid);
$: workflowType = selectedProvider?.workflowType ?? 'credentials';
2024-08-07 11:58:44 +00:00
async function loadAvailableServers(amoid) {
if (amoid) {
availableConnections = await apiCall('storage/get-connections-for-login-page', { amoid });
if (availableConnections?.length > 0) {
values.update(x => ({ ...x, databaseServer: availableConnections[0].conid }));
}
serversLoadedForAmoId = amoid;
} else {
availableConnections = null;
2024-08-02 10:25:19 +00:00
}
}
2024-08-08 07:16:50 +00:00
async function processSingleProvider(provider) {
if (provider.workflowType == 'redirect') {
await processRedirectLogin(provider.amoid);
}
if (provider.workflowType == 'anonymous') {
processCredentialsLogin(provider.amoid, {});
}
}
2024-08-07 11:58:44 +00:00
async function loadAvailableAuthProviders() {
const resp = await apiCall('auth/get-providers');
availableProviders = resp.providers;
values.update(x => ({ ...x, amoid: resp.default }));
2024-08-08 07:16:50 +00:00
if (availableProviders.length == 1) {
processSingleProvider(availableProviders[0]);
}
2024-08-07 11:58:44 +00:00
}
2022-11-25 12:36:18 +00:00
onMount(() => {
const removed = document.getElementById('starting_dbgate_zero');
if (removed) removed.remove();
2024-08-02 10:25:19 +00:00
if (!isAdminPage) {
2024-08-07 11:58:44 +00:00
loadAvailableAuthProviders();
2024-08-02 10:25:19 +00:00
}
2022-11-25 12:36:18 +00:00
});
2024-08-07 11:58:44 +00:00
$: if ($values.amoid != serversLoadedForAmoId) {
loadAvailableServers($values.amoid);
}
2024-08-08 07:16:50 +00:00
async function processRedirectLogin(amoid) {
const state = `dbg-oauth:${strmid}:${amoid}`;
sessionStorage.setItem('oauthState', state);
console.log('Redirecting to OAUTH provider');
const resp = await apiCall('auth/redirect', {
amoid: amoid,
state,
redirectUri: location.origin + location.pathname,
});
const { uri } = resp;
if (uri) {
location.replace(uri);
}
}
async function processCredentialsLogin(amoid, detail) {
const resp = await apiCall('auth/login', {
amoid,
isAdminPage,
...detail,
});
if (resp.error) {
internalRedirectTo(
2024-09-03 10:57:52 +00:00
`/not-logged.html?error=${encodeURIComponent(resp.error)}&is-admin=${isAdminPage ? 'true' : ''}`
2024-08-08 07:16:50 +00:00
);
return;
}
const { accessToken } = resp;
if (accessToken) {
localStorage.setItem(isAdminPage ? 'adminAccessToken' : 'accessToken', accessToken);
if (isAdminPage) {
2024-09-03 10:57:52 +00:00
internalRedirectTo('/admin.html');
2024-08-08 07:16:50 +00:00
} else {
internalRedirectTo('/');
}
return;
}
2024-09-03 10:57:52 +00:00
internalRedirectTo(`/not-logged.html`);
2024-08-08 07:16:50 +00:00
}
2022-11-25 12:36:18 +00:00
</script>
<div class="root theme-light theme-type-light">
<div class="text">DbGate</div>
<div class="wrap">
<div class="logo">
<img class="img" src="logo192.png" />
</div>
<div class="box">
<div class="heading">Log In</div>
2024-08-02 10:25:19 +00:00
<FormProviderCore {values}>
2024-08-08 07:16:50 +00:00
{#if !isAdminPage && availableProviders?.length >= 2}
2024-08-07 11:58:44 +00:00
<FormSelectField
label="Authentization method"
name="amoid"
isNative
options={availableProviders.map(mtd => ({ value: mtd.amoid, label: mtd.name }))}
/>
{/if}
2024-08-07 12:47:33 +00:00
{#if !isAdminPage && availableConnections && workflowType == 'database'}
2024-08-02 10:25:19 +00:00
<FormSelectField
label="Database server"
name="databaseServer"
isNative
2024-08-05 15:00:29 +00:00
options={availableConnections.map(conn => ({ value: conn.conid, label: conn.label }))}
2024-08-02 10:25:19 +00:00
/>
{/if}
2024-08-05 15:00:29 +00:00
{#if selectedConnection}
{#if selectedConnection.passwordMode == 'askUser'}
<FormTextField label="Username" name="login" autocomplete="username" saveOnInput />
{/if}
{#if selectedConnection.passwordMode == 'askUser' || selectedConnection.passwordMode == 'askPassword'}
<FormPasswordField label="Password" name="password" autocomplete="current-password" saveOnInput />
{/if}
{:else}
2024-08-07 12:47:33 +00:00
{#if !isAdminPage && workflowType == 'credentials'}
2024-08-05 15:00:29 +00:00
<FormTextField label="Username" name="login" autocomplete="username" saveOnInput />
{/if}
2024-08-07 12:47:33 +00:00
{#if workflowType == 'credentials'}
<FormPasswordField label="Password" name="password" autocomplete="current-password" saveOnInput />
{/if}
2024-07-26 14:30:01 +00:00
{/if}
2022-11-25 12:36:18 +00:00
2024-07-27 09:26:03 +00:00
{#if isAdminPage && $config && !$config.isAdminLoginForm}
<ErrorInfo message="Admin login is not configured. Please set ADMIN_PASSWORD environment variable" />
{/if}
2024-08-05 15:00:29 +00:00
{#if isTesting}
<div class="ml-5">
<FontIcon icon="icon loading" /> Testing connection
</div>
{/if}
{#if !isTesting && sqlConnectResult && sqlConnectResult.msgtype == 'error'}
<div class="error-result ml-5">
Connect failed: <FontIcon icon="img error" />
{sqlConnectResult.error}
</div>
{/if}
2022-11-25 12:36:18 +00:00
<div class="submit">
2024-08-05 15:00:29 +00:00
{#if selectedConnection?.useRedirectDbLogin}
<FormSubmit
value="Open database login page"
on:click={async e => {
2024-08-07 15:02:19 +00:00
const state = `dbg-dblogin:${strmid}:${selectedConnection?.conid}:${$values.amoid}`;
2024-08-06 10:45:28 +00:00
sessionStorage.setItem('dbloginAuthState', state);
// openWebLink(
2024-08-05 15:00:29 +00:00
// `connections/dblogin?conid=${selectedConnection?.conid}&state=${encodeURIComponent(state)}&redirectUri=${
// location.origin + location.pathname
// }`
// );
2024-08-06 10:45:28 +00:00
internalRedirectTo(
2024-08-14 14:33:30 +00:00
`/connections/dblogin-web?conid=${selectedConnection?.conid}&state=${encodeURIComponent(state)}&redirectUri=${
2024-08-06 10:45:28 +00:00
location.origin + location.pathname
}`
);
2024-08-05 15:00:29 +00:00
}}
/>
{:else if selectedConnection}
<FormSubmit
value="Log In"
on:click={async e => {
if (selectedConnection.passwordMode == 'askUser' || selectedConnection.passwordMode == 'askPassword') {
enableApi();
isTesting = true;
testIdRef.update(x => x + 1);
const testid = testIdRef.get();
const resp = await apiCall('connections/dblogin-auth', {
2024-08-07 15:02:19 +00:00
amoid: $values.amoid,
2024-08-05 15:00:29 +00:00
conid: selectedConnection.conid,
user: $values['login'],
password: $values['password'],
});
if (testIdRef.get() != testid) return;
isTesting = false;
if (resp.accessToken) {
localStorage.setItem('accessToken', resp.accessToken);
2024-08-06 12:59:09 +00:00
internalRedirectTo('/');
2024-08-05 15:00:29 +00:00
} else {
sqlConnectResult = resp;
}
2024-08-06 08:58:18 +00:00
} else {
enableApi();
const resp = await apiCall('connections/dblogin-auth', {
2024-08-07 15:02:19 +00:00
amoid: $values.amoid,
2024-08-06 08:58:18 +00:00
conid: selectedConnection.conid,
});
localStorage.setItem('accessToken', resp.accessToken);
2024-08-06 12:59:09 +00:00
internalRedirectTo('/');
2024-07-27 09:26:03 +00:00
}
2024-08-05 15:00:29 +00:00
}}
/>
{:else}
<FormSubmit
2024-08-07 12:47:33 +00:00
value={isAdminPage
? 'Log In as Administrator'
: workflowType == 'redirect'
? 'Redirect to login page'
: 'Log In'}
2024-08-05 15:00:29 +00:00
on:click={async e => {
enableApi();
2024-08-07 12:47:33 +00:00
if (isAdminPage || workflowType == 'credentials' || workflowType == 'anonymous') {
2024-08-08 07:16:50 +00:00
await processCredentialsLogin($values.amoid, e.detail);
2024-08-07 14:28:24 +00:00
} else if (workflowType == 'redirect') {
2024-08-08 07:16:50 +00:00
await processRedirectLogin($values.amoid);
2024-08-05 15:00:29 +00:00
}
}}
/>
{/if}
2022-11-25 12:36:18 +00:00
</div>
2024-08-02 10:25:19 +00:00
</FormProviderCore>
2022-11-25 12:36:18 +00:00
</div>
</div>
</div>
<style>
.logo {
display: flex;
margin-bottom: 1rem;
align-items: center;
justify-content: center;
}
.img {
width: 80px;
}
.text {
position: fixed;
top: 1rem;
left: 1rem;
2022-11-25 15:38:17 +00:00
font-size: 30pt;
font-family: monospace;
2022-11-25 12:36:18 +00:00
color: var(--theme-bg-2);
2022-11-25 15:38:17 +00:00
text-transform: uppercase;
2022-11-25 12:36:18 +00:00
}
.submit {
margin: var(--dim-large-form-margin);
display: flex;
}
.submit :global(input) {
flex: 1;
font-size: larger;
}
.root {
color: var(--theme-font-1);
display: flex;
justify-content: center;
background-color: var(--theme-bg-1);
align-items: baseline;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.box {
2022-11-25 15:38:17 +00:00
width: 600px;
max-width: 80vw;
/* max-width: 600px;
width: 40vw; */
2022-11-25 12:36:18 +00:00
border: 1px solid var(--theme-border);
border-radius: 4px;
background-color: var(--theme-bg-0);
}
.wrap {
margin-top: 20vh;
}
.heading {
text-align: center;
margin: 1em;
font-size: xx-large;
}
</style>