0

I want my app users to login with their Wordpress credentials. I've installed Alamofire & SwiftyJSON in order to do this, and I'm able to succesfully GET Wordpress endpoints to my app (via the Wordpress REST API). That said, I'm not sure how to authenticate my user on login?

I'm trying out the below, but the response is simply nil. Am I posting the user parameters to the wrong endpoint? Help is appreciated! I'm stumped.

 let parameters: Parameters = [
            "username": userField.text!,
            "password": passField.text!,
            
        ]

        
        AF.request("http://myurl.com/wp-json/wp/v2/users", method: .post,  parameters: parameters, encoding: JSONEncoding.default)
        
        
                .responseJSON { response in
                    switch response.result {
                    case .success(let value):
                        if let json = value as? [String: Any] {
                            print(json["Result"] as? Int)
                        }
                    case .failure(let error):
                        print(error)
                    }
            }
Brittany
  • 1,359
  • 4
  • 24
  • 63
  • https://stackoverflow.com/questions/13679001/register-login-user-with-wordpress-json-api – GrafiCode Apr 24 '22 at 21:04
  • @GrafiCode I checked the answer and added the code block to my theme's functions.php file... but when I do that and then navigate to the end point http://www.url.com/wp-json/custom-plugin/login, it returns the error: {"code":"rest_no_route","message":"No route was found matching the URL and request method.","data":{"status":404}} – Brittany Apr 24 '22 at 21:19

1 Answers1

0

I accomplished this some time ago for WooCommerce. I imagine it would be similar to WordPress. I found two ways of doing this.

One way you would need a JWT. I use the free plugin "JWT Auth" by Useful Team. Using the username and password you obtain the token and sign in the user.

The other way is using HTTPS Authentication using Google Cloud Functions. I opened the WooCommerce login page using a WKWebView and added the Cloud Functions addresses as return and callback URLs.

I know the answer is vague but hopefully it will provide you with different ways to approach the task. Explaining the entire thing would be almost like a tutorial.

GIJoeCodes
  • 1,680
  • 1
  • 25
  • 28