seddka
seddka

Reputation: 105

How to restrict authenticated users to only access their on profile

I am stuck with users profile feature, I want only authenticated users to access their own profile only.

User with id: 1 can only access route /applicants/profile/1, otherwise return 404 Not found?

class ApplicantProfileController extends Controller
{
    public function show(Applicant $applicant)
    {
        return view('applicant.show', compact('applicant'));
    }
}
route::group(['prefix' => 'applicants', 'middleware' => 'auth:applicant'], function() {
    Route::get('/profile/{applicant}', 'Profiles\ApplicantProfileController@show');
});

Upvotes: 0

Views: 139

Answers (1)

Professor
Professor

Reputation: 908

You can chech whether the logged user and the parameter user are the same by using the Illuminate/Support/Facades/Auth facade like this:

public function show(Applicant $applicant)
{
    if (Auth::id() == $applicant->id) {
        return view('applicant.show', compact('applicant'));
    }

    return abort(404);
}

Upvotes: 3

Related Questions