Fixes in signup
This commit is contained in:
parent
9dd09d3234
commit
73f15ac4dc
2 changed files with 59 additions and 5 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
from fastapi import FastAPI, Cookie, Request, Response
|
from fastapi import FastAPI, Cookie, Request, Response, Body
|
||||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||||
from parameciofast.modules.fastadmin import admin_app
|
from parameciofast.modules.fastadmin import admin_app
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
|
|
@ -91,6 +91,51 @@ def check_login_admin(user: UserAdmin, request: Request) -> ResponseData:
|
||||||
|
|
||||||
return {'error': error, 'message': message}
|
return {'error': error, 'message': message}
|
||||||
|
|
||||||
|
class UserSignup(BaseModel):
|
||||||
|
username: str = Field(description="The username of new user", min_length=4, pattern=r"\w+")
|
||||||
|
email: str = Field(description="The email of new user", min_length=7 , pattern=r"\w[\w\.-]*@\w[\w\.-]+\.\w+")
|
||||||
|
password: str = Field(description="The password of new user", min_length=4)
|
||||||
|
repeat_password: str = Field(description="Repeat the password of the new user", min_length=4)
|
||||||
|
|
||||||
|
model_config = {
|
||||||
|
"json_schema_extra": {
|
||||||
|
"examples": [
|
||||||
|
{
|
||||||
|
"username": "johnny",
|
||||||
|
"email": "trial@example.com",
|
||||||
|
"password": "anrandompasswordthatineverused",
|
||||||
|
"repeat_password": "anrandompasswordthatineverused",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@admin_app.post('/signup')
|
||||||
|
def signup_insert_admin(user: Annotated[UserSignup, Body(embed=True)], request: Request) -> ResponseData:
|
||||||
|
|
||||||
|
i18n=I18n('admin', I18n.session_lang(request.session))
|
||||||
|
|
||||||
|
error=1
|
||||||
|
|
||||||
|
message=''
|
||||||
|
|
||||||
|
db=WebModel.connection()
|
||||||
|
|
||||||
|
#Only can exist and user
|
||||||
|
|
||||||
|
with db.query('select count(id) as num_users from useradmin', []) as cursor:
|
||||||
|
num_users=cursor.fetchone()['num_users']
|
||||||
|
|
||||||
|
if num_users:
|
||||||
|
return RedirectResponse(app.url_path_for('login_admin'))
|
||||||
|
else:
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
return {'error': error, 'message': message}
|
||||||
|
|
||||||
@admin_app.get('/logout')
|
@admin_app.get('/logout')
|
||||||
def logout_admin(request: Request) -> RedirectResponse:
|
def logout_admin(request: Request) -> RedirectResponse:
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
${tlang('Signup')}
|
${tlang('Signup')}
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<form method="POST" action="${url_for('login_admin')}" id="login_form">
|
<form method="POST" action="${url_for('signup_insert_admin')}" id="login_form">
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="username_form" class="form-label">${tlang('Username')}*</label>
|
<label for="username_form" class="form-label">${tlang('Username')}*</label>
|
||||||
<input type="text" class="form-control form-control-lg" id="username_form" name="password" aria-describedby="username" autocomplete="off">
|
<input type="text" class="form-control form-control-lg" id="username_form" name="password" aria-describedby="username" autocomplete="off">
|
||||||
|
|
@ -39,6 +39,7 @@
|
||||||
</div>
|
</div>
|
||||||
</%block>
|
</%block>
|
||||||
<%block name="jscript">
|
<%block name="jscript">
|
||||||
|
<script>
|
||||||
$(document).ready( function () {
|
$(document).ready( function () {
|
||||||
|
|
||||||
$('#login_form').submit( function () {
|
$('#login_form').submit( function () {
|
||||||
|
|
@ -47,14 +48,21 @@ $(document).ready( function () {
|
||||||
|
|
||||||
$('#login_submit').prop('disabled', true);
|
$('#login_submit').prop('disabled', true);
|
||||||
|
|
||||||
data_form={'username': $('#username_form').val(), 'email': $('#email_form').val(), 'password': $('#password_form').val(), 'repeat_password': $('#repeat_password_form').val(), 'csrf_token': $("#csrf_token").val(), 'remember_login': 0};
|
data_form={'username': $('#username_form').val(), 'email': $('#email_form').val(), 'password': $('#password_form').val(), 'repeat_password': $('#repeat_password_form').val(), 'csrf_token': $("#csrf_token").val()};
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "${url_for('check_login_admin')}",
|
url: "${url_for('signup_insert_admin')}",
|
||||||
method: "POST",
|
method: "POST",
|
||||||
dataType: "json",
|
dataType: "json",
|
||||||
contentType : 'application/json',
|
contentType : 'application/json',
|
||||||
data: JSON.stringify(data_form)
|
data: JSON.stringify(data_form),
|
||||||
|
error: function (data) {
|
||||||
|
|
||||||
|
console.log(JSON.stringify(data));
|
||||||
|
$('#loader-div').hide();
|
||||||
|
$('#login_submit').prop('disabled', false);
|
||||||
|
|
||||||
|
},
|
||||||
}).done(function(data) {
|
}).done(function(data) {
|
||||||
|
|
||||||
if(data.error==0)
|
if(data.error==0)
|
||||||
|
|
@ -110,4 +118,5 @@ $(document).ready( function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
</script>
|
||||||
</%block>
|
</%block>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue