Skip to content

Commit

Permalink
Store passwords in localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
bbb651 committed Feb 18, 2021
1 parent 13a70e5 commit 8aa8c89
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@
import PasswordTable from "./PasswordTable.svelte";
import PasswordForm from "./PasswordForm.svelte";
import type { Password } from "./types/Password";
import { onMount } from "svelte";
let passwords: Password[] = [];
onMount(() => {
passwords = JSON.parse(window.localStorage.getItem("passworddatabase")) || [];
});
const storeDatabase = () => {
window.localStorage.setItem("passworddatabase", JSON.stringify(passwords));
};
const addPassword = (password: Password) => {
passwords = [...passwords, password];
};
</script>

<main>
<h1>Password Manager</h1>
<PasswordTable {passwords} />
<PasswordForm {addPassword} />
<PasswordTable {passwords} {storeDatabase} />
<PasswordForm {addPassword} {storeDatabase} />
</main>

<style>
Expand Down
2 changes: 2 additions & 0 deletions src/PasswordForm.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
export let addPassword;
export let storeDatabase;
let title: string;
let password: string;
Expand All @@ -13,6 +14,7 @@
if (!password)
return;
addPassword({title, password, username, url, notes});
storeDatabase();
title = "";
password = "";
username = "";
Expand Down
2 changes: 2 additions & 0 deletions src/PasswordTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type { Password } from "./types/Password";
import { elementIndexInParent } from "./util";
export let passwords: Password[];
export let storeDatabase;
const copy = (e: MouseEvent) => {
const entry: HTMLElement = (e.target as HTMLElement).parentElement;
Expand All @@ -15,6 +16,7 @@
const index = elementIndexInParent(entry) - 1;
passwords.splice(index, 1);
passwords = passwords;
storeDatabase();
};
</script>

Expand Down

0 comments on commit 8aa8c89

Please sign in to comment.