---
title: "#228: Standard Setup - Authentication with Moya 🚪"
url: https://littlebitesofcocoa.com/228-standard-setup-authentication-with-moya
author: Jake Marsh
date: 2016-04-06
published: 2016-04-06
topics: ["Standard Setup"]
description: "Last week we in Bite #223 and #224 we covered an example \"Standard Setup\" for new apps. Since those Bites were published, readers have been asking many..."
---

# #228: Standard Setup - Authentication with Moya 🚪

By Jake Marsh, April 6, 2016. HTML: https://littlebitesofcocoa.com/228-standard-setup-authentication-with-moya

Last week we in [Bite #223](https://littlebitesofcocoa.com/223) and [#224](https://littlebitesofcocoa.com/224) we covered an example _"Standard Setup"_ for new **apps**. Since those **Bites** were published, readers have been asking many questions, requesting more information about how all these things fit together. 

Today we'll answer one of those questions in more depth: Authentication. Let's dive in.

**Who?** [Again](https://littlebitesofcocoa.com/223) it's me, [Jake Marsh](https://twitter.com/jakemarsh)! I write [this thing](https://littlebitesofcocoa.com/about). 

Like we covered before, the heart of our HTTP "stack" is built around the [Moya framework](https://github.com/Moya/Moya) ([Bite #150](https://littlebitesofcocoa.com/150)). One of the neat parts about [Moya](https://github.com/Moya/Moya) is how it lets us customize how it works. One way is through an **"request closure"**. As the name implies, this closure's job is to create the actual **NSURLRequest** that will be sent.

Here's an example implementation. Inside we can set **HTTP headers**, and modify the **URL request** in any way we want.  This makes adding authentication super simple.
Here we set a "Bearer" token as the **Authorization** header.

``` swift
let requestClosure = { (endpoint: RxMoya.Endpoint<StewardAPI>, done: NSURLRequest -> Void) in
  let request = endpoint.urlRequest.mutableCopy() as! NSMutableURLRequest
  
  if let token = SharedKeycard.token {
    request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
  }

  done(request)
}
```

The **SharedKeycard** is simply a singleton that holds the auth token:

``` swift
func lookForTokenInKeychain() -> String? { /* not important */ }

public class Keycard : NSObject {
  public var token: String? = nil

  public override init() {
    super.init()    
    token = lookForTokenInKeychain()
  }
}

public let SharedKeycard = Keycard()
```

Finally, we simply pass in our custom **request closure** when creating our **Moya "provider"**:

``` swift
let StewardAPI = RxMoyaProvider<StewardAPI>(
  requestClosure: requestClosure
)
```

A quick shout out to reader [Justin Williams](https://twitter.com/justin) who [wrote in](https://twitter.com/justin/status/716729616879063044) asking for more information about how authentication fits into this puzzle.

Have any questions about how all this fits together? Want to see some other developer's example setup? Don't be shy! [hello@littlebitesofcocoa.com](mailto:hello@littlebitesofcocoa.com). Any and all comments/questions are welcome!
