Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Commit

Permalink
Created a BeamRequestDelegate to handle other JWT sources
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcook committed Dec 14, 2016
1 parent e42b02c commit 64367f7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
4 changes: 2 additions & 2 deletions BeamAPI.podspec
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Pod::Spec.new do |s|
s.name = "BeamAPI"
s.version = "1.4.3"
s.version = "1.4.4"
s.summary = "An interface to communicate with Beam's backend."
s.homepage = "https://github.com/WatchBeam/beam-client-swift"
s.license = "MIT"
s.author = { "Jack Cook" => "jack@beam.pro" }

s.requires_arc = true
s.ios.deployment_target = "8.2"
s.source = { :git => "https://github.com/WatchBeam/beam-client-swift.git", :tag => "1.4.3" }
s.source = { :git => "https://github.com/WatchBeam/beam-client-swift.git", :tag => "1.4.4" }
s.source_files = "Pod/Classes/**/*"

s.dependency "Starscream", "~> 2.0"
Expand Down
24 changes: 18 additions & 6 deletions Pod/Classes/Utilities/BeamRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import SwiftyJSON
/// The most low-level class used to make requests to the Beam servers.
public class BeamRequest {

/// A delegate for the BeamRequest class.
public static var delegate: BeamRequestDelegate?

/// The version of the app, to be used in request user agents.
public static var version = 0.1

Expand Down Expand Up @@ -185,13 +188,22 @@ public class BeamRequest {
}
case 401:
if json["message"] == "Invalid token" {
BeamClient.sharedClient.jwt.generateJWTGrant { (error) in
guard error == nil else {
completion?(data, .invalidCredentials)
return
if UserDefaults.standard.object(forKey: "Cookies") != nil {
BeamClient.sharedClient.jwt.generateJWTGrant { (error) in
guard error == nil else {
completion?(data, .invalidCredentials)
return
}

dataRequest(baseURL, requestType: requestType, headers: headers, params: params, body: body, options: options, completion: completion)
}
} else {
delegate?.requestNewJWT { token in
if token != nil {
UserDefaults.standard.set(token, forKey: "JWT")
dataRequest(baseURL, requestType: requestType, headers: headers, params: params, body: body, options: options, completion: completion)
}
}

dataRequest(baseURL, requestType: requestType, headers: headers, params: params, body: body, options: options, completion: completion)
}
} else {
requestError = .invalidCredentials
Expand Down
14 changes: 14 additions & 0 deletions Pod/Classes/Utilities/BeamRequestDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// BeamRequestDelegate.swift
// Pods
//
// Created by Jack Cook on 14/12/2016.
//
//

/// A delegate that allows clients to provide details to the BeamRequest class.
public protocol BeamRequestDelegate: class {

/// Provides the API client with a new JWT token to use in the event that one is needed.
func requestNewJWT(completion: (_ jwt: String?) -> Void)
}

0 comments on commit 64367f7

Please sign in to comment.