Skip to content

Instantly share code, notes, and snippets.

@s1037989
Created June 25, 2015 06:45
Show Gist options
  • Save s1037989/75c87092f41c9a977ac3 to your computer and use it in GitHub Desktop.
Save s1037989/75c87092f41c9a977ac3 to your computer and use it in GitHub Desktop.
Mojo app that logsin via oauth2
use Mojolicious::Lite;
use experimental 'signatures';
plugin "OAuth2" => {
fix_get_token => 1,
facebook => {
key => $ENV{OAUTH_KEY},
secret => $ENV{OAUTH_SECRET},
},
};
get "/connect" => sub ($c) {
$c->delay(
sub {
my $delay = shift;
my $args = {redirect_uri => $c->url_for('connect')->userinfo(undef)->to_abs, scope => "user_about_me email"};
$c->oauth2->get_token(facebook => $args, $delay->begin);
},
sub {
my ($delay, $err, $data) = @_;
return $c->render("connect", error => $err) unless $data->{access_token};
$c->session(token => $data->{access_token});
$c->redirect_to('profile');
},
);
};
get '/reset' => sub ($c) { $c->session(expires=>1) };
get '/profile' => sub ($c) {
$c->render_later;
return $c->redirect_to('connect') unless $c->session('token');
$c->ua->get("https://graph.facebook.com/v2.3/me?access_token=".$c->session('token'), sub {
my ($ua, $tx) = @_;
$c->session(email => $tx->res->json->{email});
$c->render(profile => $tx->res->json);
});
};
get '/email' => sub ($c) { $c->redirect_to('connect') unless $c->session('email') };
app->start;
__DATA__
@@ email.html.ep
%= session 'email'
@@ profile.html.ep
Name: <%= $profile->{name} %><br />
Email: <%= $profile->{email} %><br />
JSON: <%= dumper $profile %>
@@ connect.html.ep
Error:
%= $error
@@ reset.html.ep
Reset:
%= session 'token'
%= session 'email'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment