2016-11-24 45 views
0

我正在使用firebase作爲後端編寫ios應用程序。 在我的應用程序中,有一些管理員應該能夠創建新用戶。新用戶不應該能夠自己創建帳戶。在沒有自動登錄的情況下在ios上創建Firebase用戶

在網頁上,我使用的功能

ref.createUser({ 
     email: ctrl.user.email, 
     password: ctrl.user.password 
    }, function(error, userData) { // } 

隨機密碼,之後重置密碼。因此,用戶將收到一封帶密碼重置鏈接的電子郵件,然後可以設置新密碼並登錄。創建用戶後,我將所有相關的用戶數據存儲到數據庫中。

iOS上我試圖創建一個新用戶:

FIRAuth.auth()?.createUser(withEmail: email, password: password) { (user, error) in } 

但如果我這樣做,就這樣,我與我的管理員賬戶退出,並與我的新創建的帳戶登錄。由於這個原因,我無法將用戶數據存儲到數據庫,因爲這僅限於管理員。...

有沒有辦法讓ios只創建一個用戶,而不用自動登錄這個用戶?

最良好的祝願

託比

編輯 我寫了一個AWS lambda函數與火力的Admin SDK用於node.js的,並通過Web服務創建函數內部的用戶和觸發功能來自我的iOS應用程序。 不完美,但工作....

回答

0

是的,調用createUser(withEmail:, password:)將登錄一個新的用戶。要達到什麼樣的你正在嘗試做的,我建議做這樣的一種解決方法(假設當前的管理員已登錄):

// Create the values 
var email = alreadySelectedUserEmail 
var password = arc4random_uniform(1000000) // or another random number 

// Send the information to the database 
FIRDatabase.database().reference().child("user_data").child(email) . 
     setValue("lorem ipsum") // or another value 

// THEN create the user, AFTER sending the data 
     // This will automatically log you in to the new account 
FIRAuth.auth()?.createUser(withEmail: email, 
     password: password) { (user, error) in 
    // Check for any errors 
    if let error = error { 
     print error 
    } else { 
     // If there are none, LOG OUT… 
     try! FIRAuth.auth()!.signOut() 

     // …and log back into Admin (with whatever login info they have) 
     FIRAuth.auth()?.signIn(withEmail: adminEmail, 
       password: adminPassword) { (user, error) in 
      // Check for any errors 
      if let error = error { 
       print error 
      } else { 
       // All done, new account created! 
      } 
    } 
} 
+0

有過類似的想法,但我如何得到ADMINPASSWORD? 我可以在初始管理員登錄時以UserDefault-Value的形式存儲它,但是如果管理員更改了密碼,那又如何呢? – Tobi

+0

現在,我已經爲Node.JS編寫了一個AWS Lambda函數和Firebase Admin SDK,並通過來自ios-app的webservice調用創建用戶。 – Tobi

相關問題