我的目标非常简单,通过socket.io jwt对用户进行身份验证。这样我就可以实时操纵他/她的数据。
客户代码
import SocketIOClientSwift
import KeychainAccess
class SocketIOManager: NSObject {
static let sharedInstance = SocketIOManager()
// 1
let keychain = Keychain(server: "https://testing.herokuapp.com", protocolType: .HTTPS)
//2
var token: String {
get {
if let realToken = keychain["token"] {
return String(realToken)
} else {
return ""
}
}
}
//3
lazy var socket: SocketIOClient = SocketIOClient(socketURL: NSURL(string: "https://testing.herokuapp.com")!,
options: [.Log(true), .ConnectParams(["token": self.token])])
override init() {
super.init()
}
func establishConnection() {
socket.connect()
}
func closeConnection() {
socket.disconnect()
}
}
只为了你们的理解
keychain["token"] = "token"
,我们假设令牌已经存在
保存var token
是获取令牌,因为目前是钥匙串存储
可选的令牌,因此我需要做可选的绑定。.ConnectParams(["token": self.token])
到目前为止一直很好,但是现在,只要我尝试在app委托中建立连接,
AppDelegate.Swift
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
SocketIOManager.sharedInstance.establishConnection()
}
我会收到
Handling event: error with data: (
{
code = "invalid_token";
message = "secret or public key must be provided";
type = UnauthorizedError;
} )
为清楚起见,我不想包含服务器端代码,因为它只是来自此https://github.com/auth0/socketio-jwt的样板代码
所以,真正的问题是,我做错了什么?
答案 0 :(得分:1)
您似乎没有在服务器实施中提供秘密:
socketioJwt.authorize({
secret: 'some secret here', // <--
handshake: true
})
答案 1 :(得分:-1)
在你的 .env 中添加 JWT_SECRET=JWT_SECRET