Example #1
0
 /**
  * Test get encrypted cookie WITHOUT deleting it
  *
  * This only tests that this method runs without error. The implementation of
  * fetching the encrypted cookie is tested separately.
  */
 public function testGetEncryptedCookieWithoutDeletingIt()
 {
     \Slim\Environment::mock(array('SCRIPT_NAME' => '/foo', 'PATH_INFO' => '/bar'));
     $s = new \Slim\Slim();
     $r = $s->response();
     $this->assertFalse($s->getEncryptedCookie('foo', false));
     $this->assertEquals(0, preg_match("@foo=;.*@", $r['Set-Cookie']));
 }
R::setup("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_password);
R::freeze(true);
$app = new \Slim\Slim(array('debug' => true, 'mode' => 'development', 'cookies.secret_key' => 'o_O o_o O_o', 'cookies.lifetime' => '30 minutes'));
// Globally ensure if `id` is used as a route parameter, it is numeric,
// so handlers do not have to check an `id` parameter before mapping it
// to a numeric `id` field on the Contacts database table.
\Slim\Route::setDefaultConditions(array('id' => '[0-9]{1,}'));
// stubbed for demo
function isValidLogin($username, $password)
{
    //    return true;
    return $username == 'demo' && $password == 'password';
}
$checkLoggedOn = function ($app) {
    return function () use($app) {
        if (!isValidLogin($app->getEncryptedCookie('username'), $app->getEncryptedCookie('password'))) {
            $app->halt(401);
            // Unauthorized access
        }
    };
};
$app->post('/login', function () use($app) {
    try {
        $username = $app->request()->post('username');
        $password = $app->request()->post('password');
        if (isValidLogin($username, $password)) {
            $app->setEncryptedCookie('username', $username, '1 day');
            $app->setEncryptedCookie('password', $password, '1 day');
            $app->response()->header('Content-Type', 'application/json');
            $app->response()->status(200);
            // OK