Reputation:
I am setting like and unlike options on articles. but in LikeController there is problem. when I push the like button it says ->
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into
likes
(user_id
,article_id
,updated_at
,created_at
) values (?, 5, 2019-05-30 07:58:34, 2019-05-30 07:58:34))
Like model:
class Like extends Model
{
public $timestamps = true;
public $with = ["user"];
protected $fillable = ["user_id", "article_id"];
public function article()
{
return $this->belongsTo("App\Article");
}
public function user()
{
return $this->belongsTo("App\User");
}
}
and LikeController:
use Auth;
use App\Article;
use App\Like;
public function like($id)
{
$article = Article::find($id);
$like = Like::create([
"user_id" => Auth::id(),
"article_id" => $article->id
]);
return Like::find($like->id);
}
public function unlike($id)
{
$article = Article::find($id);
Like::where("user_id", Auth::id())
->where("article_id", $article->id)
->first()
->delete();
return 1;
}
I am not very sure why Auth::id()
can't find the user id and return null?
Using JWT with vue.js
Routes:
Route::group(['prefix' => 'auth'], function ($router) {
Route::post('register', 'AuthController@register');
Route::post('login', 'AuthController@login');
Route::post('logout', 'AuthController@logout');
Route::post('refresh', 'AuthController@refresh');
Route::post('me', 'AuthController@me');
});
Route::get("/like/{id}", [
"uses" => "LikeController@like"
]);
Route::get("/unlike/{id}", [
"uses" => "LikeController@unlike"
]);
AuthController and User
to set controller and user model I followed the docs: https://jwt-auth.readthedocs.io/en/develop/quick-start/
Upvotes: 0
Views: 3464
Reputation: 740
Check if User is authenticated or not, and don't forgot to use Auth class.
use Auth;
check if auth than delete, And Use Auth::user()->id
if (Auth::user())
{
Like::where("user_id", Auth::user()->id)
->where("article_id", $article->id)
->first()
->delete();
}
Upvotes: 0
Reputation: 399
Also you can use passport from Laravel where you have OAuth2 :-)
Laravel Documentation about Laravel Passport say:
Laravel makes API authentication a breeze using Laravel Passport, which provides a full OAuth2 server implementation for your Laravel application in a matter of minutes. Passport is built on top of the League OAuth2 server that is maintained by Andy Millington and Simon Hamp.
You can look more information here where you see Laravel Passport Documentation
Upvotes: 0
Reputation: 1373
Ok ,
so i was also having that issue with my app when i was using jwt with vue js.
for getting current Auth::user()
.
You need to pass token with your request. if you check jwt core file, It getting user from token. and you are not passing token I guess.
If yes then please pass token along with your request.
And also check the documentation of jwt
. did you install your package properly?
remember for laravel 5.5 or greater, that package version to use is jwt > 1.0.0
also, check the documentation of that version properly.
Upvotes: 0
Reputation: 3562
You have missed to specify the middleware => api
from the route groups. Also include the like
related routes Or any protected routes within this routegroup.
config/auth.php
shoud be updated as
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
...
'guards' => [
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
In routes/api.php
Route::post('login', 'AuthController@login')->name('login');
Route::group(['middleware' => 'api'], function ($router) {
Route::group(['prefix' => 'auth'], function ($router) {
Route::post('register', 'AuthController@register');
# Route::post('login', 'AuthController@login'); // commented.
Route::post('logout', 'AuthController@logout');
Route::post('refresh', 'AuthController@refresh');
Route::post('me', 'AuthController@me');
});
Route::get("/like/{id}", [
"uses" => "LikeController@like"
]);
Route::get("/unlike/{id}", [
"uses" => "LikeController@unlike"
]);
});
Upvotes: 0
Reputation: 950
You seem to have not protected your LikeController routes with auth.
Unless you have something like this in your LikeController
for constructor:
public function __construct()
{
$this->middleware('auth:api');
}
then you should have your LikeController routes protected like this:
Route::group([
'middleware' => 'api',
], function ($router) {
Route::get("/like/{id}", [
"uses" => "LikeController@like"
]);
Route::get("/unlike/{id}", [
"uses" => "LikeController@unlike"
]);
});
Then the correct way of getting the logged in used is
auth('api')->user()
or
Auth::guard('api')->user()
Upvotes: 1
Reputation: 433
first: ensure that route
or Controller
protected by auth middleware.
second: ensure that you are using correct guard
for auth, if using multiple guard
.
you should specify your guard name like this: `Auth::guard('admin')->id()`
Upvotes: 0
Reputation: 321
Try importing Auth as such
use Illuminate\Support\Facades\Auth;
and getting the ID like this:
$user = Auth::user();
$id = $user->id;
Hope this helps
Add this middelware on kernel.php
protected $middlewareGroups = [
'web' => [
\Illuminate\Session\Middleware\StartSession::class,
],
];
this link might help : Answer Link
Upvotes: 0