Skip to content

Instantly share code, notes, and snippets.

@mosluce
Last active February 12, 2018 05:25
Show Gist options
  • Save mosluce/b65cf314f3cc23a61bb659f9795f108b to your computer and use it in GitHub Desktop.
Save mosluce/b65cf314f3cc23a61bb659f9795f108b to your computer and use it in GitHub Desktop.
var token = "a JWT token"
// ...
var req = URLRequest(url: URL(string: "\(api_uri)/auth/refresh")!)
req.httpMethod = "POST"
req.addValue("Authorizetion", forHTTPHeaderField: token)
URLSession.shared.rx.data(request: req)
.map{ try! JSONDecoder().decode(TokenResult.self, from: $0) }
.map({ (result) -> TokenResult in
print(123) // 這裡不會印出...斷點也斷不到 ?
UserDefaults.standard.set(result.token, forKey: "token")
return result
})
.map{ try! JWTDecode.decode(jwt: $0.token) }
.subscribe(onNext: {[weak self] (jwt) in
self?.currentUser.onNext(AuthUser(uid: jwt.subject!,
displayName: jwt.claim(name: "displayName").string!,
level: jwt.claim(name: "level").integer!))
}, onError: {[weak self] (error) in
print(error)
self?.currentUser.onNext(nil)
}).disposed(by: disposeBag)
struct AuthUser {
var uid: String
var displayName: String
var level: Int
}
struct TokenResult: Codable {
var token: String
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment