Skip to content

Commit

Permalink
Various changes. Largely around adding the user registration section.
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Oct 12, 2012
1 parent 875b056 commit 4b29ae4
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 55 deletions.
4 changes: 4 additions & 0 deletions Shrtr/config.yml
Expand Up @@ -35,3 +35,7 @@ plugins:
user: shrtr
pass: shorty

session: cookie
session_cookie_key: "random test session key"

logger: "console"
100 changes: 99 additions & 1 deletion Shrtr/lib/Shrtr.pm
@@ -1,17 +1,111 @@
package Shrtr;
use Dancer ':syntax';
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Passphrase;

our $VERSION = '0.1';

my $url_rs = schema->resultset('Url');
my $user_rs = schema->resultset('User');

my %private = map { $_ => 1 } qw[/submit];

hook before => sub {
if ($private{request->path_info} and ! session('user')) {
session 'goto' => request->path_info;
request->path_info('/login');
}
};

hook before_template => sub {
my $params = shift;
$params->{user} = session('user');
};

get '/' => sub {
template 'index';
};

get '/register' => sub {
template 'register';
my $error = session('error');
session 'error' => undef;
template 'register', {
error => $error,
};
};

post '/register' => sub {
session 'username' => param('username');
session 'email' => param('email');
unless (param('username') and param('email')
and param('password') and param('password2')) {

session 'error' => 'You must fill in all values';
return redirect '/register';
}

unless (param('password') eq param('password2')) {
session 'error' => 'Password values are not the same';
return redirect '/register';
}

if (my $user = $user_rs->single({
username => param('username'),
})) {
session 'error' => 'Username ' . $user->username .
' is already in use.';
return redirect '/register';
}

my $user = $user_rs->create({
username => param('username'),
email => param('email'),
password => passphrase(param('password'))->generate_hash,
});

template 'registered', { user => $user };
};

get '/login' => sub {
my $error = session('error');
session 'error' => undef;
template 'login', { error => $error };
};

post '/login' => sub {
session 'user' => undef;
session 'username' => params->{username};
unless (params->{username} and params->{pass}) {
session 'error' => 'Must give both username and password';
return redirect '/login';
}

my $user = $user_rs->single({
username => params->{username},
});
unless ($user) {
session 'error' => 'Invalid username or password';
return redirect '/login';
}

unless (passphrase(params->{pass})->matches($user->password)) {
session 'error' => 'Invalid username or password';
return redirect '/login';
}

session 'user' => $user->username;

if (my $goto = session('goto')) {
session 'goto' => undef;
redirect $goto;
} else {
redirect '/';
}
};

get '/logout' => sub {
session 'user' => undef;
redirect '/';
};

get '/submit' => sub {
Expand All @@ -20,6 +114,10 @@ get '/submit' => sub {

post '/submit' => sub {
if (my $url = param('url') and my $code = param('code')) {
unless ($url =~ m[^https?://]) {
$url = "http://$url";
}

my $new_url = $url_rs->create({
url => $url,
code => $code,
Expand Down
9 changes: 9 additions & 0 deletions Shrtr/views/frame.tt
Expand Up @@ -13,6 +13,14 @@ html, body {
min-height: 100%;
}

#id {
position: absolute;
left: 0;
top: 0;
height: 30px;
width: 100%;
}

#content-frame {
margin-top: 30px;
width: 100%;
Expand All @@ -25,6 +33,7 @@ html, body {
</style>
</head>
<body>
<div id="bar">Shrtr: [% url.url %]</div>
<iframe id="content-frame" frameborder="0" noresize="noresize" src="[% url.url %]" name="content-frame"></iframe>
</body>
</html>
72 changes: 20 additions & 52 deletions Shrtr/views/index.tt
@@ -1,66 +1,34 @@
<div id="page">
<div id="sidebar">
<ul id="sidebar-items">
<li>
<h3>Shortcuts</h3>
<ul class="links">
<li><a href="/register">Register as a user</a></li>
<li><a href="/login">Login</a></li>
<li><a href="/submit">Submit a URL</a></li>
</ul>
</li>

<h3>More Details</h3>
<ul class="links">
<li><a href="/about">About Shrtr</a></li>
<li><a href="/about/team">About the Team</a></li>
</ul>
</li>

<li>
<h3>Blogroll</h3>

<ul class="links">
<li><a href="">A blog</a></li>
<li><a href="">Another blog</a></li>
</ul>
</li>
</ul>
</div>

<div id="content">
<div id="header">
<h1>Shrtr</h1>
<h2>URL shortening without the Googlejuice</h2>
</div>

<div id="getting-started">
<h1>Getting started</h1>
<h2>Here&rsquo;s how to get started:</h2>
<div id="getting-started">
<h1>Getting started</h1>
<h2>Here&rsquo;s how to get started:</h2>

<ol>
<li>
<h2>Register as a user</h2>
<ol>
<li>
<h2>Register as a user</h2>

<p>Only registered users can use Shrtr's URL shortening
service. You can <a href="/register">register here</a>.</p>
</li>
<p>Only registered users can use Shrtr's URL shortening
service. You can <a href="/register">register here</a>.</p>
</li>

<li>
<h2>Submit URLs</h2>
<li>
<h2>Submit URLs</h2>

<p>Once you're registered and logged in, you can
<a href="/submit">submit a URL</a>.</p>
</li>
<p>Once you're registered and logged in, you can
<a href="/submit">submit a URL</a>.</p>
</li>

<li>
<h2>Profit!</h2>
<li>
<h2>Profit!</h2>

<p>Or, more accurately, stop other people profiting from
you linking to them.</p>
</li>
<p>Or, more accurately, stop other people profiting from
you linking to them.</p>
</li>

</ol>
</div>
</div>
</ol>
</div>
47 changes: 46 additions & 1 deletion Shrtr/views/layouts/main.tt
@@ -1,12 +1,57 @@
<!DOCTYPE html>
<html>
[% USE Dumper -%]
<!-- [% Dumper.dump(session) %] -->
<head>
<meta http-equiv="Content-type" content="text/html; charset=[% settings.charset %]" />
<title>Shrtr</title>
<link rel="stylesheet" href="[% request.uri_base %]/css/style.css" />
</head>
<body>
[% content %]
<div id="page">
<div id="sidebar">
<ul id="sidebar-items">
<li>
[% IF user -%]
<p>Welcome <b>[% user %]</b></p>
[% END %]
<h3>Shortcuts</h3>
<ul class="links">
<li><a href="/">Home</a>
[% IF user -%]
<li><a href="/user/[% user %]">View your profile</a></li>
<li><a href="/submit">Submit a URL</a></li>
<li><a href="/logout"/>Log out</a></li>
[% ELSE -%]
<li><a href="/register">Register as a user</a></li>
<li><a href="/login">Login</a></li>
[% END -%]
</ul>
</li>

<li>
<h3>More Details</h3>
<ul class="links">
<li><a href="/about">About Shrtr</a></li>
<li><a href="/about/team">About the Team</a></li>
</ul>
</li>

<li>
<h3>Blogroll</h3>

<ul class="links">
<li><a href="">A blog</a></li>
<li><a href="">Another blog</a></li>
</ul>
</li>
</ul>
</div>

<div id="content">
[% content %]
</div>
</div>
<div id="footer">
Powered by <a href="http://perldancer.org/">Dancer</a> [% dancer_version %]
</div>
Expand Down
12 changes: 12 additions & 0 deletions Shrtr/views/login.tt
@@ -0,0 +1,12 @@
[% IF error -%]
<p class="error">[% error %]</p>
[% END -%]
[% IF session.goto -%]
<p>You need to be logged in to do that</p>
[% END -%]
<form method="POST" action="/login">
<p>User: <input name="username" value="[% session.username %]" /><br />
Password: <input type="password" name="pass" /><br />
<input type="submit" value="Log in" />
</form>

14 changes: 14 additions & 0 deletions Shrtr/views/register.tt
@@ -0,0 +1,14 @@
[% IF error -%]
<p class="error">[% error %]</p>
[% END -%]
<form method="POST">
<p>Username: <input name="username" value="[% session.username %]" /><br />
Email address: <input name="email" value="[% session.email %]" /><br />
Password: <input type="password" name="password" /><br />
Password (again): <input type="password" name="password2" /><br />
<input type="submit" value="Register" /></p>
</form>
[% USE Dumper %]
<pre>
[% Dumper.dump(session) -%]
</pre>
1 change: 1 addition & 0 deletions Shrtr/views/registered.tt
@@ -0,0 +1 @@
<p>User [% user.username %] created successfully.</p>
2 changes: 1 addition & 1 deletion Shrtr/views/url.tt
@@ -1,3 +1,3 @@
<p><b>Code:</b> [% url.code %]</br>
<p><b>Code:</b> <a href="/[% url.code %]">[% url.code %]</a></br>
<b>URL:</b> [% url.url %]</br>
<b>Clicks:</b> [% url.clicks.size %]</p>

0 comments on commit 4b29ae4

Please sign in to comment.