Skip to content

Commit 722dc74

Browse files
committed
add an error_message property to the login component
- Updated SQL queries in `create_session_token.sql` and `login.sql` to use consistent parameter naming conventions. - Enhanced the login form in `login.sql` to include an error message for failed login attempts. - Added a new parameter `error_message` in the migration file for better user feedback. - Modified the Handlebars template to display the error message when applicable, improving user experience during authentication.
1 parent c28cf2a commit 722dc74

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

examples/official-site/examples/authentication/create_session_token.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ delete from user_sessions where created_at < datetime('now', '-1 day');
44
-- check that the
55
SELECT 'authentication' AS component,
66
'login.sql?failed' AS link, -- redirect to the login page on error
7-
(SELECT password_hash FROM users WHERE username = :Username) AS password_hash, -- this is a hash of the password 'admin'
8-
:Password AS password; -- this is the password that the user sent through our form in 'index.sql'
7+
(SELECT password_hash FROM users WHERE username = :username) AS password_hash, -- this is a hash of the password 'admin'
8+
:password AS password; -- this is the password that the user sent through our form in 'index.sql'
99

1010
-- if we haven't been redirected, then the password is correct
1111
-- create a new session
12-
insert into user_sessions (session_token, username) values (sqlpage.random_string(32), :Username)
12+
insert into user_sessions (session_token, username) values (sqlpage.random_string(32), :username)
1313
returning 'cookie' as component, 'session_token' as name, session_token as value;
1414

1515
-- redirect to the authentication example home page

examples/official-site/examples/authentication/login.sql

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
select 'dynamic' as component, properties FROM example WHERE component = 'shell' LIMIT 1;
22

3-
select 'form' as component, 'Authentication' as title, 'Log in' as validate, 'create_session_token.sql' as action;
4-
select 'Username' as name, 'user' as prefix_icon, 'admin' as placeholder;
5-
select 'Password' as name, 'lock' as prefix_icon, 'admin' as placeholder, 'password' as type;
6-
7-
select 'alert' as component, 'danger' as color, 'Invalid username or password' as title where $failed is not null;
3+
select
4+
'login' as component,
5+
'create_session_token.sql' as action,
6+
'/assets/icon.webp' as image,
7+
'Demo Login Form' as title,
8+
'Username' as username,
9+
'Password' as password,
10+
case when $failed is not null then 'Invalid username or password. In this demo, you can log in with admin / admin.' end as error_message,
11+
'In this demo, the username is "admin" and the password is "admin".' as footer_md,
12+
'Sign in' as validate;
813

914
select 'text' as component, '
1015
1116
# Authentication
1217
1318
This is a simple example of an authentication form.
1419
It uses
15-
- the [`form`](/documentation.sql?component=form#component) component to create a login form
20+
- the [`login`](/documentation.sql?component=login#component) component to create a login form
1621
- the [`authentication`](/documentation.sql?component=authentication#component) component to check the user password
1722
- the [`cookie`](/documentation.sql?component=cookie#component) component to store a unique session token in the user browser
1823
- the [`redirect`](/documentation.sql?component=redirect#component) component to redirect the user to the login page if they are not logged in

examples/official-site/sqlpage/migrations/68_login.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
2020
('title','Title of the authentication form.','TEXT',TRUE,TRUE),
2121
('enctype','Form data encoding.','TEXT',TRUE,TRUE),
2222
('action','An optional link to a target page that will handle the results of the form. ','TEXT',TRUE,TRUE),
23+
('error_message','An error message to display above the form, typically shown after a failed login attempt.','TEXT',TRUE,TRUE),
2324
('username','User account identifier.','TEXT',TRUE,FALSE),
2425
('password','User password.','TEXT',TRUE,FALSE),
2526
('username_icon','Icon to display on the left side of the input field, on the same line.','ICON',TRUE,TRUE),

sqlpage/templates/login.handlebars

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,23 @@
1313
{{/if}}
1414
>
1515
{{#if image}}
16-
<div class="form-group d-flex justify-content-center align-items-center">
16+
<div class="form-group d-flex justify-content-center align-items-center mb-2">
1717
<img src="{{image}}"/>
1818
</div>
1919
{{/if}}
2020
{{#if title}}
2121
<h1 class="text-center mb-3">{{title}}</h1>
2222
{{/if}}
23+
{{#if error_message}}
24+
<div class="alert alert-danger mb-3" role="alert">
25+
<div class="alert-icon">
26+
{{icon_img 'alert-circle'}}
27+
</div>
28+
<div class="overflow-auto w-100">
29+
{{error_message}}
30+
</div>
31+
</div>
32+
{{/if}}
2333
<label class="form-label">{{username}}</label>
2434
<div class="input-icon mb-3">
2535
{{#if username_icon}}

0 commit comments

Comments
 (0)