Web based family history software

Question Check if user logged in and get username

  • jbldn1982
  • Topic Author
  • Offline
  • New Member
  • New Member
More
1 month 3 weeks ago #1 by jbldn1982
Check if user logged in and get username was created by jbldn1982
Hi all!

I just migrated from 1.7.11 to the latest version.
There is a lot of custom code we have been using on our family site for almost 20 years and in the past I used to be able to get the username of users quite easily.
However, the new middleware heavy system is very confusing and I have not been able to get a username query to work in a standalone script.
To be clear, I would really rather not build a module if that can be avoided at all as the documentation is quite limited / challenging and I have fully working forum already.
I just need the username of the logged in person. Below is what I tried and it always says not logged on.
Thank you so much!
Code:
<?php namespace Fisharebest\Webtrees; use Fisharebest\Webtrees\Controller\PageController; use Fisharebest\Webtrees\Functions\Functions; use Fisharebest\Webtrees\Auth; use Rhumsaa\Uuid\Uuid; use PHPMailer; use Fisharebest\Webtrees\Webtrees; use Illuminate\Database\Capsule\Manager as DB; // Display all errors, warnings, and notices error_reporting(E_ALL); ini_set('display_errors', 1); define('WT_SCRIPT_NAME', 'meran_forum.php'); require 'vendor/autoload.php'; // Autoloads necessary classes $webtrees = new Webtrees(); $webtrees->bootstrap(); if (Auth::check()) { $username = Auth::user()->userName(); // Retrieve the username echo "Logged in as: " . $username; } else { echo "Not logged in."; }

Please Log in or Create an account to join the conversation.

More
1 month 3 weeks ago #2 by fisharebest
Replied by fisharebest on topic Check if user logged in and get username
Your code isn't working because you have bypassed the "middleware", which is what sets the current user from the session data.

Summary: webtrees is built around request handlers and middleware.

A request handler takes an HTTP request, and generates an HTTP response.

Middleware are wrappers around this process. They are called before an after. Thus they can modify the request, modify the response, etc.

To create a new page, you need to create a module.

There are lots of example modules here:
github.com/webtrees/

Your module needs the following

1) register a route (i.e. a URL), and set itself as the request handler for that route. Use the following function in your module

public function boot(): void
{
Registry::routeFactory()->routeMap()
->get(static::class, '/foo/bar/whatever', $this);
}

You'd create URLs to this page using

route(YourModule::class, )

Your module would need to implement RequestHanderInterface and have a function handle()

Now, requests to this URL will be passed to your module.

You can use Auth::user(), etc. here

To send output back to the browser, don't use echo.

Instead

return response($your_html);

To send output with webtrees headers/footers, you'd use the ViewResponseTrait and this:

return $this->viewResponse(...)

Greg Roach - greg@subaqua.co.uk - @fisharebest@phpc.social - fisharebest.webtrees.net

Please Log in or Create an account to join the conversation.

  • jbldn1982
  • Topic Author
  • Offline
  • New Member
  • New Member
More
1 month 3 weeks ago #3 by jbldn1982
Replied by jbldn1982 on topic Check if user logged in and get username
Amazing, thank you so much Greg! Let me try and figure that out.

Please Log in or Create an account to join the conversation.

Powered by Kunena Forum