'iOS Swift - Authenticate wordpress user using Alamofire?

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)
                    }
            }


Solution 1:[1]

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.

Solution 2:[2]

You are just declaring a new variable trying to access it via object's property. You should map your data in the first place like:

const names = [
    {
        firstname: "John",
        fullname: "John Doe"
    },
    {
        firstname: "Amber",
        fullname: "Amber Smith"
    },
    {
        firstname: "Michael",
        fullname: "Michael Smith"
    },
    {
        firstname: "Jessica",
        fullname: "Jessica Kelly Charles"
    }
].map(person => ({
    ...person, 
    lastName: person.fullName.replace(person.firstName + ' ', '')
}));

After mapping your data like that, you can use .lastName in the place you need.

names.sort(function(a, b) {
    if (a.lastName < b.lastName) {
        return -1;
    } else if (a.lastName > b.lastName) {
        return 1;
    }
    return 0;
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GIJoeCodes
Solution 2 Sefa ?ahino?lu