# Server Side Swift with Vapor - Part 28: API | Ray Wenderlich

**URL:** <https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141>\
**Category:** 🎓 Courses & Articles\
**Created:** [March 8, 2018, 8:03pm UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141 "2018-03-08T20:03:58Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![0xtim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/0xtim/32/111111_2.png) [@0xtim](https://forums.kodeco.com/u/0xtim)\
**Post date:** [March 8, 2018, 8:03pm UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/1 "2018-03-08T20:03:59Z")

</div>

Learn how to protect APIs with authentication using both HTTP Basic Authentication and Token Authentication.

* * *
 This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/4493-server-side-swift-with-vapor/lessons/28](https://www.raywenderlich.com/4493-server-side-swift-with-vapor/lessons/28)

---

<div class="post-metadata">

**Author:** ![vinamelody](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/vinamelody/32/97385_2.png) [@vinamelody](https://forums.kodeco.com/u/vinamelody)\
**Post date:** [April 5, 2018, 3:19pm UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/2 "2018-04-05T15:19:21Z")

</div>

Hi @0xtim… it’s me again … I’m on `Vapor Framework: 3.0.0-rc.2.2.3` now and getting this error. Standar procedure of `vapor update`, `brew upgrade vapor` and `rm -rf .build/ *.xcodeproj/ Package.resolved && vapor xcode -y` were done.

 ![07%20PM](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/5/1/5134f0e54ddcf3f4ee29fd4587d7a41efb234232.png)

---

<div class="post-metadata">

**Author:** ![0xtim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/0xtim/32/111111_2.png) [@0xtim](https://forums.kodeco.com/u/0xtim)\
**Post date:** [April 5, 2018, 3:51pm UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/3 "2018-04-05T15:51:47Z")

</div>

@vinamelody check out [the gist](https://gist.github.com/0xTim/b372f91c8bd71ea757d1da526058f2d9) for the latest changes. Once Vapor 3.0.0 is out (probably early next week) I’ll rerecord all the parts that need changing and update the projects

---

<div class="post-metadata">

**Author:** ![vinamelody](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/vinamelody/32/97385_2.png) [@vinamelody](https://forums.kodeco.com/u/vinamelody)\
**Post date:** [April 6, 2018, 2:02pm UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/4 "2018-04-06T14:02:44Z")

</div>

Thanks! it’s back to good now 🙂

---

<div class="post-metadata">

**Author:** ![jbrown36301](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jbrown36301/32/81494_2.png) [@jbrown36301](https://forums.kodeco.com/u/jbrown36301)\
**Post date:** [April 6, 2018, 8:57pm UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/5 "2018-04-06T20:57:21Z")

</div>

Also, make sure you increase the timeout preference in rested settings … I was timing out with a connection reset erroNo:54 until I did this… after changing teh time out value it worked but man was it slow… ![23%20PM](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/2/0/204c2bdb1132ac8c59b365a2f5b1e2adefe4f707.png)

---

<div class="post-metadata">

**Author:** ![0xtim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/0xtim/32/111111_2.png) [@0xtim](https://forums.kodeco.com/u/0xtim)\
**Post date:** [April 7, 2018, 9:34am UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/6 "2018-04-07T09:34:03Z")

</div>

@jbrown36301 is this specifically for logging in? Looks like there may have been a regression with the BCrypt cost and hashing is taking a long time. Though 20s is way too long! I’ll take a look.

If it’s not logging in, let me know because it shouldn’t take that long at all!

---

<div class="post-metadata">

**Author:** ![izzywizz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/izzywizz/32/81494_2.png) [@izzywizz](https://forums.kodeco.com/u/izzywizz)\
**Post date:** [April 9, 2018, 1:24pm UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/7 "2018-04-09T13:24:14Z")

</div>

@0xtim, @jbrown36301Unfortunately mine takes a while logging in as well otherwise, creating acronyms are lighting fast

---

<div class="post-metadata">

**Author:** ![0xtim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/0xtim/32/111111_2.png) [@0xtim](https://forums.kodeco.com/u/0xtim)\
**Post date:** [April 9, 2018, 2:08pm UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/8 "2018-04-09T14:08:51Z")

</div>

@izzywizz @jbrown36301 yeah this looks like a regression in BCrypt - it’s been captured here [BCrypt Regression · Issue #50 · vapor/open-crypto · GitHub](https://github.com/vapor/crypto/issues/50)

---

<div class="post-metadata">

**Author:** ![izzywizz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/izzywizz/32/81494_2.png) [@izzywizz](https://forums.kodeco.com/u/izzywizz)\
**Post date:** [April 10, 2018, 12:44pm UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/9 "2018-04-10T12:44:38Z")

</div>

@0xtim Ahhh yeah, hmmm 25s does seem excessive but I guess it does take awhile to hash

---

<div class="post-metadata">

**Author:** ![0xtim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/0xtim/32/111111_2.png) [@0xtim](https://forums.kodeco.com/u/0xtim)\
**Post date:** [April 10, 2018, 11:14pm UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/10 "2018-04-10T23:14:57Z")

</div>

@izzywizz it shouldn’t take that long unless you want REALLY secure passwords! It’s past the point of being usable.

However it’s an issue with Swift debug vs release builds - on release it’s actually fine. There’s a fix in the issue linked above, I’ll add it to the video re-records

---

<div class="post-metadata">

**Author:** ![izzywizz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/izzywizz/32/81494_2.png) [@izzywizz](https://forums.kodeco.com/u/izzywizz)\
**Post date:** [April 11, 2018, 8:45am UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/11 "2018-04-11T08:45:39Z")

</div>

@0xtim You are right that does seem a tad extreme haha but it’s cool that it doesn’t happen in release though.

---

<div class="post-metadata">

**Author:** ![dpenny](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/dpenny/32/98604_2.png) [@dpenny](https://forums.kodeco.com/u/dpenny)\
**Post date:** [May 10, 2018, 1:44pm UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/12 "2018-05-10T13:44:07Z")

</div>

@0xtim There seem to have been some recent changes to the Auth (2.0.0-rc.4) package that are causing issues with token authentication.

1. `typealias UserIDType = UUID` needs to be added to the `Token: Authentication.Token` extension in `Token.swift`
2. There is an error in `User.swift` stating that `Type 'User' does not conform to protocol 'TokenAuthenticatable'`. I haven’t been able to figure this one out yet. It looks like there may be a new function in the protocol, `authenticate`, that needs to be implemented. Can you provide some assistance with this?

Thanks,  
Doug

---

<div class="post-metadata">

**Author:** ![0xtim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/0xtim/32/111111_2.png) [@0xtim](https://forums.kodeco.com/u/0xtim)\
**Post date:** [May 10, 2018, 3:51pm UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/13 "2018-05-10T15:51:09Z")

</div>

Just to clear this up you should just need to add a type alias to `UserIDType` to your Token. E.g.

```swift
extension Token: Authentication.Token {
  static let userIDKey: UserIDKey = \Token.userID
  typealias UserType = User
  typealias UserIDType = User.ID
}

```

I’ve raised it [here](https://github.com/vapor/auth/issues/41)

---

<div class="post-metadata">

**Author:** ![dpenny](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/dpenny/32/98604_2.png) [@dpenny](https://forums.kodeco.com/u/dpenny)\
**Post date:** [May 10, 2018, 3:55pm UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/14 "2018-05-10T15:55:40Z")

</div>

And to clarify my previous post, point 2 was simply an Xcode issue. Doing a clean and build, cleared the error. Thanks for the response.

---

<div class="post-metadata">

**Author:** ![gargoyle](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gargoyle/32/107285_2.png) [@gargoyle](https://forums.kodeco.com/u/gargoyle)\
**Post date:** [June 17, 2018, 5:48am UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/15 "2018-06-17T05:48:00Z")

</div>

How do we authenticate if we’re just using something like `curl` to test out the API calls? I tried calling my login method from the web controller with the `-c cookie-jar.txt` option to curl, and I see that it does in fact write out a cookie file.

But now when I make my POST to the API and add the `-b cookie-jar.txt` to curl, I’m still getting a 401 error from the API.

---

<div class="post-metadata">

**Author:** ![0xtim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/0xtim/32/111111_2.png) [@0xtim](https://forums.kodeco.com/u/0xtim)\
**Post date:** [June 18, 2018, 7:00am UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/16 "2018-06-18T07:00:02Z")

</div>

For curl you can use the API login fuction to get a token and then send that token as header in the next requests. The API uses the tokens, only the web uses cookies

---

<div class="post-metadata">

**Author:** ![gargoyle](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gargoyle/32/107285_2.png) [@gargoyle](https://forums.kodeco.com/u/gargoyle)\
**Post date:** [June 18, 2018, 7:51pm UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/17 "2018-06-18T19:51:14Z")

</div>

“Palm, this is your forehead. Forehead, meet your palm”

😳

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [August 5, 2018, 9:56pm UTC](https://forums.kodeco.com/t/server-side-swift-with-vapor-part-28-api-ray-wenderlich/40141/19 "2018-08-05T21:56:08Z")

</div>

Opened on August 5, 2018, 9:56pm UTC


