I have a mobile application(mobile_users) and a backend to manage the system(system_users) i want to manage them in separate tables so basically to communicate with the mobile application i'm trying to use an api with JWT
enabled and for the backend i'm using default auth method in laravel.
My problem is when i try to change the auth table for JWT it keeps on accessing the system_users table, even though the documentation says you can change the default User model path i tried changing but no luck,
Please help me with this,
I'm using https://github.com/tymondesigns/jwt-auth this library for JWT,
My Database Structure will be,
mobile_users_table = authentication using JWT
system_users_table = default laravel auth
api.php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::post('auth', 'Api\v1\AuthController@authenticate');
Route::get('auth/me', 'Api\v1\AuthController@getAuthenticatedUser');
Route::get('validate', function () {
return "cool";
})->middleware('jwt.auth');
AppUser.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class AppUser extends Model
{
//
}
AuthController.php
namespace App\Http\Controllers\Api\v1;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
class AuthController extends Controller
{
public function authenticate(Request $request)
{
// grab credentials from the request
$credentials = $request->only('email', 'password');
try {
// attempt to verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}
// all good so return the token
return response()->json(compact('token'));
}
public function getAuthenticatedUser()
{
try {
if (! $user = JWTAuth::parseToken()->authenticate()) {
return response()->json(['user_not_found'], 404);
}
} catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
return response()->json(['token_expired'], $e->getStatusCode());
} catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
return response()->json(['token_invalid'], $e->getStatusCode());
} catch (Tymon\JWTAuth\Exceptions\JWTException $e) {
return response()->json(['token_absent'], $e->getStatusCode());
}
// the token is valid and we have found the user via the sub claim
return response()->json(compact('user'));
}
}
jwt.php
/*
|--------------------------------------------------------------------------
| User Model namespace
|--------------------------------------------------------------------------
|
| Specify the full namespace to your User model.
| e.g. 'Acme\Entities\User'
|
*/
'user' => 'App\AppUser',