I’m trying to create a user registration form and create collection upon registration.
I tried following code, but when i try to execute i get this error:
HTTP 400 Bad Request and page cannot be displayed. Here is the code:
xquery version "1.0";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace response="http://exist-db.org/xquery/response";
declare namespace xdb="http://exist-db.org/xquery/xmldb";
declare option exist:serialize "method=xhtml media-type=text/html indent=yes";
declare variable $database-uri as xs:string { "xmldb:exist:///db" };
declare variable $redirect-uri as xs:anyURI { xs:anyURI("login.xql") };
declare function local:header() as node(){
<div style="background-color:#b0c4de; padding: 20px; border: 1px solid black;">
<h1>test</h1>
</div>
};
declare function local:footer() as node(){
<div style="background color:#eee; height: 1%; padding:20px; border: 1px solid black;">
<p>PROJECT</p>
</div>
};
declare function local:reg($user as xs: string) as element()?
{
let $password := request:get-parameter("password", "")
let $password2 := request:get-parameter("password2", "")
return
if (not(xdb:exists-user($user))) then (
xdb:create-collection($database-uri, $user),
xdb:create-user($user, $password, "guest", ()),
response:redirect-to($redirect-uri)
) else
<p>User already exists!</p>
};
declare function local:do-reg() as element()?
{
let $user := request:get-parameter("user", ())
return
<p>{$user}</p>
if($user) then
local:reg($user)
else ()
};
<html>
<head><title>Registration</title>
</head>
<body>
{local:header()}
<h1>Forma for reg</h1>
<form action="{request:get-uri()}">
User Name: <br />
<input type="text" name="user" size="20" />
<br />
Password: <br />
<input type="password" name="password" size="20" />
<br />
Confirm: <br />
<input type="password" name="password2" size="20" />
<br />
<input type="submit" />
<input type="reset" name="reset" value="Clear" />
</form>
{local:do-reg()}
{local:footer()}
</body>
</html>
Please help, i’m beginner in xquery and exist db. Thanks in advance!
I got it, In exist you can’t register new user if you are not in db group of premissions.
So what needs to be done is to create a new user for you collection, and give him right premissions, then you need to login with that user in order to register/create new user.
the problem with this is that you put admin user and password in your .xql document – and that is not a good idea. But you can use:
and
to encode and decode you password. so how i did it is like this:
system:as-user((util:base64-decode(“bXBhcmF2YWM=”)),(util:base64-decode(“cGFzcw==”)), (
if (not(xdb:exists-user($user))) then (
xdb:create-collection($database-uri, $user),
xdb:create-user($user, $pass, ‘user’, ()),
response:redirect-to($redirect-uri)
) else
User exists!
) )
Also there is another way to do this, even more secure, take a look at this example