简体   繁体   中英

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');
});

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM