Swift 3:使用自定义令牌进行身份验证,使用服务器管理请求(node.js)

时间:2016-11-10 20:40:35

标签: node.js swift firebase firebase-authentication

Firebase文档中没有太多关于处理自定义令牌的信息。

我已经设置了node.js的服务器,但我不知道如何管理服务器的请求以将信息发送回应用程序,反之亦然。有关如何实现这一目标的任何资源?

这是node.js服务器

的代码
var serviceAccount = require('./service-account.json');
var firebase = require('firebase');
var path = require("path");
var admin = require('firebase-admin');
var FirebaseTokenGenerator = require("firebase-token-generator");
var refreshToken;

var uid = 'clientId';
var additionalClaims = {
  admin: true, manager: true, menuAccess: true
};
var clientIdToken = firebase.auth().createCustomToken(uid, additionalClaims);

console.log(clientIdToken)

firebase.initializeApp({

    serviceAccount: path.resolve(__dirname, '/Users/myUserName/Desktop/nodeClient/service-account.json'),
    databaseURL: "https://<firebase-project>.firebaseio.com/",
    databaseAuthVariableOverride: {
      uid: "clientId"
    }

}); 

在xcode中:

    func login (email: String, password: String, onComplete: Completion?) {
        self.showActivityIndicatory(uiView: self.uiView)
            FIRAuth.auth()?.signIn(withEmail: email, password: password, completion: { (user, error) in

            if error != nil {

                self.handleFirebaseError(error: error! as NSError, onComplete: onComplete)

                self.stopAnimating()

            } else {

                // User successfully logged in

                FIRAuth.auth()?.addStateDidChangeListener { auth, user in
                    if user != nil {

                        let userUpdate = ["provider": String(describing:FIRAuth.auth()?.currentUser?.providerID), "email": "email": String(describing: FIRAuth.auth()?.currentUser?.email!) ]
                        DataService.instance.registerUsers(uid: (user?.uid)!, user: userUpdate)

                        FIRAuth.auth()?.signIn(withCustomToken: "clientIdToken") {(user, error) in

                            if user != nil {
                                print("Got here somehow..")
                                print(user?.uid)

// as I create a custom token, this user?.uid should be "clientId", right?

                            } else {
                                print(error)

// I get an error here
                            }

                        }

                        print("Signed in")

                    } else {
                        print("NOT SIGNED IN")
                    }
                }

                let deadlineTime = DispatchTime.now() + .seconds(1)
                DispatchQueue.main.asyncAfter(deadline: deadlineTime) {

                    self.performSegue(withIdentifier: "HomeSegue", sender: nil)

                }

                }

            })

    }
  

可选(错误域= FIRAuthErrorDomain代码= 17000&#34;自定义令牌格式不正确。请查看文档。&#34; UserInfo = {NSLocalizedDescription =自定义令牌格式不正确。请查看文档。, ERROR_NAME = ERROR_INVALID_CUSTOM_TOKEN})

我该怎么办?我知道我必须从应用程序将凭据发送到我的服务器,但我如何实现这一目标?我正在考虑挖掘JWTAlamofire,我已经搜索了几天来解决这个特殊问题。我遇到过其他程序员处理同样的问题。

0 个答案:

没有答案