Ejemplo n.º 1
0
 /**
  * This methods will be called at application startup
  * @param $appInstance
  * @return void
  */
 public static function addRouteDefinitions(Slim $appInstance)
 {
     $appInstance->get('/', function () {
         print '<h1>A simple frontend</h1>';
     });
     $appInstance->post("/login", function () use($appInstance) {
         $appInstance->response->headers->set('Cache-Control', 'no-store');
         if (isset($_POST['username']) && is_string($_POST['username']) && (isset($_POST['password']) && is_string($_POST['password']))) {
             try {
                 try {
                     $user = new MembersAuth();
                 } catch (SessionExpired $e) {
                     $user = new MembersAuth();
                 }
                 $user->userLogin($_POST['username'], $_POST['password']);
                 $appInstance->response->headers->set('Content-Type', 'application/json');
                 print json_encode($user->getSessionAuthData());
             } catch (LoginIncorrect $e) {
                 $appInstance->response->headers->set('Content-Type', 'text/plain');
                 $appInstance->response->setStatus(400);
                 print $e->getMessage();
             }
         } else {
             $appInstance->response->headers->set('Content-Type', 'text/plain');
             $appInstance->response->setStatus(400);
             print 'Bad request';
         }
     });
     $appInstance->map('/logout', function () use($appInstance) {
         try {
             $user = new MembersAuth();
             if ($user->isUserLoggedInSimple()) {
                 $user->logout();
             }
         } catch (SessionExpired $e) {
         }
     })->via('GET', 'POST');
 }