Add new loggedin container slim

Here is an example of how you can add a new loggedin container to your Slim framework application:

use Slim\App;
use Slim\Http\Request;
use Slim\Http\Response;

$app = new App();

// Define a new container for loggedin users
$app->container->add('loggedin', function ($c) {
    // Initialize the loggedin container
    $loggedin = new \stdClass();
    $loggedin->user = null; // assume user is not logged in initially

    // Add a method to check if the user is logged in
    $loggedin->isLoggedIn = function () use ($loggedin) {
        return!is_null($loggedin->user);
    };

    // Add a method to login a user
    $loggedin->login = function (Request $request, Response $response) use ($loggedin) {
        // Implement your login logic here
        // For example, you could check if the username and password are valid
        $username = $request->getParam('username');
        $password = $request->getParam('password');
        if ($username === 'admin' && $password === 'password') {
            $loggedin->user = new \stdClass();
            $loggedin->user->name = 'Admin';
            return $response->withJson(['message' => 'Logged in successfully']);
        } else {
            return $response->withJson(['message' => 'Invalid username or password'], 401);
        }
    };

    // Add a method to logout a user
    $loggedin->logout = function (Request $request, Response $response) use ($loggedin) {
        $loggedin->user = null;
        return $response->withJson(['message' => 'Logged out successfully']);
    };

    return $loggedin;
});

In this example, we define a new container called loggedin and add three methods to it: isLoggedIn, login, and logout. The isLoggedIn method checks if the user is logged in by checking if the user property is not null. The login method checks if the username and password are valid and sets the user property if they are. The logout method sets the user property to null.

You can then use these methods in your routes to check if the user is logged in, login a user, or logout a user. For example:

$app->get('/protected', function (Request $request, Response $response) use ($app) {
    $loggedin = $app->container->get('loggedin');
    if (!$loggedin->isLoggedIn()) {
        return $response->withJson(['message' => 'You must be logged in to access this page'], 401);
    }
    // The user is logged in, so you can access protected resources here
    return $response->withJson(['message' => 'Welcome, '. $loggedin->user->name]);
})->add(new \Slim\Middleware\Authenticate($app->container->get('loggedin')));

$app->post('/login', function (Request $request, Response $response) use ($app) {
    $loggedin = $app->container->get('loggedin');
    return $loggedin->login($request, $response);
})->add(new \Slim\Middleware\BodyParser());

$app->post('/logout', function (Request $request, Response $response) use ($app) {
    $loggedin = $app->container->get('loggedin');
    return $loggedin->logout($request, $response);
});

Note that this is just a basic example and you should implement your own login and logout logic. You may also want to add additional security measures, such as hashing and salting passwords, and validating user input.