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(); 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> { async function getAccessToken(): Promise<CognitoAccessToken | null> {
try { try {
const u = await Auth.currentSession(); const u = await Auth.currentSession();

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

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