Tuesday, March 21, 2017

Laravel multiple JWTAuth can be pass by another auth token

I have three Auth in my project

  • User
  • Admin
  • Hall

And each auth have a middleware to protected the api route, that should be login and get the token

//login
Route::get('login/hall','HallController@hallLogin');
Route::get('login/user','UserController@userLogin');
Route::get('login/admin','AdminController@adminLogin');

// Need Auth to get the data
Route::get('hall','HallController@show')->middleware('jwt.hall');
Route::get('user','UserController@show')->middleware('jwt.user');
Route::get('admin','AdminController@show')->middleware('jwt.admin');

Here is login part

public function userLogin(Request $request)
{
   $credentials = $request->only('email', 'password');

    if($token = JWTAuth::attempt($credentials)){
        return response()->json(ResponesFormat::make(true,'ok',['token' => $token]), 200);
    }
    return response()->json(ResponesFormat::make(false,'failed'), 403);
}

public function hallLogin(Request $request)
{
    $credentials = $request->only(['account','password']);

    Config::set('auth.providers.users.model', \Acme\Hall\Hall::class);

    if($token = JWTAuth::attempt($credentials)){

        return response()->json(ResponesFormat::make(true,'ok',['token' => $token]), 200);
    }
    return response()->json(ResponesFormat::make(false,'failed'), 403);
}


public function adminLogin(Request $request)
{
    $credentials = $request->only(['account','password']);

    Config::set('auth.providers.users.model', \Acme\Admin\Admin::class);

    if($token = JWTAuth::attempt($credentials)){
        return response()->json(ResponesFormat::make(true,'ok',['token' => $token]), 200);
    }
    return response()->json(ResponesFormat::make(false,'failed'), 403);
}

Here is the middleware:

namespace App\Http\Middleware\Auth;
use Config;
use Closure;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use Acme\Core\Helper\ResponesFormat;
use Tymon\JWTAuth\Middleware\BaseMiddleware;
class Hall extends BaseMiddleware
{
    public function handle($request, Closure $next)
    {
       // Using the config to change auth model
        Config::set('auth.providers.users.model', \Acme\Hall\Hall::class);
        if (! $token = $this->auth->setRequest($request)->getToken()) {

            return response()->json(ResponesFormat::make(false,'token_not_found'), 400);
        }

        try {

          $user = $this->auth->authenticate($token);
        } catch (TokenExpiredException $e) {
            return response()->json(ResponesFormat::make(false,'token_expired'), $e->getStatusCode());

        } catch (JWTException $e) {
            return response()->json(ResponesFormat::make(false,'token_invalid'), $e->getStatusCode());
        }

        if (! $user) {
            return response()->json(ResponesFormat::make(false,'user_not_found'), $e->getStatusCode());

        }

        $this->events->fire('tymon.jwt.valid', $user);

        return $next($request);
    }

}

The question is ,when user login and get the token to add on a hall auth only api that can success get the data, and hall to user or admin is the same, Any Idea?



via Fan

Advertisement