You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.0 KiB
74 lines
2.0 KiB
<script lang="ts">
|
|
import type {CognitoUser} from "amazon-cognito-identity-js";
|
|
|
|
import { signIn } from "../clients/amplify";
|
|
import authStore from "../stores/auth";
|
|
import Modal from "../components/Modal.svelte";
|
|
|
|
let user: CognitoUser | null = null;
|
|
let username = "";
|
|
let password = "";
|
|
let newPassword = "";
|
|
let newPasswordRepeat = "";
|
|
let settingNewPassword = false;
|
|
let error = null;
|
|
let done = false;
|
|
|
|
function login() {
|
|
error = null;
|
|
|
|
if (settingNewPassword) {
|
|
if (newPasswordRepeat !== newPassword) {
|
|
error = "New passwords do not match.";
|
|
return;
|
|
}
|
|
|
|
user.completeNewPasswordChallenge(newPassword, null, {
|
|
onSuccess: () => {
|
|
done = true;
|
|
authStore.check();
|
|
},
|
|
onFailure: err => {
|
|
error = err
|
|
},
|
|
})
|
|
} else {
|
|
signIn(username, password).then(newUser => {
|
|
if (!newUser) {
|
|
error = "Incorrect username or password."
|
|
return
|
|
}
|
|
|
|
if ((newUser as any).challengeName === "NEW_PASSWORD_REQUIRED") {
|
|
error = "Password is expired, please update it."
|
|
settingNewPassword = true;
|
|
} else {
|
|
authStore.check();
|
|
done = true;
|
|
}
|
|
|
|
user = newUser;
|
|
}).catch(err => {
|
|
error = err
|
|
});
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<Modal show={!done} title="Login" error={error}>
|
|
<form on:submit|preventDefault={login}>
|
|
<label for="username">Username</label>
|
|
<input name="username" type="text" bind:value={username} />
|
|
<label for="password">Password</label>
|
|
<input name="password" type="password" bind:value={password} />
|
|
{#if settingNewPassword}
|
|
<label for="newPassword">New Password</label>
|
|
<input name="newPassword" type="password" bind:value={newPassword} />
|
|
<label for="newPasswordRepeat">New Password</label>
|
|
<input name="newPasswordRepeat" type="password" bind:value={newPasswordRepeat} />
|
|
{/if}
|
|
|
|
<hr />
|
|
<button type="submit">Login</button>
|
|
</form>
|
|
</Modal>
|