I have already multi auth system in my project, one for the user and the other for admin, and here are my guards
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => admin_auth',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'admin_auth' => [
'driver' => 'eloquent',
'model' => App\Models\admins\admin::class,
],
],
and I would like to have multi auth for API using sanctum because I only do it for one auth which is the user
here is my code to create a token
// Validate the form data
$this->validate($request, [
'phone_number' => 'required|numeric',
'password' => 'required',
//'device_name' => 'required', for mobile app //need to be placed in token name
]);
$user = User::where('phone_number', $request->input('phone_number'))->first();
// Check password
if(!$user || !Hash::check($request->input('password'), $user->password)) {
return response([
'message' => 'The phone number or passowrd is not correct'
], 401);
}
$roles = $user->getRoleNames()->toArray();
$token = $user->createToken('mymobile_token', $roles)->plainTextToken;
so how to create another auth for the admin?
Related
I have created an authentication with JWT in custom table. Authentication works fine, i am able to login and token is generated with success. The problem is not being able to get the information like 'id' etc FROM the generated TOKEN of the custom table.
$token = $request->header('Authorization');
$user = JWTAuth::toUser($token);
return $user;
Returned information are form the 'user' table not from the custom table used for the jwt authenticaiton.
My configration looks like
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'partners_credentials' => [
'driver' => 'eloquent',
'table' => 'partners_credentials',
'model' => App\Models\PartnerCredentials::class,
],
],
Guards
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'partners_credentials' => [
'driver' => 'session',
'provider' => 'partners_credentials',
'hash' => false,
],
],
What i am missing here
Try this way
$user = JWTAuth::user();
I have a users table already.
I need to log my user in base on another table babies in my database.
/config/auth.php
I've tried adding a guard
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
------------------------------------------------ Added 👇🏾
'baby' => [
'driver' => 'session',
'provider' => 'babies'
],
],
and a provider
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
------------------------------------------------ Added 👇🏾
'babies' => [
'driver' => 'eloquent',
'model' => App\Baby::class,
],
],
and tried the attempt like this :
$auth = Auth::guard('baby')->attempt([
'email' => strtolower(Request::get('email')),
'password' => Request::get('password'),
'status' => 1
]);
It works ... my $auth returns true
but I have no idea how to access the current Auth::Baby or Auth::Object.
Can someone please give me some directions?
$baby = Auth::guard('baby')->user(); $defaultUser = Auth::guard('web')->user(); or Auth::user(); to retrieve logged in user. $isBabyLogged = Auth::guard('baby')->check(); $isDefaulrUserLogged = Auth::guard('web')->check(); or Auth::check(); - to check if the user is logged in.
Laravel Can't log in the user after register
When I register a new User, it returns successful, but when I try to log in it just returns 'login email id or password invalid'. I can't figure out what's wrong. It's working on the first created account, and it does not work for newly created accounts
class CustomerController extends Controller
{
public function login(Request $request){
$user =new Customer;
$user->email =$request->email;
$user->password =Hash::make($request->password);
$credentials = $request->only('email', 'password');
if(Auth::attempt($credentials)) {
$user = Auth::user();
$data['token'] = $user->createToken('kla')->accessToken;
return response()->json([
'status' => true,
'data' => $data,
'message' => ''
]);
}
else
{
return response()->json([
'status' => false,
'data' => [],
'message' => 'login email id or password invalid'
]);
}
}
public function register(Request $request){
$user =new Customer;
$user->firstname =$request->firstname;
$user->lastname =$request->lastname;
$user->email =$request->email;
$user->password =Hash::make($request->password);
$user->country =$request->country;
$user->mobile =$request->mobile;
if($user->save()){
return response()->json([
'status' => true,
'data' => [],
'message' => 'User Registation Successfully'
]);
}
else{
return response()->json([
'status' => false,
'data' => [],
'message' => 'User Registation Faild'
]);
}
}
}
this is customer model
class Customer extends Authenticatable
{
use HasFactory, Notifiable ,HasApiTokens;
protected $fillable = [
'firstname',
'lastname',
'email',
'password',
'country',
'mobile',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
this is my Config/auth.php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
'hash' => false,
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'Customers' => [
'driver' => 'eloquent',
'model' => App\Models\Customer::class,
'table' => 'customers',
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
];
None of your guards are setup to use this Customer model as none of them are using your Customers provider setup; which will be renamed to the lowercase version customers from here out. The 'provider' key for the guard would need to be customers in this case.
'guards' => [
...
'api' => [
'driver' => 'passport',
'provider' => 'customers',
'hash' => false,
],
],
'providers' => [
...
'customers' => [
'driver' => 'eloquent',
'model' => App\Models\Customer::class,
],
],
Also when making calls to the authentication system it will use the default guard unless told otherwise. So without specifying you are currently using the web guard. If you wanted to use the 'passport' driver, that the api guard is set to use, you would have to adjust the default or specify this:
Auth::guard('api')->....
In your config file you should define provider
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'customers',
],
'api' => [
'driver' => 'passport',
'provider' => 'customers',
'hash' => false,
],
First time i've asked anything on here before and not the most experienced so please be nice!
I have two tables set up in my DB - Users & Agents.
I have tried multiple ways in which to login both of the users and would ideally like them to be able to login from the same form and do a check on both guards to see if the credentials are valid but for some reason when doing doing attempts on the guards it will only work for the table my default is set to in my auth.php file.
Guards I am attempting:
Auth::guard('web')->attempt($credentials)
Auth::guard('agent')->attempt($credentials)
If I set my default guard in my auth.php file to the web guard its works fine and logs in the matching credentials from the Users table but when trying the agents credentials it shows in my network tab:
Login -
POST 302 found
Agent -
GET 302 found
Login -
Get 200 OK
So it's almost like it acknowledges to credentials are correct and re-directs but then doesnt carry on using the selected guard?
LoginController - DoLogin
Auth.php configeration
in your config/auth.php copy below code :
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'agent' => [
'driver' => 'session',
'provider' => 'agents',
]
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'agents' => [
'driver' => 'eloquent',
'model' => App\Agent::class,
]
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'agents' => [
'provider' => 'agents',
'table' => 'password_resets',
'expire' => 60,
],
],
];
in your Login Controller :
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);
if (Auth::guard('agent')->attempt(['email' => $request->email, 'password' => $request->password])) {
return redirect('/dashboard/agent');
}
if(Auth::guard('web')->attempt(['email' => $request->email, 'password' => $request->password])){
return redirect('/dashboard/user');
}
return redirect()->back()->withInput($request->input());
}
I am trying to make a user authentication with two tables, a different table to 'users' and the one of 'users' which is the one with default Laravel, use Laravel passport. The detail is when I try to login with the second table I can not do the authentication because I do not recognize the users of the second table.
-Model User2: ->protected $guard_name='api2';
-connection: ->protected $connection='mysql2';
**config/auth.php**
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
'api2' => [
'driver' => 'passport',
'provider' => 'users2',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'user2' => [
'driver' => 'eloquent',
'model' => App\User2::class,
],
],
My controller
public function login(Request $request)
{
if (Auth::attempt($request->only('email', 'password'))) {
$email = $request->only('email');
$token = $user->createToken('123456')->accessToken;
$user->withAccessToken($token);
$email=json_decode(json_encode($email));
$user = User2::where('email', $email->email )->first()->only('id','name','email','employee_id');
$user1 = User::find($user['id']);
if(!$user1->hasAnyRole(Role::all())){
return response()->json([
'success' => false,
'data' => '',
'msg' => "does not have assigned role"
], 403);
}
$roles = $user1->getRoleNames()->first();
$role = Role::findByName($roles,'api');
$user=json_decode(json_encode($user));
return response()->json([
'success' => true,
'token' => $token,
'data' => [
'user_id' => $user->id,
'name' => $user->name,
'email' => $user->email,
'employee_id' => $user->employee_id,
'role_id' => $role->id,
'role_name' => $role->name
],
'msg' => "Successfully"
], 200);
} else {
return response()->json(['error' => 'Unauthorised'], 401);
}
}
While Laravel Passport is useful to most developers, it is still a puzzle on how to authenticate multiple (user) models within it since underneath(by default), it looks on the users table only.
https://github.com/jsdecena/laravel-passport-mutiauth