Why did Auth.auth().currentUser?.uid return a nil value?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I want to store users under their Firebase generated uid. When I attempt to use this code:
func showCredentials() {
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
Auth.auth().signInAndRetrieveData(with: credential) { (AuthDataResult, error) in
if error != nil {
print("Something went wrong with our Facebook User: ", error)
return
}
print("Successfuly logged in with our Facebook User: ", AuthDataResult)
self.performSegue(withIdentifier: "FacebookSegue", sender: self)
}
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {
(connection, graphResult, error) in
if error != nil {
print("Failed to Start Graph Request:", error)
return
}
print(graphResult)
let facebookDetail = graphResult as! NSDictionary
let userID = facebookDetail["id"]
let fullName = facebookDetail["name"]
let email = facebookDetail["email"]
let providerName = "Facebook"
let userValues = (["Email": email, "Full Name": fullName, "UID": userID, "Provider":providerName])
let databaseRef = Database.database().reference()
let key = databaseRef.child("node").childByAutoId().key ?? ""
let childUpdates = ["/Users/(userID))/": userValues]
databaseRef.updateChildValues(childUpdates)
print(userID!)
print(fullName!)
let firebaseID = Auth.auth().currentUser?.uid
print(firebaseID!)
}
}
The error I am given is: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value on the line where I am attempting to print the firebaseID. Why is the value of Auth.auth().currentUser?.uid nil and what can I do to obtain this value?
ios swift xcode
add a comment |
I want to store users under their Firebase generated uid. When I attempt to use this code:
func showCredentials() {
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
Auth.auth().signInAndRetrieveData(with: credential) { (AuthDataResult, error) in
if error != nil {
print("Something went wrong with our Facebook User: ", error)
return
}
print("Successfuly logged in with our Facebook User: ", AuthDataResult)
self.performSegue(withIdentifier: "FacebookSegue", sender: self)
}
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {
(connection, graphResult, error) in
if error != nil {
print("Failed to Start Graph Request:", error)
return
}
print(graphResult)
let facebookDetail = graphResult as! NSDictionary
let userID = facebookDetail["id"]
let fullName = facebookDetail["name"]
let email = facebookDetail["email"]
let providerName = "Facebook"
let userValues = (["Email": email, "Full Name": fullName, "UID": userID, "Provider":providerName])
let databaseRef = Database.database().reference()
let key = databaseRef.child("node").childByAutoId().key ?? ""
let childUpdates = ["/Users/(userID))/": userValues]
databaseRef.updateChildValues(childUpdates)
print(userID!)
print(fullName!)
let firebaseID = Auth.auth().currentUser?.uid
print(firebaseID!)
}
}
The error I am given is: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value on the line where I am attempting to print the firebaseID. Why is the value of Auth.auth().currentUser?.uid nil and what can I do to obtain this value?
ios swift xcode
1
I had this issue before when attempting to retrieve a user's uid. Your's is not guaranteed to return the current user's information which is why the uid and likely the current user information is nil. You'll need to wait for the response to come back before you can retrieve the user's information. You might be able to init a global var uid and then assign it a value once the value has come back. Maybe this can help github.com/ThriveCommunityChurch/ThriveChurchOfficialApp/issues/…
– KSigWyatt
Nov 17 '18 at 5:16
add a comment |
I want to store users under their Firebase generated uid. When I attempt to use this code:
func showCredentials() {
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
Auth.auth().signInAndRetrieveData(with: credential) { (AuthDataResult, error) in
if error != nil {
print("Something went wrong with our Facebook User: ", error)
return
}
print("Successfuly logged in with our Facebook User: ", AuthDataResult)
self.performSegue(withIdentifier: "FacebookSegue", sender: self)
}
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {
(connection, graphResult, error) in
if error != nil {
print("Failed to Start Graph Request:", error)
return
}
print(graphResult)
let facebookDetail = graphResult as! NSDictionary
let userID = facebookDetail["id"]
let fullName = facebookDetail["name"]
let email = facebookDetail["email"]
let providerName = "Facebook"
let userValues = (["Email": email, "Full Name": fullName, "UID": userID, "Provider":providerName])
let databaseRef = Database.database().reference()
let key = databaseRef.child("node").childByAutoId().key ?? ""
let childUpdates = ["/Users/(userID))/": userValues]
databaseRef.updateChildValues(childUpdates)
print(userID!)
print(fullName!)
let firebaseID = Auth.auth().currentUser?.uid
print(firebaseID!)
}
}
The error I am given is: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value on the line where I am attempting to print the firebaseID. Why is the value of Auth.auth().currentUser?.uid nil and what can I do to obtain this value?
ios swift xcode
I want to store users under their Firebase generated uid. When I attempt to use this code:
func showCredentials() {
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
Auth.auth().signInAndRetrieveData(with: credential) { (AuthDataResult, error) in
if error != nil {
print("Something went wrong with our Facebook User: ", error)
return
}
print("Successfuly logged in with our Facebook User: ", AuthDataResult)
self.performSegue(withIdentifier: "FacebookSegue", sender: self)
}
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {
(connection, graphResult, error) in
if error != nil {
print("Failed to Start Graph Request:", error)
return
}
print(graphResult)
let facebookDetail = graphResult as! NSDictionary
let userID = facebookDetail["id"]
let fullName = facebookDetail["name"]
let email = facebookDetail["email"]
let providerName = "Facebook"
let userValues = (["Email": email, "Full Name": fullName, "UID": userID, "Provider":providerName])
let databaseRef = Database.database().reference()
let key = databaseRef.child("node").childByAutoId().key ?? ""
let childUpdates = ["/Users/(userID))/": userValues]
databaseRef.updateChildValues(childUpdates)
print(userID!)
print(fullName!)
let firebaseID = Auth.auth().currentUser?.uid
print(firebaseID!)
}
}
The error I am given is: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value on the line where I am attempting to print the firebaseID. Why is the value of Auth.auth().currentUser?.uid nil and what can I do to obtain this value?
ios swift xcode
ios swift xcode
asked Nov 17 '18 at 0:42
Justin ReddickJustin Reddick
286
286
1
I had this issue before when attempting to retrieve a user's uid. Your's is not guaranteed to return the current user's information which is why the uid and likely the current user information is nil. You'll need to wait for the response to come back before you can retrieve the user's information. You might be able to init a global var uid and then assign it a value once the value has come back. Maybe this can help github.com/ThriveCommunityChurch/ThriveChurchOfficialApp/issues/…
– KSigWyatt
Nov 17 '18 at 5:16
add a comment |
1
I had this issue before when attempting to retrieve a user's uid. Your's is not guaranteed to return the current user's information which is why the uid and likely the current user information is nil. You'll need to wait for the response to come back before you can retrieve the user's information. You might be able to init a global var uid and then assign it a value once the value has come back. Maybe this can help github.com/ThriveCommunityChurch/ThriveChurchOfficialApp/issues/…
– KSigWyatt
Nov 17 '18 at 5:16
1
1
I had this issue before when attempting to retrieve a user's uid. Your's is not guaranteed to return the current user's information which is why the uid and likely the current user information is nil. You'll need to wait for the response to come back before you can retrieve the user's information. You might be able to init a global var uid and then assign it a value once the value has come back. Maybe this can help github.com/ThriveCommunityChurch/ThriveChurchOfficialApp/issues/…
– KSigWyatt
Nov 17 '18 at 5:16
I had this issue before when attempting to retrieve a user's uid. Your's is not guaranteed to return the current user's information which is why the uid and likely the current user information is nil. You'll need to wait for the response to come back before you can retrieve the user's information. You might be able to init a global var uid and then assign it a value once the value has come back. Maybe this can help github.com/ThriveCommunityChurch/ThriveChurchOfficialApp/issues/…
– KSigWyatt
Nov 17 '18 at 5:16
add a comment |
1 Answer
1
active
oldest
votes
You init 2 asynchronous requests at the same time
Auth.auth().signInAndRetrieveData and FBSDKGraphRequest(graphPath: "/me"
where the second may finish first causing the crash as the auth isn't yet finished , so you need to nest them to be sure that you access the user after the auth signin finishes
func showCredentials() {
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
Auth.auth().signInAndRetrieveData(with: credential) { (AuthDataResult, error) in
if error != nil {
print("Something went wrong with our Facebook User: ", error)
return
}
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {
(connection, graphResult, error) in
if error != nil {
print("Failed to Start Graph Request:", error)
return
}
print(graphResult)
let facebookDetail = graphResult as! NSDictionary
let userID = facebookDetail["id"]
let fullName = facebookDetail["name"]
let email = facebookDetail["email"]
let providerName = "Facebook"
let userValues = (["Email": email, "Full Name": fullName, "UID": userID, "Provider":providerName])
let databaseRef = Database.database().reference()
let key = databaseRef.child("node").childByAutoId().key ?? ""
let childUpdates = ["/Users/(userID))/": userValues]
databaseRef.updateChildValues(childUpdates)
print(userID!)
print(fullName!)
let firebaseID = Auth.auth().currentUser?.uid
print(firebaseID!)
DispatchQueue.main.async {
print("Successfuly logged in with our Facebook User: ", AuthDataResult)
self.performSegue(withIdentifier: "FacebookSegue", sender: self)
}
}
}
}
I know that I wanted to grab that Auth.auth().currentUser?.uid token as a string and store users under their appropriate token.
– Justin Reddick
Nov 17 '18 at 0:58
@JustinReddick you should mark this as the correct answer
– Brian Ogden
Jan 10 at 4:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347132%2fwhy-did-auth-auth-currentuser-uid-return-a-nil-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You init 2 asynchronous requests at the same time
Auth.auth().signInAndRetrieveData and FBSDKGraphRequest(graphPath: "/me"
where the second may finish first causing the crash as the auth isn't yet finished , so you need to nest them to be sure that you access the user after the auth signin finishes
func showCredentials() {
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
Auth.auth().signInAndRetrieveData(with: credential) { (AuthDataResult, error) in
if error != nil {
print("Something went wrong with our Facebook User: ", error)
return
}
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {
(connection, graphResult, error) in
if error != nil {
print("Failed to Start Graph Request:", error)
return
}
print(graphResult)
let facebookDetail = graphResult as! NSDictionary
let userID = facebookDetail["id"]
let fullName = facebookDetail["name"]
let email = facebookDetail["email"]
let providerName = "Facebook"
let userValues = (["Email": email, "Full Name": fullName, "UID": userID, "Provider":providerName])
let databaseRef = Database.database().reference()
let key = databaseRef.child("node").childByAutoId().key ?? ""
let childUpdates = ["/Users/(userID))/": userValues]
databaseRef.updateChildValues(childUpdates)
print(userID!)
print(fullName!)
let firebaseID = Auth.auth().currentUser?.uid
print(firebaseID!)
DispatchQueue.main.async {
print("Successfuly logged in with our Facebook User: ", AuthDataResult)
self.performSegue(withIdentifier: "FacebookSegue", sender: self)
}
}
}
}
I know that I wanted to grab that Auth.auth().currentUser?.uid token as a string and store users under their appropriate token.
– Justin Reddick
Nov 17 '18 at 0:58
@JustinReddick you should mark this as the correct answer
– Brian Ogden
Jan 10 at 4:55
add a comment |
You init 2 asynchronous requests at the same time
Auth.auth().signInAndRetrieveData and FBSDKGraphRequest(graphPath: "/me"
where the second may finish first causing the crash as the auth isn't yet finished , so you need to nest them to be sure that you access the user after the auth signin finishes
func showCredentials() {
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
Auth.auth().signInAndRetrieveData(with: credential) { (AuthDataResult, error) in
if error != nil {
print("Something went wrong with our Facebook User: ", error)
return
}
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {
(connection, graphResult, error) in
if error != nil {
print("Failed to Start Graph Request:", error)
return
}
print(graphResult)
let facebookDetail = graphResult as! NSDictionary
let userID = facebookDetail["id"]
let fullName = facebookDetail["name"]
let email = facebookDetail["email"]
let providerName = "Facebook"
let userValues = (["Email": email, "Full Name": fullName, "UID": userID, "Provider":providerName])
let databaseRef = Database.database().reference()
let key = databaseRef.child("node").childByAutoId().key ?? ""
let childUpdates = ["/Users/(userID))/": userValues]
databaseRef.updateChildValues(childUpdates)
print(userID!)
print(fullName!)
let firebaseID = Auth.auth().currentUser?.uid
print(firebaseID!)
DispatchQueue.main.async {
print("Successfuly logged in with our Facebook User: ", AuthDataResult)
self.performSegue(withIdentifier: "FacebookSegue", sender: self)
}
}
}
}
I know that I wanted to grab that Auth.auth().currentUser?.uid token as a string and store users under their appropriate token.
– Justin Reddick
Nov 17 '18 at 0:58
@JustinReddick you should mark this as the correct answer
– Brian Ogden
Jan 10 at 4:55
add a comment |
You init 2 asynchronous requests at the same time
Auth.auth().signInAndRetrieveData and FBSDKGraphRequest(graphPath: "/me"
where the second may finish first causing the crash as the auth isn't yet finished , so you need to nest them to be sure that you access the user after the auth signin finishes
func showCredentials() {
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
Auth.auth().signInAndRetrieveData(with: credential) { (AuthDataResult, error) in
if error != nil {
print("Something went wrong with our Facebook User: ", error)
return
}
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {
(connection, graphResult, error) in
if error != nil {
print("Failed to Start Graph Request:", error)
return
}
print(graphResult)
let facebookDetail = graphResult as! NSDictionary
let userID = facebookDetail["id"]
let fullName = facebookDetail["name"]
let email = facebookDetail["email"]
let providerName = "Facebook"
let userValues = (["Email": email, "Full Name": fullName, "UID": userID, "Provider":providerName])
let databaseRef = Database.database().reference()
let key = databaseRef.child("node").childByAutoId().key ?? ""
let childUpdates = ["/Users/(userID))/": userValues]
databaseRef.updateChildValues(childUpdates)
print(userID!)
print(fullName!)
let firebaseID = Auth.auth().currentUser?.uid
print(firebaseID!)
DispatchQueue.main.async {
print("Successfuly logged in with our Facebook User: ", AuthDataResult)
self.performSegue(withIdentifier: "FacebookSegue", sender: self)
}
}
}
}
You init 2 asynchronous requests at the same time
Auth.auth().signInAndRetrieveData and FBSDKGraphRequest(graphPath: "/me"
where the second may finish first causing the crash as the auth isn't yet finished , so you need to nest them to be sure that you access the user after the auth signin finishes
func showCredentials() {
let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
Auth.auth().signInAndRetrieveData(with: credential) { (AuthDataResult, error) in
if error != nil {
print("Something went wrong with our Facebook User: ", error)
return
}
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {
(connection, graphResult, error) in
if error != nil {
print("Failed to Start Graph Request:", error)
return
}
print(graphResult)
let facebookDetail = graphResult as! NSDictionary
let userID = facebookDetail["id"]
let fullName = facebookDetail["name"]
let email = facebookDetail["email"]
let providerName = "Facebook"
let userValues = (["Email": email, "Full Name": fullName, "UID": userID, "Provider":providerName])
let databaseRef = Database.database().reference()
let key = databaseRef.child("node").childByAutoId().key ?? ""
let childUpdates = ["/Users/(userID))/": userValues]
databaseRef.updateChildValues(childUpdates)
print(userID!)
print(fullName!)
let firebaseID = Auth.auth().currentUser?.uid
print(firebaseID!)
DispatchQueue.main.async {
print("Successfuly logged in with our Facebook User: ", AuthDataResult)
self.performSegue(withIdentifier: "FacebookSegue", sender: self)
}
}
}
}
edited Nov 17 '18 at 1:17
answered Nov 17 '18 at 0:56
Sh_KhanSh_Khan
48.4k51434
48.4k51434
I know that I wanted to grab that Auth.auth().currentUser?.uid token as a string and store users under their appropriate token.
– Justin Reddick
Nov 17 '18 at 0:58
@JustinReddick you should mark this as the correct answer
– Brian Ogden
Jan 10 at 4:55
add a comment |
I know that I wanted to grab that Auth.auth().currentUser?.uid token as a string and store users under their appropriate token.
– Justin Reddick
Nov 17 '18 at 0:58
@JustinReddick you should mark this as the correct answer
– Brian Ogden
Jan 10 at 4:55
I know that I wanted to grab that Auth.auth().currentUser?.uid token as a string and store users under their appropriate token.
– Justin Reddick
Nov 17 '18 at 0:58
I know that I wanted to grab that Auth.auth().currentUser?.uid token as a string and store users under their appropriate token.
– Justin Reddick
Nov 17 '18 at 0:58
@JustinReddick you should mark this as the correct answer
– Brian Ogden
Jan 10 at 4:55
@JustinReddick you should mark this as the correct answer
– Brian Ogden
Jan 10 at 4:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347132%2fwhy-did-auth-auth-currentuser-uid-return-a-nil-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I had this issue before when attempting to retrieve a user's uid. Your's is not guaranteed to return the current user's information which is why the uid and likely the current user information is nil. You'll need to wait for the response to come back before you can retrieve the user's information. You might be able to init a global var uid and then assign it a value once the value has come back. Maybe this can help github.com/ThriveCommunityChurch/ThriveChurchOfficialApp/issues/…
– KSigWyatt
Nov 17 '18 at 5:16