0

Im getting an error while im trying to login: there's my login service, im sure it works so well because I tested it on postman, and I worked with it already on android, and it works perfectly, encrypted password and salt works so well ..


app.post('/login/',(req,res,next)=>{
    var post_data = req.body;

    //Extract email and password from request
    var user_password = post_data.password;
    var email = post_data.email;

    con.query('SELECT * FROM user where email=?',[email],function (err,result,fields) {
        con.on('error', function (err) {
            console.log('[MYSQL ERROR]', err);
        });
        if (result && result.length)

        {
            var salt = result[0].salt;
            var encrypted_password = result[0].encrypted_password;
            var hashed_password = checkHashPassword(user_password, salt).passwordHash;
        if (encrypted_password == hashed_password)
            res.end(JSON.stringify(result[0]))
        else
            res.end(JSON.stringify('Wrong Password'))


        }

        else {

                res.json('user not exists!!');

            }

    });


})

and there's my swift code on btn_login action, im using Alamofire, he shows me an error with the url, but i worked with same url on displaying data from my table at it worked fine

    @IBAction func btn_login(_ sender: Any) {


        let serverUrl = "https://127.0.0.1:1337/login"


        guard let email = emailtf.text, !email.isEmpty else {return}
        guard let password = passwordtf.text, !password.isEmpty else {return}


      let loginRequest = [
                   "email" : email,
                   "password" : password
               ]

        Alamofire.request(serverUrl, method: .post, parameters: loginRequest, encoding: JSONEncoding.default, headers: nil)
            .validate(statusCode:200..<300)
                .responseJSON { response in
                    switch response.result {
                    case .success(let value):
                        print(value)


                        break
                    case .failure(let error):
                        print(error)

                        break
                    }
            }
    }

and im getting this error on xcode console

["Email": "test", "Password": "test"]
2019-11-23 15:41:19.991572+0100 ProjetIOS[19982:799169] Task <F5DB6B8F-2658-4C83-BF64-663B04673CAC>.<1> finished with error [-1002] Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSErrorFailingURLStringKey=localhost:1337/login, NSErrorFailingURLKey=localhost:1337/login, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <F5DB6B8F-2658-4C83-BF64-663B04673CAC>.<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <F5DB6B8F-2658-4C83-BF64-663B04673CAC>.<1>, NSUnderlyingError=0x600002f809f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}
Request failed with error: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSErrorFailingURLStringKey=localhost:1337/login, NSErrorFailingURLKey=localhost:1337/login, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <F5DB6B8F-2658-4C83-BF64-663B04673CAC>.<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <F5DB6B8F-2658-4C83-BF64-663B04673CAC>.<1>, NSUnderlyingError=0x600002f809f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}

Any help please? Thank you

Yafet Shil
  • 41
  • 7

1 Answers1

0

put your local IP instead of localhost in url . e.g. http://192.168.1.34:1337/

Also try adding headers in your Alamofire request. Here is an example:

let loginRequest = [
        "Email" : self.emailtf.text! as String,
        "Password" : self.passwordtf.text! as String
    ]
let headers = ["Content-Type" : "application/json"]

Alamofire.request(url, method : .post, parameters : loginRequest, encoding : 
JSONEncoding.default , headers : headers).responseData { dataResponse in

 print(dataResponse.request as Any) // your request 
 print(dataResponse.response as Any) // your response
 }
Keshu R.
  • 5,045
  • 1
  • 18
  • 38