Hi Yoann,
I guess having examples like these would be helpful in general. Together with basex, we built a larger project for managing conference registrations etc. last year. I could extract some of the modules - maybe we could manage to publish them on some github repo or a dedicated site? @basex: maybe it is time for a basex contrib page?
What you need, is some json endpoint which leverages the session module, checks the credentials and returns some json back to Angular (or JQuey)
Besides that, you need to secure all your restxq endpoints with a authentication check:
if(not(session:logged-in())) then web:redirect($C:START-PAGE) else _:do-something-useful()
Login example:
import module namespace session = "http://basex.org/modules/web/session";
declare %restxq:path("/api/login/check") %restxq:POST("{$content}") %output:method("json") %output:json("lax=yes") function _:check( $content as item()* ) { let $user := $content//u/string() let $pass := $content//p/string()
let $ok := _:check-user($user, $pass)
if ($ok) then let $user-id := session:id() let $role := session:role() return((), <json objects="json"> <url>{_:redirect-url-from-role($role)}</url> </json> ) else <json objects="json"> <error>Login failed.</error> </json> };
angular.module('login', []) .config([ '$routeProvider', function ($routeProvider) { $routeProvider.when('/Logout', { redirectTo: '/restxq/logout' }); }])
.controller('LoginCtrl', [ '$scope', '$http', '$location', function ($scope, $http, $location) { $scope.login = function() { var url = '/restxq/api/login/check';
//use jquery here, because angular does not detect auto-fill data var payload = { u: $('#u').val(), p: $('#p').val() };
$http.post(url, payload). success(function(data) { if (data.error) { // trigger error status $scope.loginForm.$error.failed = true; } else { // redirect window.location.pathname = data.url; } }). error(function(data, status, headers, config) { alert("Could not load data from server.") }); }; }]);
<div class="row" ng-controller="LoginCtrl"> <div class="span6 offset3"> <h2 class="alumni-name">Please Login</h2>
<div class="alert alert-error" ng-show="loginForm.$error.failed"> <p> Incorrect login data. Please try again. </p> </div> <form name="loginForm"> <table cellpadding="3"> <tr> <td>Username  </td> <td><input type="text" name="u" id="u" ng-model="user" style="width:146px !important;margin:0"/></td> </tr> <tr> <td>Password   </td> <td><input id="p" name="p" ng-model="password" style="width:146px !important;margin:0" type="password" /></td> </tr> <tr> <td/> <td><button ng-click="login()" class="btn" style="width:152px !important;margin:0">Login</button> </td> </tr> </table>
</form> </div> </div>
Regards,
Max
2014-05-07 1:38 GMT+02:00 Yoann Maingon yoann.maingon@mydatalinx.com:
Hi,
After xmlprague and my presentation at the BaseX user group I was told (and I agree) that it wasn't really smart to use php for what I was building as it had almost no added value as I could directly query basex using the Rest Interface.
Does anyone as some example using either angularjs or jquery ? I think I'm struggling with the login. Even just trying with a Rest test tool, I can see that I have error messages in Basex telling me theat access was refused.
Yoann Maingon CEO - mydatalinx +33664324966