Laravel Sanctum's createToken method tries to insert to wrong table - laravel

I'm trying to setup Laravel Sanctum to issue API tokens.
When I do $user->createToken('test');, it fails while attempting to insert the generated token into my tokens table which I had previously created to store third party API tokens.
I was expecting it to insert it into personal_access_tokens instead.
My guess is that somehow this tokens table and Token model interfere with Sanctum. In my User model, I have this method which return a user's third party API tokens:
public function tokens()
{
return $this->hasMany(Token::class);
}
Any idea why it does that and how to fix it?

The createToken method is like so :
public function createToken(string $name, array $abilities = ['*'])
{
$token = $this->tokens()->create([
'name' => $name,
'token' => hash('sha256', $plainTextToken = Str::random(40)),
'abilities' => $abilities,
]);
return new NewAccessToken($token, $token->getKey().'|'.$plainTextToken);
}
but I had already in my User model a tokens method. That why is was creating the token in the wrong table.

Related

How to find user by user name in laravel?

I am trying to find user by user name while building a laravel API. This is what I'm trying
api.php
Route::get('user-profile/user/{user}', 'UserProfileController#getUserProfile');
Controller
public function getUserProfile(User $user)
{
$userId = User::where('userName', $user->userName)->first()->id; // I am expecting user id here like "1"
}
This causing 404 not found error.
By default route model binding will search for id.
You should customise the column name in your route if you want Laravel to select the user using the column userName.
In you api.php:
Route::get('user-profile/user/{user:userName}', 'UserProfileController#getUserProfile');
Assuming that your userName is unique in the table, then in your controller you should use:
public function getUserProfile(User $user)
{
$userId = user->id;
...
}
This will automatically handle 404 errors for you as opposed to this answer.
by default route model binding search for id and in your case you want search by username so all you need is to remove dependency injection from your controller parameters like this
public function getUserPortfolio($user) // fix it as $user only
{
$userId = User::where('userName', $user)->first()->id; // I am expecting user id here like "1"
}

how to get id from url to add it in database as a foreign key in another table

i want to get id form url and use it as a foreign key to put it in table in database
when i do it like that a got an error that tour_id column is embty
public function addtour(Request $request,$id) {
$form_data = array(
'user_id' => $request->input("user_id",auth::user()->id),
'tour_id' => $request->input("tour_id",$id),
);
tecket::create($form_data);
return view('submit_tour');
}
You want to define a route that has the id as a parameter, so something like:
Route::get('/add-tour/{id}', [TourController::class, 'addTour']); // Laravel 8
Route::get('/add-tour/{id}', 'TourController#addTour'); // Laravel 7
The {id} parameter in the URL will be passed in as the $id in your addTour function.
What you then do with the $id such as checking it is valid is up to you.

How to get password from another table?

I'm implementing a login API using Laravel 5.4 for a company that already has two tables where they store username and password. I'm using Tymons JWTAuth which uses the default auth. How can I override the authentication so that it gets the password from another table and not the users table?
Currently I'm using my localhost and the default users table
public function login() {
$credentials = $request->only('user', 'password');
try {
if ( ! $token = JWTAuth::attempt($credentials))
{
return response()->json([
'error' => 'Your email or password is incorrect'
], 401);
}
}
already has two tables where they store username and password
The best way you can do is copy your data to the Auth tables. If you don't want to use the password from the auth, then better rewrite it from scratch and don't use Auth to make your life simplier. Goodluck!
Decided to create a view in my database from the two tables. Seemed like the easiest solution

Laravel 5.2 validation on joined table

I want to make validation on a joined table,I know that laravel 5.2 doesn't support this. what is the best practice to do that ?
any suggestions?
The best way to do this, manually authenticate the user with the values you've grabbed from your joined database.
The fastest solution I've brought up is like this:
//join the tables and name it as $joined_table_values
if(isset($joined_table_values)){
if($joined_table_values->field == 'theValueYouNeed'){
//Then authenticate manually with the values in default users table
if (Auth::attempt(['email' => $email, 'password' => $password])) {
// Authentication passed...
return redirect()->intended('dashboard');
}
}
}
Resource: https://laravel.com/docs/5.2/authentication#authenticating-users

Laravel 4 - Login count using standard authentication

I was wondering if someone could point me in the right direction.
I have just started using laravel 4 and i want to add a login count to my application.
Im using the standard authentication from the laravel docs.
Im guess that i have to add something like the following SQL ... But do i add it from the routes file or the User.php model ... and then how would i call it from the login route?
$sql = "UPDATE some_table SET total_logins = total_logins + 1 WHERE user_id = $userID";
Any help would be awesome
First, instead of sql, i would use Eloquent.
Then, you can access your logged in user with the Auth::user() function, and use Eloquent increment function :
Auth::user()->increment('total_logins');
Auth::user()->save();
I would do that in your login function in your UserController file.
if (Auth::attempt(array('email' => $email, 'password' => $password)))
{
Auth::user()->increment('total_logins');
Auth::user()->save();
}

Resources