preaction / Yancy

The Best Web Framework Deserves the Best Content Management System
http://preaction.me/yancy/
Other
54 stars 21 forks source link

How to make logout (yancy->auth->clear) #91

Closed pavelsr closed 4 years ago

pavelsr commented 4 years ago

Since nowhere in docs mention what is reserved session key for storing current user (I assume it's always session->{yancy}{auth} but still ) I think it could be useful to add into Yancy::Plugin::Auth section how to properly logout. I assume that it's always possible to do with delete $c->session->{yancy}{auth}, something like

get '/logout' => sub {
  my ( $c ) = @_;
  delete $c->session->{yancy}{auth};
  $c->redirect_to('/');
};

But maybe there is some public method to do logout ? Or should we add it?

Searching by code I've found that you made logout in Yancy::Plugin::Auth::Basic like

sub _get_logout {
    my ( $c ) = @_;
    $c->yancy->auth->clear;
    $c->flash( info => 'Logged out' );
    return $c->render( 'yancy/auth/basic/login' );
}

But when I tried to do similar in my app

get '/logout' => sub {
  my ( $c ) = @_;
  $c->yancy->auth->clear;  # or app->yancy->auth->clear
  $c->redirect_to('/');
};

I've got an error, Can't locate object method "clear" via package "Mojolicious::Renderer::Helpers::4d3bfb8b02efde8b1bbef4956167936a"

preaction commented 4 years ago

Auth::Basic is deprecated and not used by the main auth plugin. You want to read Yancy::Plugin::Auth::Password. I will add some documentation saying where to get the logout route for that plugin (the route's name is yancy.auth.password.logout). I will also add a generic logout method and route to the main plugin so folks can do what they wish. Thanks for finding this issue!