Skip to content

Instantly share code, notes, and snippets.

@pateketrueke
Created May 28, 2012 05:17
Show Gist options
  • Save pateketrueke/2817382 to your computer and use it in GitHub Desktop.
Save pateketrueke/2817382 to your computer and use it in GitHub Desktop.
Basic auth with tetl::PHP
<?php
require 'tetlphp/framework/initialize.php';
import('db');
import('www');
import('a_record');
run(function () {
class users extends mongo_model
{
static $columns = array(
'user' => array('type' => 'string'),
'pass' => array('type' => 'string'),
'salt' => array('type' => 'string'),
);
}
auth::config('model', 'users');
if ( ! users::count_by_user('admin')) {
auth::insert('admin', 'test');
}
get('/', function () {
if ( ! auth::is_logged()) {
$out = form::post(url_for::session(), function() {
echo form::field(array('type' => 'text', 'label' => 'Username', 'name' => 'user'));
echo form::field(array('type' => 'password', 'label' => 'Password', 'name' => 'pass'));
echo form::submit('done', 'OK');
});
} else {
$out = link_to('Exit', url_for::quit());
}
response($out);
});
post('/session', function () {
if (request::is_post()) {
if (auth::validate(request::post('user'), request::post('pass'))) {
redirect(ROOT);
}
$out = link_to('Try again?', url_for(ROOT));
response($out, array('status' => 404));
}
});
get('/quit', function () {
auth::logout();
redirect(ROOT);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment