Rework the login page in the new example.

This commit is contained in:
Relintai 2024-02-26 19:48:40 +01:00
parent f5319e3858
commit abf64d6d10
5 changed files with 95 additions and 73 deletions

View File

@ -1,5 +1,7 @@
extends UserLoginWebPage
export(HTMLTemplate) var login_template : HTMLTemplate
var _login_validator : FormValidator = null
#func _render_index(request: WebServerRequest) -> void:
@ -86,78 +88,8 @@ func _render_user_page(request: WebServerRequest, data: Dictionary) -> void:
if type == "render_login_success":
request.send_redirect(redirect_on_success_url)
return
var b : HTMLBuilder = HTMLBuilder.new()
# Title
b.div("row mb-4")
b.div("col-2")
b.cdiv()
b.div("col-8")
b.h2()
b.w("Login")
b.ch2()
b.cdiv()
b.div("col-2")
b.cdiv()
b.cdiv()
# Errors
var error_str : String = data["error_str"]
if !error_str.empty():
b.div("row mb-4")
b.div("col-2")
b.cdiv()
b.div("col-8")
b.div("alert alert-danger").attrib("role", "alert")
b.w(error_str)
b.cdiv()
b.cdiv()
b.div("col-2")
b.cdiv()
b.cdiv()
# Form
b.div("row")
b.div("col-2")
b.cdiv()
b.div("col-8")
if true:
b.form().method_post()
b.csrf_tokenr(request)
b.div("form-group")
b.label().fora("username_input").cls("form_label").f().w("Username").clabel()
b.input_text("username", data["uname_val"], "", "form-control", "username_input")
b.cdiv()
b.div("form-group")
b.label().fora("password_input").cls("form_label").f().w("Password").clabel()
b.input_password("password", "", "*******", "form-control", "password_input")
b.cdiv()
b.button().type("submit").cls("btn btn-outline-primary mt-3").f().w("Send").cbutton()
b.cform()
b.cdiv()
b.div("col-2")
b.cdiv()
b.cdiv()
b.write_tag()
request.body += b.result
request.body += login_template.render(request, data)
request.compile_and_send_body()
func _ready() -> void:

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=11 format=2]
[gd_scene load_steps=12 format=2]
[ext_resource path="res://WebServerSimple.gd" type="Script" id=1]
[ext_resource path="res://WebRoot.gd" type="Script" id=2]
@ -7,6 +7,7 @@
[ext_resource path="res://Login.gd" type="Script" id=5]
[ext_resource path="res://Register.gd" type="Script" id=6]
[ext_resource path="res://Settings.gd" type="Script" id=7]
[ext_resource path="res://templates/LoginPage.tres" type="HTMLTemplate" id=8]
[sub_resource type="SessionSetupWebServerMiddleware" id=3]
@ -53,6 +54,7 @@ uri_segment = "login"
logged_in_render_type = 1
logged_in_redirect_url = "/"
script = ExtResource( 5 )
login_template = ExtResource( 8 )
[node name="Register" type="UserRegisterWebPage" parent="WebServerSimple/WebRoot/UserWebPage"]
uri_segment = "register"

View File

@ -0,0 +1,27 @@
[gd_resource type="HTMLTemplate" load_steps=3 format=2]
[ext_resource path="res://templates/LoginPageTD.phtpl" type="HTMLTemplateData" id=1]
[sub_resource type="GDScript" id=1]
script/source = "extends HTMLTemplate
func _render(request: WebServerRequest, data: Dictionary) -> String:
var error_str : String = data[\"error_str\"]
if (!error_str.empty()):
data[\"error_str\"] = get_and_render_template(@\"LoginErrorStrTemplate\", data)
var uname_val : String = data[\"uname_val\"]
data[\"username_prev_value\"] = \"\"
if (!uname_val.empty()):
data[\"username_prev_value\"] = 'value=\"' + uname_val + '\"'
return get_and_render_template(@\"Login\", data)
"
[resource]
templates = [ ExtResource( 1 ) ]
template_override/add_key_button = null
template_defaults/add_key_button = null
script = SubResource( 1 )

View File

@ -0,0 +1,48 @@
[ Login ]
<div class="row mb-4">
<div class="col-2"></div>
<div class="col-8">
<h2>Login</h2>
</div>
<div class="col-2"></div>
</div>
{{ pr(error_str) }}
<div class="row">
<div class="col-2"></div>
<div class="col-8">
<form method="POST">
<div class="form-group">
<label for="username_input" class="form_label">Username</label>
<input type="text" name="username" {{ username_prev_value }} class="form-control" id="username_input">
</div>
<div class="form-group">
<label for="password_input" class="form_label">Password</label>
<input type="password" name="password" placeholder="*******" class="form-control" id="password_input">
</div>
<button type="submit" class="btn btn-outline-primary mt-3">Send</button>
</form>
</div>
<div class="col-2"></div>
</div>
[ LoginErrorStrTemplate ]
<div class="row mb-4">
<div class="col-2"> </div>
<div class="col-8">
<div class="alert alert-danger" role="alert">
{{ pr(error_str) }}
</div>
</div>
<div class="col-2"> </div>
</div>

View File

@ -0,0 +1,13 @@
[remap]
importer="html_template_data"
type="HTMLTemplateData"
path="res://.import/LoginPageTD.phtpl-f9fdc5c73f5a02caa6f37a7c338b3362.res"
[deps]
source_file="res://templates/LoginPageTD.phtpl"
dest_files=[ "res://.import/LoginPageTD.phtpl-f9fdc5c73f5a02caa6f37a7c338b3362.res" ]
[params]