• I have a main website programmed with laravel and the blog of this website in a subdomain and made with wordpress , i have already shareed user register between them , so when new user register on main laravel site , it will be created automatically on wp site . the issue now is sharing login i want if a user logged in main site to be logged also in blog wp ?

    i have tried with api , but it doesnot work ?

    in laravel login function

    //login user in WP help website
           Http::post('https://subdomain/wp-json/userapi/v1/userLogin', [
           'email'  => auth()->user()->email,
            ]);
    

    in functions.php in wordpress

    public function userLogin (WP_REST_Request $request){
            $email = $request->get_param('email');
            $user = get_user_by('email',  $email);  
            wp_set_auth_cookie($user->ID);
            do_action( 'wp_login', $user->user_login, $user );
            
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s impractical to auto login a user based on any random WP request. Adequate security would be difficult. It’d be better for the Laravel site to request that WP log the user in when they login to the Laravel site. It could do so by making an API request to a custom endpoint intended for this purpose. The Laravel site can use a WP application password to gain enough authority to login a user via the API.

    The endpoint process could take passed user information such as their login name and use it to get the associated WP_User object. With the object, a user can be logged into WP with the following:

            wp_clear_auth_cookie();
            wp_set_current_user ( $user->ID ); // Set the current user detail
            wp_set_auth_cookie  ( $user->ID ); // Set auth details in cookie
    Thread Starter mohamedfathy90

    (@mohamedfathy90)

    thanks bcworkz for your reply and help , i have already followed your idea and made the endpoint and send the api request from laravel site to the endpoint in wp upon user login in laravel site , but i didnot use application password , i tested with postman and gives status code 200 and i can see that cookie has been created successfully . but when i use actually i cannot find this cookie created .

    Moderator bcworkz

    (@bcworkz)

    There are a number of possible authentication methods for the API. I’ve found application passwords the easiest to use. It doesn’t really matter which you use as long as it’s done correctly.

    You say you see the cookie is created but when you use you cannot find it? How could you know it was created if you cannot find it? ?? If you send the request from Postman, then it was sent the cookie in return. If you later try to access the WP from a web browser, it doesn’t have the cookie, so of course there’s nothing to be found. The API login request has to come from the same client app/browser that will be used to access WP.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to login same user in wordpress when login in laravel?’ is closed to new replies.