我使用以下代码来检测身份验证提供程序并正确注销
static func logOut() {
let auth = FIRAuth.auth()!
let provider = auth.currentUser?.providerID
switch provider! {
case "Facebook": FBSDKLoginManager().logOut()
case "Google": GIDSignIn.sharedInstance().signOut()
case "Twitter": Twitter.sharedInstance().sessionStore.logOutUserID(TWTRAPIClient.withCurrentUser().userID!)
default:
print("Unknown provider ID: \(provider!)")
return
}
try! auth.signOut()
}
但提供商总是" Firebase"。我究竟做错了什么? 0_o一旦该代码抛出" Facebook"当我登录twitter时。提前致谢
UPD:是的,我实际上可以在UserDefaults
中存储身份验证提供程序,但可能是它的Firebase错误。我使用的是Firebase SDK 3.5.2
答案 0 :(得分:23)
由于用户可以使用多个提供商登录其Firebase身份验证帐户,因此顶级提供商ID现在(通常)为Firebase
。
但currentUser
has a providerData
property提供有关特定提供商的信息。循环FIRAuth.auth()!.currentUser.providerData
将为您提供所需的FIRUserInfo.providerID
。
另请参阅有关UID的问题,这些情况类似:Firebase returns multiple IDs, Which is unique one?
答案 1 :(得分:7)
Swift 4 解决方案:
if let providerData = Auth.auth().currentUser?.providerData {
for userInfo in providerData {
switch userInfo.providerID {
case "facebook.com":
print("user is signed in with facebook")
case "google.com":
print("user is signed in with google")
default:
print("user is signed in with \(userInfo.providerID)")
}
}
}
答案 2 :(得分:1)
我不得不JSON.stringify(currentUser.providerData)
为了了解它的组织方式:
然后我终于找到了Auth Provider
,就像这样:
currentUser.providerData[0].providerId
干杯,看看您的代码:)
答案 3 :(得分:0)
要注销,有一个更简单的方法:
export class QuizComponent implements OnInit {
constructor(private http: HttpClient) { }
myData = { param1: 'This is param 1', param2: 'this is param 2' }
sendmydata(){
const req = this.http.post('http://myhost.com/phpapi/api.php',this.myData)
.subscribe(
res => {
console.log(res);
// how can I access res.status here?
res.status;//this line says Property 'status' does not exist on type 'Object'
},
err => {
console.log("Error occured");
}
);
}
另一方面,如果要查找提供程序并确定用户是否通过该提供程序登录,请检查accessToken。要获取accessToken,您需要提供给提供者的特定提供者实例。
我发现最好首先通过以下方式在您的班级中声明您的提供者:
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-
Allow-Headers, Authorization, X-Requested-With");
$db = "dbname";//Your database name
$dbu = "dbuser";//Your database username
$dbp = "dbpass";//Your database users' password
$host = "localhost";//MySQL server - usually localhost
$dblink = mysql_connect($host,$dbu,$dbp);
$seldb = mysql_select_db($db);
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$item1 = $request->param1;
$item2 = $request->param;
$sql = mysql_query("INSERT INTO `$db`.`table` (`id`,`item1`,`item2`)
VALUES ('','$item1','$item2');");
if($sql){
if (strcmp($item1, "") != 0) {
echo '{"status":"ok"}';
}
}else{
echo '{"status":"error"}';
}
mysql_close($dblink);//Close off the MySQL connection to save resources.
?>
然后,当您提供提供程序时:
let authUI = FUIAuth.defaultAuthUI()
do {
try authUI?.signOut()
} catch let err {
print(err);
}
当您需要特定提供者数据时:
lazy var facebookProvider = FUIFacebookAuth()
lazy var googleProvider = FUIGoogleAuth()
否则,无论用户是否实际登录,您都将获得有关每个提供商的信息。
答案 4 :(得分:0)
使用currentUser.providerData
为您提供了一组提供程序,每个提供程序都有自己的uid。该列表按用于登录的最新提供程序进行排序。因此currentUser.providerData
中的第一个元素是用户用于登录的方法。
因此,currentUser.providerData[0].providerId
将为您提供用户用来登录的方法。
答案 5 :(得分:0)
// Provider Type
struct AuthProviders {
static let phone = "phone"
static let facebook = "facebook.com"
static let google = "google.com"
static let apple = "apple.com"
}
let providerIds = auth.currentUser?.providerData.map { $0.providerID }