$data = array('user' => 'testuser', 'test' => 'hahahahha', 'static_url' => $assets, 'action' => $action, 'csrf_key' => $app->view()->getData('csrf_key'), 'csrf_token' => $app->view()->getData('csrf_token')); echo $twig->render('views/login.php', $data); }); $app->post('/app/login', function () use($app, $twig, $assets) { $check_user = $MongoUser->findOne(array('$and' => array(array('email' => $slim->request()->post('email')), array('password' => $slim->request()->post('password'))))); if ($check_user) { // set the session here if (!isset($_SESSION)) { session_start(); } # nice technique : http://stackoverflow.com/questions/19068363/storing-and-retrieving-an-array-in-a-php-cookie $dataArray = array('user' => $new_user, 'message' => "User created sucessfully"); $response = json_encode($dataArray); echo $response; } else { $app->halt(401, "Credentials incorrect or user dont exist."); } }); $app->post('/app/create', function () use($app, $twig, $assets, $MongoUser) { $_user = array('_id' => uniqid(), 'email' => $app->request()->post('email'), 'password' => $app->request()->post('password')); $new_user = $MongoUser->save($_user); if ($new_user) { // redirect to login page $dataArray = array('user' => $new_user, 'message' => "User created sucessfully"); //$response = json_encode($dataArray); //echo $response; if (!isset($_SESSION)) { session_start(); } $cookie_data = json_encode($new_user, true); $_SESSION['app_id'] = $cookie_data;