-
Notifications
You must be signed in to change notification settings - Fork 305
Add cookie strategy configuration to PHP request handler #1753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e6ee1b7
934d1be
f77d0c5
e532d08
4938594
985fe83
ef8e39e
364e7de
3ab3876
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -209,7 +209,6 @@ async function run() { | |
php: args.php as SupportedPHPVersion, | ||
wp: args.wp, | ||
}, | ||
login: args.login, | ||
}; | ||
} | ||
|
||
|
@@ -311,6 +310,7 @@ async function run() { | |
} | ||
}, | ||
}, | ||
cookieStrategy: 'pass-through', | ||
}); | ||
|
||
const php = await requestHandler.getPrimaryPhp(); | ||
|
@@ -348,6 +348,11 @@ async function run() { | |
process.exit(0); | ||
} else { | ||
logger.log(`WordPress is running on ${absoluteUrl}`); | ||
if (args.login) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm starting to think we should just ship a WordPress plugin to log the user in the first time they interact with the site. It could go like this:
This should work in all runtimes (Studio, Playground CLI, WP-NOW, Playground webapp) without any runtime-specific loginc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Claude gave me this. Coming from AI, there's likely some problem with that code, but it seems like a good starting point: <?php
/*
Plugin Name: Auto Admin Login
Description: Automatically logs in as admin if a specific cookie is not present
Version: 1.0
Author: Your Name
*/
// Function to check cookie and perform auto-login
function auto_admin_login() {
$cookie_name = 'wordpress_user_was_auto_logged';
// Check if the cookie exists
if (isset($_COOKIE[$cookie_name])) {
return;
}
// Cookie doesn't exist, set it
setcookie($cookie_name, '1', time() + (86400 * 30), '/'); // Cookie expires in 30 days
// Get the admin user
$admin_user = get_user_by('login', 'admin');
// If admin user exists, log them in
if ($admin_user) {
wp_set_current_user($admin_user->ID);
wp_set_auth_cookie($admin_user->ID);
do_action('wp_login', $admin_user->user_login, $admin_user);
}
}
// Hook the function to run on WordPress init
add_action('init', 'auto_admin_login'); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, good idea. However, I'm concerned about using a plugin in Studio's case, since we'd need to filter it out when exporting the site. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I don't want to pollute the exports, filtering files out just doesn't work as a long term strategy. The login plugin would live in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah, ok, I didn't know about this approach 😅. Still, I wonder about how we could make this plugin work so the user can decide when it should auto-login and when not. If I understand correctly the code shared here, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fluiddot here's my thinking:
This way the user will remain logged in until they explicitly log out, at which point they'll remain logged out until they explicitly log in. A private browsing session will still default to the logged in state. There could also be a "querystring mode" where autologin is only performed when What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The approach looks good to me, thanks @adamziel ! I'll try to work on this in the next few days. Note that the original issue lowered its priority, so I might delay updating the PR.
At least for the Studio app, and probably when using node clients, this will be necessary to ensure that API requests to the site are not auto-logged. This is important when using, for instance, API keys as referenced in Automattic/studio#387. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A check like |
||
logger.log( | ||
`➜ You can auto-login to the site using the query parameter "playground-auto-login=true"\n➜ Homepage: ${absoluteUrl}/?playground-auto-login=true\n➜ WP-Admin: ${absoluteUrl}/wp-admin/?playground-auto-login=true` | ||
); | ||
} | ||
} | ||
}, | ||
async handleRequest(request: PHPRequest) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.