waneta's avatar

How to get logged in user data for multiple authentication?

first of all, i'm sorry about my bad english. and i'm newbie to laravel.

i've set up multiple authentication ( https://www.codementor.io/okoroaforchukwuemeka/9-tips-to-set-up-multiple-authentication-in-laravel-ak3gtwjvt )

from this scenerio, how do i get logged in doctor data ?

get. auth()->user()->id; etc.. for normal user. ok!

but how is it for another authentication type?

0 likes
3 replies
BezhanSalleh's avatar
Level 25

based on the article, for the doctor data you could do:

if (Auth::guard('doctor')->check()){
    $user = Auth::user();
}

for the other types do as above.

also for multiple authentication you could use on of the following packages for multiple authentication based on the size of your application, this way you will have more control over the user management. and if you wanna start there are bunch of tutorials based on these package just do google search on how to use anyone.

https://github.com/JosephSilber/bouncer

https://github.com/spatie/laravel-permission

cheers...

waneta's avatar

thank you very much. i'll look at these packages soon.

last question:

for my comments table and comment controller(store function)

$comment->user_id = Auth::id();

or

$comment->user_id = Auth::user()->id;

it works. i can get user_id.

but after setup all things for doctor

$comment->doctor_id = Auth::guard('doctor')->id;

fail! it returns "Undefined property: Illuminate\Auth\SessionGuard::$id"

how can i get doctor id as user id?

BezhanSalleh's avatar

@waneta no problem... for that do the same,

if (Auth::guard('doctor')->check()){
    $comment->doctor_id = Auth::id();
}

if $comment->doctor_id is nullable in case if the authenticated user is not a doctor then set as follow:

if (Auth::guard('doctor')->check()){
    $comment->doctor_id = Auth::id();
}else{
    $comment->doctor_id = 0; //if data type integer so you would know it's not doctor
//or
    $comment->doctor_id = 'Not Doctor'; //if data type string
}

Please or to participate in this conversation.