Skip to content

Commit

Permalink
[docs] Improved security in code example (#6877)
Browse files Browse the repository at this point in the history
  • Loading branch information
bfanger authored Sep 19, 2022
1 parent 33722bd commit c032e59
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions documentation/docs/06-form-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,13 @@ export const actions = {
const email = data.get('email');
const password = data.get('password');

const user = await db.getUser(email);
+ if (!user) {
+ if (!email) {
+ return invalid(400, { email, missing: true });
+ }
+
+ if (user.password !== hash(password)) {

const user = await db.getUser(email);

+ if (!user || user.password !== hash(password)) {
+ return invalid(400, { email, incorrect: true });
+ }

Expand All @@ -173,11 +174,10 @@ export const actions = {
/// file: src/routes/login/+page.svelte
<form method="POST" action="?/login">
- <input name="email" type="email">
+ {#if form?.missing}<p class="error">No user found with this email</p>{/if}
+ {#if form?.missing}<p class="error">The email field is required</p>{/if}
+ {#if form?.incorrect}<p class="error">Invalid credentials!</p>{/if}
+ <input name="email" type="email" value={form?.email ?? ''}>

- <input name="password" type="password">
+ {#if form?.incorrect}<p class="error">Wrong password!</p>{/if}
<input name="password" type="password">
<button>Log in</button>
<button formaction="?/register">Register</button>
Expand Down

1 comment on commit c032e59

@machak
Copy link

@machak machak commented on c032e59 Sep 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

providing information like "user not found" and "invalid credentials" is already considered security risk these days,
so merging messages into one message "invalid username or password" would be a better example...

Please sign in to comment.