Lravel 5.4: JWT API with multi-auth on two tables one works the other not - php

I am using...
Laravel 5.4
tymon/jwt-auth : 1.0.0-rc.2
I have application with two authentications API one is customers and the other is drivers each one has it's own table.
now let me describe shortly JWT package installation and the updates I did on it.
I installed the package as described in the JWT documents exactly.
Now comes to the quick start here I updated two Models one is the User and the second Driver.
Comes here to the Configure Auth guard again I used the configuration for the two guards let me show a snapshot of my auth.php.
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
'driver' => [
'driver' => 'session',
'provider' => 'drivers',
],
'driver-api' => [
'driver' => 'jwt',
'provider' => 'drivers',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'drivers' => [
'driver' => 'eloquent',
'model' => App\Models\Driver::class,
],
],
Now continue the application with authentication routes here is my Routes for the two Models
Here is the User and Driver Routes
Route::group( [
'prefix' => 'auth',
'middleware' => 'api'
], function () {
.......
});
Route::group( [
'prefix' => 'driver',
'middleware' => 'api'
], function () {
.......
});
Now comes the AuthController
in the JWT documentation the construct is writing like that.
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
I found some article that suggest to make it something like this to switch between the two models we have.
so here with my controller looks like now.
public function __construct() {
$this->user = new User;
$this->driver = new Driver;
}
public function userLogin( Request $request ) {
Config::set( 'jwt.user', 'App\Models\User' );
Config::set( 'auth.providers.users.model', User::class );
$credentials = $request->only( 'email', 'password' );
$token = null;
try {
if ( $token = $this->guard()->attempt( $credentials ) ) {
return response()->json( [
'response' => 'error',
'message' => 'invalid_email_or_password',
] );
}
} catch ( JWTAuthException $e ) {
return response()->json( [
'response' => 'error',
'message' => 'failed_to_create_token',
] );
}
return response()->json( [
'response' => 'success',
'result' => [
'token' => $token,
'message' => 'I am front user',
],
] );
}
public function driverLogin( Request $request ) {
Config::set( 'jwt.user', 'App\Models\Driver' );
Config::set( 'auth.providers.users.model', Driver::class );
$credentials = $request->only( 'email', 'password' );
$token = null;
try {
if ( ! $token = $this->guard()->attempt( $credentials ) ) {
return response()->json( [
'response' => 'error',
'message' => 'invalid_email_or_password',
] );
}
} catch ( JWTAuthException $e ) {
return response()->json( [
'response' => 'error',
'message' => 'failed_to_create_token',
] );
}
return response()->json( [
'response' => 'success',
'result' => [
'token' => $token,
'message' => 'I am driver user',
],
] );
}
public function me() {
return response()->json( $this->guard()->user() );
}
public function logout() {
$this->guard()->logout();
return response()->json( [ 'message' => 'Successfully logged out' ] );
}
public function refresh() {
return $this->respondWithToken( $this->guard()->refresh() );
}
protected function respondWithToken( $token ) {
return response()->json( [
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => $this->guard()->factory()->getTTL() * 60
] );
}
public function guard() {
return Auth::guard();
}
Now with is happening and the problems I faced
Now the driver api is working as login only Ex.
localhost:8000/api/driver/login Working fine
but when try to get the driver user id like this
localhost:8000/api/driver/me it return empty array
Second Issue comes.
the use login from the interface for Ex. http://localhost:8000/login it returns back to the login screen without any errors becouse the login information is right but the defaults in the auth.php is 'guard'=>'api' if I change it to 'guard'=>'web' it do the login correctly.
even the User API for Ex. localhost:8000/api/auth/login always return
{
"response": "error",
"message": "invalid_email_or_password"
}
Update
I solved half the way
I updated the AuthController to be something like this.
public function __construct() {
if ( Request()->url() == '/api/driver/me' ) {
$this->middleware( 'auth:driver-api', [ 'except' => [ 'login' ] ] );
} elseif ( Request()->url() == '/api/customer/me' ) {
$this->middleware( 'auth:api', [ 'except' => [ 'login' ] ] );
}
}
and the login function to be something like this.
public function login() {
if ( Request()->url() == '/api/driver' ) {
Config::set( 'auth.providers.users.model', Driver::class );
$credentials = request( [ 'email', 'password' ] );
if ( ! $token = auth()->attempt( $credentials ) ) {
return response()->json( [ 'error' => 'Unauthorized' ], 401 );
}
return $this->respondWithToken( $token );
}
Config::set( 'auth.providers.users.model', User::class );
$credentials = request( [ 'email', 'password' ] );
if ( ! $token = auth()->attempt( $credentials ) ) {
return response()->json( [ 'error' => 'Unauthorized' ], 401 );
}
return $this->respondWithToken( $token );
}
but still have problem in auth.php here it is
'defaults' => [
'guard' => 'driver-api',
'passwords' => 'users',
],
here I need to switch the 'guard'=>'api' to be 'guard'=>'driver-api' in case if URL request is localhost:8000/api/driver/login and 'guard'=>'api' in case if URL request is localhost:8000/api/customer/login any way to do this.
Update 2
Here is the driver Model
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Driver extends Authenticatable implements JWTSubject {
protected $guard = 'driver';
protected $fillable = [
...
'email',
'password',
...
];
protected $hidden = [
'password',
'remember_token',
];
public function getJWTIdentifier() {
return $this->getKey();
}
public function getJWTCustomClaims() {
return [];
}
}
and the User Model
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements JWTSubject {
use Notifiable;
protected $fillable = [
'name',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
public function getJWTIdentifier() {
return $this->getKey();
}
public function getJWTCustomClaims() {
return [];
}
I need some help, Ideas please.

There is no need to change the providers in config/auth.php.
You can change the __construct function in each of your controllers as follows. So that jwt know which model to authenticate.
DriverController
function __construct()
{
Config::set('jwt.user', Driver::class);
Config::set('auth.providers', ['users' => [
'driver' => 'eloquent',
'model' => Driver::class,
]]);
}

My example when i used multi auth with jwt
I have 2 models :
1. users
2. admins
the routes :
Route::post('auth/userlogin', 'ApiController#userLogin');
Route::post('auth/adminlogin', 'ApiController#adminLogin');
the controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Config;
use JWTAuth;
use JWTAuthException;
use App\User;
use App\Admin;
class ApiController extends Controller
{
public function __construct()
{
$this->user = new User;
$this->admin = new Admin;
}
public function userLogin(Request $request){
Config::set('jwt.user', 'App\User');
Config::set('auth.providers.users.model', \App\User::class);
$credentials = $request->only('email', 'password');
$token = null;
try {
if (!$token = JWTAuth::attempt($credentials)) {
return response()->json([
'response' => 'error',
'message' => 'invalid_email_or_password',
]);
}
} catch (JWTAuthException $e) {
return response()->json([
'response' => 'error',
'message' => 'failed_to_create_token',
]);
}
return response()->json([
'response' => 'success',
'result' => [
'token' => $token,
'message' => 'I am front user',
],
]);
}
public function adminLogin(Request $request){
Config::set('jwt.user', 'App\Admin');
Config::set('auth.providers.users.model', \App\Admin::class);
$credentials = $request->only('email', 'password');
$token = null;
try {
if (!$token = JWTAuth::attempt($credentials)) {
return response()->json([
'response' => 'error',
'message' => 'invalid_email_or_password',
]);
}
} catch (JWTAuthException $e) {
return response()->json([
'response' => 'error',
'message' => 'failed_to_create_token',
]);
}
return response()->json([
'response' => 'success',
'result' => [
'token' => $token,
'message' => 'I am Admin user',
],
]);
}
}
I hope that's help you .

First let me thank you #AmrAbdelRahman for you efforts and your time.
My problem was the application always using my default authentication "guard" as my default looks like that
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
so every time I try to authenticate the other user which was the driver it fails during the default authenticate is api and the it should using driver in this case.
what I did in my case was making a switcher in my App\Providers\AppServiceProvider under the boot here is how it looks like
$this->app['router']->matched(function (\Illuminate\Routing\Events\RouteMatched $e) {
$route = $e->route;
if (!array_has($route->getAction(), 'guard')) {
return;
}
$routeGuard = array_get($route->getAction(), 'guard');
$this->app['auth']->resolveUsersUsing(function ($guard = null) use ($routeGuard) {
return $this->app['auth']->guard($routeGuard)->user();
});
$this->app['auth']->setDefaultDriver($routeGuard);
});
Now in my case if the $guard =api it will read that guard and act correctly and if it's driver it will use the new guard and act as expected. Hope this will help some one in future.

Related

Laravel 8 Passport - Multi Auth setup

I am trying to setup Passport in Laravel 8 with two guards, but keep running into issues. I am using Postman to test.
I have two tables setup:
Users
Contacts
I can successfully register a user in both tables. However I can only authenticate and retrieve a token in the login method for the users table. I keep getting "Invalid Credentials" on contacts table. I am pretty sure the reason for this is because it's looking at the users table and not the contacts table when trying to authenticate the user. I think I am missing something in the setup process to allow the use of different tables when authenticating.
My codes is as follows:
auth.php
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
'hash' => false,
],
'api-crm' => [
'driver' => 'passport',
'provider' => 'contacts',
'hash' => false,
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'contacts' => [
'driver' => 'eloquent',
'model' => App\Models\Contact::class,
],
],
api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::post("/register", [ApiAuthController::class, 'register']);
Route::post("/login", [ApiAuthController::class, 'login']);
Route::post("/crm/register", [CrmAuthController::class, 'register']);
Route::post("/crm/login", [CrmAuthController::class, 'login']);
ApiAuthController.php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
class ApiAuthController extends Controller
{
public function register(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|max:55',
'email' => 'email|required|unique:users',
'password' => 'required|confirmed'
]);
$validatedData['password'] = bcrypt($request->password);
$user = User::create($validatedData);
$accessToken = $user->createToken('authToken')->accessToken;
return response([ 'user' => $user, 'access_token' => $accessToken]);
}
public function login(Request $request)
{
$loginData = $request->validate([
'email' => 'email|required',
'password' => 'required'
]);
if (!auth()->attempt($loginData)) {
return response(['message' => 'Invalid Credentials']);
}
$accessToken = auth()->user()->createToken('authToken')->accessToken;
return response(['user' => auth()->user(), 'access_token' => $accessToken]);
}
}
CrmAuthController.php
namespace App\Http\Controllers\CRM;
use App\Http\Controllers\Controller;
use App\Models\Contact;
use Illuminate\Http\Request;
class CrmAuthController extends Controller
{
public function register(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|max:55',
'email' => 'email|required|unique:users',
'password' => 'required|confirmed'
]);
$validatedData['password'] = bcrypt($request->password);
$user = Contact::create($validatedData);
$accessToken = $user->createToken('authToken')->accessToken;
return response([ 'user' => $user, 'access_token' => $accessToken]);
}
public function login(Request $request)
{
$loginData = $request->validate([
'email' => 'email|required',
'password' => 'required'
]);
if (!auth()->attempt($loginData)) {
return response(['message' => 'Invalid Credentials']);
}
$accessToken = auth()->user()->createToken('authToken')->accessToken;
return response(['user' => auth()->user(), 'access_token' => $accessToken]);
}
}
In crmAuthController.php login method, when you use auth()->attempt($loginData) it looks to validate login data on default users table.
so instead of using the attempt($loginData) you have to get crm user by email using "Contact" Model in your case.
$loginData = $request->validate([
'email' => 'email|required',
'password' => 'required'
]);
$user = new \App\Models\Contact();
$check = $user->where('email',$loginData['email'])->exists();
if($check){
$users = $user->where('email',$loginData['email'])->first();
// verify the password
if (password_verify($loginData['password'],$users->password)) {
// Authentication passed...
$token = $users->createToken('YOUR TOKEN NAME');
return response($token);
}
else return response(['message' => 'Invalid Credentials']);
}
else return response(['message' => 'user doesnt exist with this email']);
Also once you logged in, to get the current user for CRM, use
Auth::guard('api-crm')->user();

AuthenticationException when create two AuthController in Laravel 7

I created a Laravel App. It should can authenticated via web and api.
Doing so, I need two Auth Controller. One is automatically created using laravel/ui scaffolding, for the web. And the other is created manually for user's login via token.
Here is the API/AuthController
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\User;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
class AuthController extends Controller
{
//untuk login
public function login(Request $request)
{
//validasi data
$this->validate($request, [
'email' => 'email',
'username' => 'string',
'password' => 'required'
]);
//login dapat menggunakan email atau username
$user = User::where('email', '=', $request->email)
->orWhere('username', '=', $request->username)->first();
// $username = User::where('username', $request->username)->first();
// dd($username);
$status = "error";
$message = "";
$data = null;
$code = 401;
// echo (gettype($email));
// echo(gettype($username));
// echo($email);
if($user){
if (Hash::check($request->password, $user->password)){
$user->generateToken(); //generated 60 random string
$status = 'success';
$message = 'Login Success';
//tampilkan data user menggunakan method to Array
$data = $user->toArray();
$code = 200;
}
else{
$message = "Login gagal, password salah";
}
}
else {
$message = "Login gagal, username atau email salah";
}
return response()->json([
'status' => $status,
'message' => $message,
'data' => $data,
], $code);
}
//untuk registrasi
public function register(Request $request)
{
$validator = Validator::make($request->all(),
[
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6',
'username' => 'string'
]);
if ($validator->fails()){
$errors = $validator->errors();
return response()->json([
'data' => [
'message' => $errors,
]
],400);
}
else{
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
'username' => $request->username,
'roles' => json_encode(['CUSTOMER'])
]);
if ($user){
$user->generateToken();
$status = "success";
$message = "Register berhasil!";
$data = $user->toArray();
$code = 200;
}
else{
$message = "Register gagal";
}
return response()->json([
'status' => $status,
'message' => $message,
'data' => $data
], $code);
}
}
//untuk logout
public function logout(Request $request)
{
//get authenticated user data
$user = Auth::guard('api')->user();
if($user){
$user->api_token = null; //delete user's token
$user->save();
}
return response()->json([
'status' => 'success',
'message' => 'logout success,
'data' => null,
], 200);
}
}
Login and Register method is worked. But when i try to run Logout method, i get AuthenticationException. I tried to debug the $user variable at Logout method, it return null. So, that's why i get the error. My question is, how i solve it? Is it even possible to create two different authentication controller in laravel?
Route api.php
//public
Route::prefix('v1')->group(function (){
Route::post('login', 'API\AuthController#login');
Route::post('register', 'API\AuthController#register');
//private
Route::middleware('auth:api')->group(function (){
Route::post('logout', 'API\AuthController#logout');
});
});
Logout method response:
{
"status": "error",
"message": "Unauthenticated.",
"data": null,
"errors": {
"exception": "Illuminate\\Auth\\AuthenticationException",
"trace": [
"#0 D:\\xampp\\htdocs\\book-store\\vendor\\laravel\\framework\\src\\Illuminate\\Auth\\Middleware\\Authenticate.php(68): Illuminate\\Auth\\Middleware\\Authenticate->unauthenticated(Object(Illuminate\\Http\\Request), Array)"
]
}
}
config\auth.php
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
*Note: default guard in config\auth.php is web so i can log in to the web too.

Angular Laravel JWT get authenticated user

I'm developing an Angular + Laravel app. I did a register-login with JWT and it works fine, I get the token and I store it in localStorage, the log out works too. But I have a problem with identifying the logged user. The me function is always returning a null json, why ? I searched for a solution online but nothing helped
AuthController
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login','register']]);
}
public function login()
{
$credentials = request(['email', 'password']);
if (! $token = Auth::guard()->attempt($credentials)) {
return response()->json(['error' => 'Email or password doesn\'t exits.'], 401);
}
return $this->respondWithToken($token);
}
public function register(RegisterRequest $request)
{
User::create($request->all());
return $this->login($request);
}
public function me()
{
return response()->json(Auth::guard()->user());
}
public function logout()
{
Auth::guard()->logout();
return response()->json(['message' => 'Successfully logged out']);
}
public function refresh()
{
return $this->respondWithToken(Auth::guard()->refresh());
}
protected function respondWithToken($token)
{
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => Auth::guard()->factory()->getTTL() * 60,
'user' => Auth::guard()->user()->name
]);
}
config/auth
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
'hash' => false,
],
],
You are excluding me method from the auth:api middleware in your controllers constructor;
Change this:
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login','register','me']]);
}
to this:
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login','register']]);
}
You're not use default guard ,
so you should select api guard
with auth('api')
or auth()->guard('api')
and me function need authorization

Laravel API Authentication - Passport Token

I'm building an API using Laravel. For authentication and security I am using Passport: https://laravel.com/docs/5.7/passport
I followed all the steps in the documentation. I am working with several profiles that pass through authentication, and I came across a problem, the token used by a middleware can be applied in other middleware.
In my config / auth.php file it looks like this:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'user' => [
'driver' => 'passport',
'provider' => 'users',
],
'producer' => [
'driver' => 'passport',
'provider' => 'producers',
],
'coordinator' => [
'driver' => 'passport',
'provider' => 'coordinators',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'producers' => [
'driver' => 'eloquent',
'model' => App\Producer::class,
],
'coordinators' => [
'driver' => 'eloquent',
'model' => App\Coordinator::class,
],
],
The Coordinator model looks like this:
class Coordinator extends Authenticatable{
use HasApiTokens, Notifiable;
protected $table = 'coordinators';
protected $guard = 'coordinator';
protected $fillable = [
'coordinator_name', 'email', 'password', 'cpf_cnpj', 'phone'
];
protected $hidden = [
'password',
];
public function events(){
return $this->belongsToMany('App\Event')->using('App\EventCoordinator');
}}
And the Model Producer looks like this:
class Producer extends Authenticatable{
use HasApiTokens, Notifiable;
protected $guard = 'producer';
protected $fillable = [
'name', 'email', 'password', 'cpf_cnpj', 'phone', 'street', 'neighborhood', 'city', 'state', 'number', 'zipcode', 'complement'
];
protected $table = 'producers';
protected $hidden = [
'password',
];
public function events(){
return $this->hasMany('App\Event');
}}
On the routes I'm using the middleware set in auth.php
Route::middleware('auth:producer')->group(function() {
Route::get('events', 'ProducerController#events');
});
Route::middleware('auth:coordinator')->group(function() {
Route::get('events', 'CoordinatorController#events');
});
And finally the events method in the CoordinatorController looks like this:
public function events(){
try{
if(Auth::guard('coordinator')->check()){
$events = Auth::user()->events;
return response()->json(['events' => $events], 200);
}else{
return response()->json(['error' => ['message' => 'Usuário não autenticado.']], 421);
}
}catch(\Exception $err){
return response()->json(['error' => ['code' => $err->getCode(), 'message' => $err->getMessage()]], 400);
}
}
and in ProducerController:
public function events(){
try{
try{
if(Auth::guard('producer')->check()){
$events = Auth::user()->events;
return response()->json(['events' => $events], 200);
}else{
return response()->json(['error' => ['message' => 'Usuário não autenticado.']], 421);
}
}catch(\Exception $err){
return response()->json(['error' => ['code' => $err->getCode(), 'message' => $err->getMessage()]], 400);
}
}
I am using Postman for testing, and when I use the Producer token to access the Coordinator method it works even though using different middleware in the route. Can anyone help? Something is missing?
as far as I know, passport didn't support MultiAuth
you can use widely available 3rd party multiauth support for laravel, like this package.

Handeling hasTooManyLoginAttempts in laravel?

The user has surpassed their alloed maximum of login attempts will key this by the username and the IP address of the client making,I use trait AuthenticatesUsers pulled in.
you look inside of mentioned trait, you will see another trait ThrottlesLogins pulled in.
Auth congfig:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
'admin-web' => [
'driver' => 'session',
'provider' => 'admins',
],
'admin-api' => [
'driver' => 'passport',
'provider' => 'admins',
],
],
Authcontroller:
class AuthController extends Controller
{
use ThrottlesLogins;
public function login(Request $request)
{
$method = __FUNCTION__;
//set validations
$validator = Validator::make($request->all(), [
'email' => 'required|string|email',
'password' => 'required|string|min:6',
]);
if ($validator->fails()) {
return (new FailedServerResponse($this->controller, $method, $this->errorType['validation'], $validator->errors()))->show();
}
$admin = Admin::where('email', $request->email)->first();
if ( $this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if (Auth::guard('admin-web')->attempt(['email' => $request->email, 'password' => $request->password, 'is_active' => 1], true)) {
try {
$token = $admin->createToken('register admin')->accessToken;
} catch (\Exception $e) {
return (new FailedServerResponse($this->controller, $method, $this->errorType['token']))->show();
}
return $token;
//success and everything is ok
$extra = ['token' => $token, 'is_register' => true];
return (new UserResponse($admin->load('userActivities', 'addresses.city.province', 'wallets', 'userGalleries'), $actionName, $extra))->withPrimaryLayout();
} else {
return (new FailedServerResponse($this->controller, $method, $this->errorType['notFound']))->show();
}
}
protected function hasTooManyLoginAttempts(Request $request)
{
$attempts = 2;
$lockoutMinites = 10;
return $this->limiter()->tooManyAttempts(
$this->throttleKey($request), $attempts, $lockoutMinites
);
}
hasTooManyLoginAttempts not working. can you help me?
Maybe the problem is that
$this->incrementLoginAttempts($request);
If the login attempt was unsuccessful we will increment the number of attempts to login and redirect the user back to the login form.

Categories