Search code examples
dartrikulo

Rikulo security client side login


I've been trying to create a Dart SPA using Rikulo security to log in. I have been unable to do so successfully. I found the following in the docs:

//If you'd like to login in an Ajax request, SOAP or others,             
//you can invoke this method directly by providing the username, password
//and, optional, rememberMe:                                             

    //prepare username, password, rememberMe from, say, Ajax           
    security.login(connect, username: username, password: password,    
      rememberMe: rememberMe, redirect: false);

My question is how do I return a success/fail login value from Rikulo security for use on the client side. An example would be extremely helpful. Thanks in advance.


Solution

  • I assume you'd like to log in over Ajax (and JSON). Basically, it is no different from handling Ajax -- you can refer to the Handle Post requests section here.

    import "dart:convert";
    import "package:rikulo_commons/convert.dart";
    
    Future login(HttpConnect connect) {
      return readAsJson(connect.request).then((Map<String, String> data) {
         return security.login(connect, redirect: false, 
            username: data["username"],
            password: data["password"]);
     })
     .then((_) {
        //login successfully
        response
          ..headers.contentType = contentTypes["json"]
          ..write(JSON.encode({"result": "success"}));
      })
      .catchError((ex) {
        if (ex is AuthenticationException) {
          // login fail
          response
            ..headers.contentType = contentTypes["json"]
            ..write(JSON.encode({'result': 'fail'}));
        } else {
          throw new Http404.fromConnect(connect);
        }
      });
    }