Example Login with basic auth authentication using Alamofire?

Asked

Viewed 477 times

0

I’m trying to make a login screen where I use Alamofire to make the request, but with the examples of the site Alamofire is not working.

If anyone can help with an example code, I would really appreciate it!!

What’s the big deal? I try to use the basic auth of the Alamofire to perform authentication, but I am not successful.

Example of the code I’m using and testing:

import UIKit

import Alamofire import Swiftyjson

class Loginviewcontroller: Uiviewcontroller {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}


@IBOutlet weak var txtEmail: UITextField!

@IBOutlet weak var txtSenha: UITextField!


@IBAction func bntLogin(_ sender: Any) {



    //let user = txtEmail.text
    //let password = txtSenha.text
    //let credentialData = "\(user):\(password)".data(using: <#T##String.Encoding#>)
    //let base64Credentials = credentialData?.base64EncodedData(options: <#T##Data.Base64EncodingOptions#>)
    //let headers = ["Authorization": "Basic \(base64Credentials)"]


    //let user = txtEmail.text
    //let password = txtSenha.text
    /*
    var headers: HTTPHeaders = [:]

    if let authorizationHeader = Request.authorizationHeader(user: user!, password: password!) {
        headers[authorizationHeader.key] = authorizationHeader.value
    }

    Alamofire.request("https://www.url.com.br/wp-json/wp/v2/users", headers: headers).responseJSON {

        response in debugPrint(response)

    }
    */

    let username = txtEmail.text
    let password = txtSenha.text

    let loginString = String(format: "%@:%@", username!, password!)
    let loginData = loginString.data(using: String.Encoding.utf8)!
    let base64LoginString = loginData.base64EncodedString()

    //Cria uma requisição
    let url = URL(string: "https://www.url.com.br/wp-json/wp/v2/users")
    var request = URLRequest(url:url!)
    request.httpMethod = "POST"
    request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")

    // executa
    let urlConnection = NSURLConnection(request: request, delegate: self)


}







override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

  • Apparently what you’re doing is right. Basically encoding username:password in Base64 and sending as part of http header "Authorization". This is basic Authentication... which is the error?

  • And as I check, if really the user managed to log in or gave error?

  • From the moment you receive data from the called q url and/or received no 4xx error I assume your authentication worked. What is the status code?

  • Which part of that pile of codes, in which case would be correct, because I have some commented ?

  • I haven’t looked at the commented code. But I don’t program in Swift, but I program in C++ and I can understand what’s going on. Regardless of the code, if you are not having build errors, the process is correct for basic Authentication. The server has to send you a 2xx code for success or 4xx for error. Which code you are receiving?

  • When I send the request it just returns the json

  • Then enter a qqer user and password and do the negative test. If the server returns 4xx code then it is working. If with the wrong password you still receive the answer and data, the server is poorly configured and allowing qqer request independent of credentials.

  • Thanks I’ll try.

  • You have to keep an eye on the HTTP code, especially when you’re doing webserver-based authentication. Good luck.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.