Problem: {“message”:”Unauthenticated.”}
Explanation: I am using Tenancy for Laravel, having single central domain, but with multi database. Each database has personal_access_token table where I am storing the data. There is a 6 digit code organisation code for each organisation and minimum 8 digit user code for each user. User can only login through the usercode. On the time of login I can easily access the tenant data by having the organisation code and can store the token in the personal_access_token table for that particular database. But after the token generate I can not able to authenticate the user
Expectation: Validating the token and get the user info and the set the user’s tenant for the future tasks on the particular database.
api.php
Route::post('/login', [UserController::class, 'login']);
Route::middleware('auth:sanctum')->group(function () {
Route::post('/logout', [UserController::class, 'logout']);
Route::post('/create-new-user', [UserController::class, 'store']);
});
login code
public function login(Request $request): mixed
{
$request->validate([
'username' => 'required',
'password' => 'required'
]);
try {
$userCode = (int)(substr($request->username, 3));
$organisationCode = (int)(substr($request->username, 3, 6));
$tenant = Tenant::find($organisationCode);
Tenancy::initialize($tenant);
$user = User::where('users.userCode', $userCode)
->join('organisation_infos', 'organisation_infos.code', '=', 'users.organisationCode')
->select('users.*', 'organisation_infos.centreName')
->first();
if (!$user || !Hash::check($request->password, $user->password)) {
return APIResponseHelper::ApiResponse(
isError: false,
responseCode: 4701,
responseMessage: "Invalid Username or Password.",
responseContent: "Invalid Username or Password."
);
}
if ($user->loggedIn === 1) {
return APIResponseHelper::ApiResponse(
isError: false,
responseCode: 4702,
responseMessage: "User Already Logged in on another device.",
responseContent: "User Already Logged in on another device."
);
}
$user->loggedIn = true;
$user->save();
$token = $user->createToken($user->organisationCde . $user->name . $user->mobile . 'Rizwan' . $user->userCode)->plainTextToken;
Tenancy::end();
return APIResponseHelper::ApiResponse(
isError: false,
responseCode: 4700,
responseMessage: "Successfully Logged In.",
responseContent: ["token" => $token, "createdUserInfo" => $user]
);
} catch (Exception $exception) {
$getTheErrorMessage = $exception->getPrevious();
return APIResponseHelper::ApiResponse(
isError: false,
responseCode: 4713,
responseMessage: "Error Occurs",
responseContent: $getTheErrorMessage->errorInfo[2] ?? 'Contact Core Team'
);
}
}
logout code php
public function logout(): mixed
{
$userInfo = auth()->user();
dd($userInfo->organisationCode);
$tenant = Tenant::find($userInfo->organisationCode);
Tenancy::initialize($tenant);
return APIResponseHelper::ApiResponse(
isError: false,
responseCode: 4700,
responseMessage: "Logout Message",
responseContent: "Logout Content"
);
}
I have tried to overcome this problem but I failed each time. Now I am expecting a solution from PHP or Developer community.