# Access token when obtained from iOS results in 401 unauthorized while from react-admin is ok

**URL:** <https://community.auth0.com/t/access-token-when-obtained-from-ios-results-in-401-unauthorized-while-from-react-admin-is-ok/71115>\
**Category:** Get Help\
**Tags:** swift, access-token, nestjs\
**Created:** [October 11, 2021, 8:56am UTC](https://community.auth0.com/t/access-token-when-obtained-from-ios-results-in-401-unauthorized-while-from-react-admin-is-ok/71115 "2021-10-11T08:56:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kennethphough](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kennethphough/32/12848_2.png) [@kennethphough](https://community.auth0.com/u/kennethphough)\
**Post date:** [October 11, 2021, 8:56am UTC](https://community.auth0.com/t/access-token-when-obtained-from-ios-results-in-401-unauthorized-while-from-react-admin-is-ok/71115/1 "2021-10-11T08:56:58Z")

</div>

I have a nestJS backend, a react-admin front-end and a mobile app written in swift. When I access the backend from the react-admin front end I am able to successfully login and make requests to the backend. However, with the iOS app, I am able to successfully login but am unable to make requests. Requests made result in 401 unauthorized. I have posted this issue on stack overflow as well with screenshots and code snippets.

[https://stackoverflow.com/questions/69522011/swift-auth0-access-code-results-in-unauthorized-access-but-nothing-in-auth0-logs](https://stackoverflow.com/questions/69522011/swift-auth0-access-code-results-in-unauthorized-access-but-nothing-in-auth0-logs)

The swift code used is as follows:

```auto
let path = "\(baseURL)\(endpoint.rawValue)"
        
guard let url = URL(string: path)
else { preconditionFailure("Bad URL") }
        
var headers: [String:String] = [:]
headers["Content-Type"] = "application/json"
        
// if access token is set then set Authorization headers
if (accessToken != nil) {
    headers["Authorization"] = "Bearer \(accessToken!)"
    print("Bearer \(accessToken!)")
}
        
var request = URLRequest(url: url)
request.httpMethod = "\(method)"
request.allHTTPHeaderFields = headers
        
// check if body exists
if (body != nil) {
    request.httpBody = body!
}

let dataTask = URLSession.shared.dataTask(with: request) {
    (data, response, error) in
    guard error == nil
    else { completion(.failure(.serverError)); return }
            
    do {
        guard let data = data
        else { completion(.failure(.serverError)); return }
                
        guard let object : [[String: AnyObject]] = try JSONSerialization.object(with: data) as? [[String: AnyObject]]
        else {
            print("Unable to convert from data")
            return
        }
        
        guard let json = try? JSONSerialization.data(withJSONObject: object, options: .prettyPrinted)
        else {
            print("Unable to prettify")
            return
        }

        guard let jsonString = String(data: json, encoding: .utf8)
        else {
            print("Unable to convert to string")
            return
        }
                
         print("JSON: \(jsonString)")
                
         completion(Result.success(object))
     } catch {
         completion(Result.failure(.parsingError))
     }
}
dataTask.resume()

```

`baseURL` is a string that points to my nestJS backend. `endpoint` is an enum of endpoints, for example `\user`

Using Proxyman I am able to confirm that the endpoint is hit with the correct headers. Screenshot below.

 ![Screen Shot 2021-10-11 at 3.12.04 AM](https://global.discourse-cdn.com/auth0/original/3X/0/6/06f00ba6459763f5f3309823537f12e0c14789e6.png)

Additionally, using postman I am able to successfully login and also make a get request to protected data. Screenshot below.

 ![Screen Shot 2021-10-11 at 3.28.46 AM](https://global.discourse-cdn.com/auth0/original/3X/7/3/73d692122ba05d3a9847165b3a264bd17ae94e87.png)

I am using Auth0 1.0 for swift (installed via pods). Any pointers to what might be the problem would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![kennethphough](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/kennethphough/32/12848_2.png) [@kennethphough](https://community.auth0.com/u/kennethphough)\
**Post date:** [October 11, 2021, 5:37pm UTC](https://community.auth0.com/t/access-token-when-obtained-from-ios-results-in-401-unauthorized-while-from-react-admin-is-ok/71115/2 "2021-10-11T17:37:35Z")

</div>

The problem was that `audience` was set when requesting access tokens from the react-admin, while I did not include this in the swift login implementation.

Decoding the JWT on [jwt.io](http://jwt.io) and the following thread lead to this conclusion.  
[https://community.auth0.com/t/access-token-too-short-jwt-malformed/9169/11?u=kennethphough](https://community.auth0.com/t/access-token-too-short-jwt-malformed/9169/11)

Adding the audience in the following code resulted in the correct jwt being returned and successful access to backend.

```auto
Auth0
    .authentication()
    .login(
        usernameOrEmail: self.email,
        password: self.password,
        realm: "Username-Password-Authentication",
        audience: "<YOUR_AUDIENCE>", // <- This is what I forgot
        scope: "openid profile email"
)

```

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [October 12, 2021, 9:26am UTC](https://community.auth0.com/t/access-token-when-obtained-from-ios-results-in-401-unauthorized-while-from-react-admin-is-ok/71115/3 "2021-10-12T09:26:46Z")

</div>

Perfect! Glad to hear that and thanks for sharing with the rest of community!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [October 27, 2021, 9:27am UTC](https://community.auth0.com/t/access-token-when-obtained-from-ios-results-in-401-unauthorized-while-from-react-admin-is-ok/71115/4 "2021-10-27T09:27:41Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
