rluders / wn-jwtauth-plugin

JWTAuth Plugin for WinterCMS
GNU General Public License v3.0
29 stars 28 forks source link

get user object and check the user without middleware #25

Closed omidmm closed 5 years ago

omidmm commented 5 years ago

Hi Recardo , thank you for useful plugin, but I have some question about this plugin. I want to check the user is login Or return the user object by static method without using middleware, I can use Auth::check but I want find by token ,

ex: class Post extends Controller public function review(){ if(JwtAuth::check){ return [ 'user'=>JwtAuth::user()]; }else{ return ['message'=> "you must register Or login to site']; } }

Thanks

rluders commented 5 years ago

Hello, @omidmm

Well, first at all you need to pass the token somehow to your endpoint. So, let me try to write a quick example here (that I'll not test 'cause I do not have time right now).

class GetUserByTokenController extends Controller
{
    public function __invoke(
        JWTAuth $auth,
        Request $request
    ) {
        $token = $request->get('token');
        $auth->setToken($token);

        return $auth->check() ? ['user' => $auth->user()] : ['message' => 'You must register or login to site'];
    }
}

I think that you will use something like this. It's always useful to check how I did the endpoints for the plugin, so you can grab some ideas:

https://github.com/rluders/oc-jwtauth-plugin/tree/master/http/controllers

And, please, let me know if it helped you, or if you found another solution.