You can use the userSession from dependency Injection.
This gives you an Instance of IUser, of which you can use getDisplayName() $userSession->getUser()->getDisplayName()
I know this thread is a bit dated already, but I found another solution that may be helpful for others too:
If your external app or website resides inside your nextcloud domain and you use SSL (which you should!), you can just check $_SERVER[‘HTTP_COOKIE’] for the string “nc_username=”. The current user name just starts after that. The string will only be present while the user is logged in into nextcloud.
To prevent sending out any data while the user is not being logged in, you may use something like this: