0
votes

In my model i have a method

public function newUserUpload(string $save_path){

    $this->photo_moderation_src = $save_path;

    if(Auth::check()){
        $this->user_id = Auth::id();
    }

    $this->save();

    return null;
}

After authorization i try to upload file but the record in the database is created without user_id. At the same time the authorization check in my blade is working correctly.

        @if (!Auth::check())
            <li><a href="/home">Auth</a></li>
        @else
            <li><a href="/logout/">Exit</a></li>
        @endif

Could this be due to the fact that I use vueJs + Laravel api routes?

Route::middleware('api')->group(function(){

    Route::post('/upload/', 'CompgenApiController@userUpload');

    Route::post('/reupload/', 'CompgenApiController@moderationReupload');

});
1
The default authentication uses the session which does not work on API routes.apokryfos
Any ideas for a solution?Viktor
Using the web routes is the easiest solution. Otherwise you can use passport for API authentication.apokryfos

1 Answers

0
votes

use Auth; or use Illuminate\Support\Facades\Auth;

use Illuminate\Support\Facades\Auth;

if (Auth::check()) {
    // The user is logged in...
}

or

if (\Illuminate\Support\Facades\Auth::check()) {
    // The user is logged in...
}

for user id u can use Auth::user()->id or something else in users table, like: Auth::user()->hasRole, Auth::user()->registrationCompleted

public function newUpload(string $save_path){

    if(!Auth::check()){ return false }
    // if(!Auth::user()->registrationCompleted){ return false }
    // other Security measures

    $newUpload = new Pic(); // pics table in dataBase
    $newUpload->photo_moderation_src = $save_path;
    $newUpload->user_id = Auth::user()->id;
    $newUpload->save();

    return true;
}