Last active
February 12, 2018 05:25
-
-
Save mosluce/b65cf314f3cc23a61bb659f9795f108b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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