Browse Source

add password reset.

main
Gisle Aune 4 years ago
parent
commit
4443ec9c84
  1. 8
      svelte-ui/src/clients/amplify.ts
  2. 35
      svelte-ui/src/forms/LoginForm.svelte

8
svelte-ui/src/clients/amplify.ts

@ -19,6 +19,14 @@ export async function signOut(): Promise<void> {
await Auth.signOut();
}
export async function forgotPassword(username: string): Promise<void> {
const res = await Auth.forgotPassword(username);
}
export async function forgotPasswordSubmit(username: string, code: string, password: string): Promise<void> {
await Auth.forgotPasswordSubmit(username, code, password);
}
async function getAccessToken(): Promise<CognitoAccessToken | null> {
try {
const u = await Auth.currentSession();

35
svelte-ui/src/forms/LoginForm.svelte

@ -1,7 +1,7 @@
<script lang="ts">
import type {CognitoUser} from "amazon-cognito-identity-js";
import { signIn } from "../clients/amplify";
import { forgotPassword, forgotPasswordSubmit, signIn } from "../clients/amplify";
import authStore from "../stores/auth";
import Modal from "../components/Modal.svelte";
@ -10,10 +10,21 @@
let password = "";
let newPassword = "";
let newPasswordRepeat = "";
let resetCode = "";
let settingNewPassword = false;
let resettingPassword = false;
let error = null;
let done = false;
function startPasswordReset() {
forgotPassword(username).then(() => {
settingNewPassword = true;
resettingPassword = true;
}).catch(err => {
error = "Reset request failed: " + err.message;
})
}
function login() {
error = null;
@ -23,6 +34,15 @@
return;
}
if (resettingPassword) {
forgotPasswordSubmit(username, resetCode, newPassword).then(() => {
authStore.check();
settingNewPassword = false;
resettingPassword = false;
}).catch(err => {
error = "Reset submit failed: " + err.message;
});
} else {
user.completeNewPasswordChallenge(newPassword, null, {
onSuccess: () => {
done = true;
@ -32,6 +52,7 @@
error = err
},
})
}
} else {
signIn(username, password).then(newUser => {
if (!newUser) {
@ -49,7 +70,7 @@
user = newUser;
}).catch(err => {
error = err
error = err.message
});
}
}
@ -59,16 +80,24 @@
<form on:submit|preventDefault={login}>
<label for="username">Username</label>
<input name="username" type="text" bind:value={username} />
{#if !resettingPassword}
<label for="password">Password</label>
<input name="password" type="password" bind:value={password} />
{:else}
<label for="resetCode">Code From Email</label>
<input name="resetCode" type="text" bind:value={resetCode} />
{/if}
{#if settingNewPassword}
<label for="newPassword">New Password</label>
<input name="newPassword" type="password" bind:value={newPassword} />
<label for="newPasswordRepeat">New Password</label>
<label for="newPasswordRepeat">Repeat Password</label>
<input name="newPasswordRepeat" type="password" bind:value={newPasswordRepeat} />
{/if}
<hr />
<button type="submit">Login</button>
{#if !settingNewPassword}
<button disabled={username === ""} on:click|preventDefault={startPasswordReset}>Forgot Password</button>
{/if}
</form>
</Modal>
Loading…
Cancel
Save