johnbrett / hapi-auth-bearer-token

Simple Bearer authentication scheme plugin for hapi, accepts token by Header, Cookie or Query parameter.
MIT License
218 stars 46 forks source link

"Error 500, invalid token, data null" when recalling the previous token in Alamofire #206

Closed haruharuya closed 4 years ago

haruharuya commented 4 years ago

I save my login token with UserDefault. Here is the code :

            AF.request(encodeURL, method: .post, parameters: nil, headers: headers, interceptor: nil)
            .responseString { response  in

                switch response.result{
                case .success(_):
                    if let json = response.value{
                        UserDefaults.standard.set(json, forKey: "token")
                        print("token saved \(json)")
                        // print((json as! [String : AnyObject]))
                        let homePage = self.storyboard?.instantiateViewController(identifier: "dashboard") as! HalamanUtama
                        self.navigationController?.pushViewController(homePage, animated: true)

                    }
                    break
                case .failure(let error) :
                    print([error as! Error])
                    break

                }
        }

I didn't make an Interceptor class(retry and adapt) func due to the the token is not expired yet. Then, I called the token that I've already saved before in this class. Here is the code :

       AF.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default).responseJSON { (response) in
        switch response.result{
                           case .success(_):
                               if let json = response.value{
                                let defaults = UserDefaults.standard
                                defaults.value(forKey: "token")
                                   print("token saved \(json)")

                               }
                               break
                           case .failure(let error) :
                               print([error as! Error])
                               break

                           }
    }

I got an error message : "error : 500, invalid token, data null". what's wrong with my code?

johnbrett commented 4 years ago

I don't see how this is related to this library, please create a PR with a failing test case if you want to debug an application issue.