(Xcode 6 Swift Language) saving users data (username, userEmail, userPassword) and send it to database - swift

I'm making an IOS app for iPhone that required users to register. I want to send data to a service sites and then record it on database, but i only know how to save it locally, by using this code:
NSUserDefaults.standardUserDefaults().setObject(userEmail, forKey:"userEmail");
NSUserDefaults.standardUserDefaults().setObject(userEmail, forKey:"userPasswoed");
NSUserDefaults.standardUserDefaults().synchronize();
So the question is how can I save the users data and then send it to a service sites and record it in database (using the Swift language)?

By database, I'm guessing you mean something like parse. You can register for Parse at parse.com and go through the steps in quick start to set your app up. Parse creates a user class for you then you can create a variable for the username, email and password and send that data to parse using the pfuser class. Once you have done all of that, this is the code you can use to set those properties and send them to parse.
let user = PFUser()
user.username = username
user.email = userEmail
user.password = password

Related

How to retrieve name of user in firebase

I am creating an app (Xcode, swift) that has a profile page for each user and I want their name to appear on that page.
I have been able to get their email address through:
let email : String = (Auth.auth().currentUser?.email)!
How would I gather the users name? I have the users UID as well.
I am using firebase by the way
If you are not using Google or Facebook to log in with firebase, You need to manually create the profile for each user. See Update a user's profile
If you're using a social provider to sign in, you can get the display name from that provider through Firebase with:
Auth.auth().currentUser?.displayName
If you're signing in with another provider, the display name won't automatically be set, and you will (as Abdullah answered) have to create your own registration system where the user enters their name - and you then store it in the displayName property of Firebase Authentication.
To achieve what you requested, you either have to use a social auth provider (such as Google or Facebook) or change it yourself from the client, as the other answers suggest.
First of all, you would have to create a changeRequest, using the following code
let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
Once the change request is created, you can change whatever basic information you need to (either the photo URL or the display name) with the following code:
changeRequest?.displayName = "Lorem ipsum"
changeRequest?.photoURL = "https://your_link/path_to_image.png"
Finally, you must send the change request to Firebase, which will handle it and possibly return an error for you to handle.
changeRequest?.commitChanges { error in
if let error = error {
print(error.localizedDescription)
// You can handle the given error here
return
}
}
As others have already pointed out, you can find this and more information on the official on the official Firebase docs website.

Passing user id with AuthController

I just made simple authentication app using aqueduct as a back end. I used codes from aqueduct documentation pages for login and registering. When I login with this code in backend
router
.route('/auth/token')
.link(() => AuthController(authServer));
I get back token, token type and expiration date, Is there any chance to also pass userId? Or do I have to create my own controller to do that?
UPDATE
or how can I in my backend to save user id when saving the data
#Operation.post()
Future<Response> addData(#Bind.body(ignore: ['id']) Data newData) async {
final query = Query<Data>(context)..values = newData;
final insertData = await query.insert();
return Response.ok(insertData);
}
Flutter frontend
Login initially with the username/email and password. You will get an authorization token back from the server if the username and password are valid. Then use that token to make further privileged requests to the server.
You don't need to save any personal data about the user (email or password) on the client. You can save the token, though, if you don't want to make the user log in again the next time they use the app. When saving the token you should use a secure storage option. The flutter_secure_storage plugin uses KeyChain on iOS and KeyStore on Android.
Aqueduct backend
You can use the user IDs all you want on the backend. I don't know of any need to pass them to the client, though. On the backend you can query the user ID and then use it to fetch other information from the database.
Here is an example from the documentation:
class NewsFeedController extends ResourceController {
NewsFeedController(this.context);
ManagedContext context;
#Operation.get()
Future<Response> getNewsFeed() async {
var forUserID = request.authorization.ownerID;
var query = Query<Post>(context)
..where((p) => p.author).identifiedBy(forUserID);
return Response.ok(await query.fetch());
}
}
The client only passed in the token. Aqueduct looks up the user id for you based on that token. Now you know the user ID.
Your other tables can have a column for the user ID so that only that user may save and retrieve their data. In the example above, Posts have an Author and an Author has an ID, that is, the user ID.
where((p) => p.author).identifiedBy(forUserID)
is equivalent to
where((p) => p.author.id).equalTo(forUserID)
You can read about this in the Advanced Queries section of the documentation.

Firebase Authentication Get User Profile

So on the Firebase Docs, there is this block of code:
let user = Auth.auth().currentUser
if let user = user {
// The user's ID, unique to the Firebase project.
// Do NOT use this value to authenticate with your backend server,
// if you have one. Use getTokenWithCompletion:completion: instead.
let uid = user.uid
let email = user.email
let photoURL = user.photoURL
// ...
}
I don't understand how to use this. I'm trying to use this when a user logs in. Can someone help explain this?
The currentUser call synchronously gets the cached current user, or null if there is none.
Then if the user is not null, you will have access to all of the variables within the user block, via optional unwrapping.
When you create a new user with email and password or using some federated identity providers, the email and user id value will be auto created.
You can use a uid to reference a user within database solutions like Firestore or Realtime database.

Checking if a user already signed up

I built a custom authentication system using FirebaseAuthentication tokens.
My signup / login flow should work like this:
User presses login button
My server generates the authentication token and sends it to the client
Check if the user already exists (in the 'Auth' table or in my database?)
If true: sign in using FIRAuth.auth()?.signIn(withCustomToken:...
If false: Show a form to to enter custom information (name, etc..)
sign using FIRAuth.auth()?.signIn(withCustomToken:...
save the custom information to my database
My question is: How can I find out if the user has already signed up?
Would a publicly accessible database with only uid's be the way to go?
This is fairly opinion based, but yes, I would use a standalone DB that stores each user's username who has signed up. Then all that is required is a quick web request through a PHP file querying for any rows returned with that username.
The firebase sign in method will feedback in asynchronous callback.
FIRAuth.auth()?.signInWithEmail(email, password: password, completion: { (user , error) in
if let error = error {
print(error.localizedDescription)
return
}
self.signedIn(user)
})
If you haven't sign up yet. The error will print out
There is no user record corresponding to this identifier. The user may have been deleted.

Parse - Unity3D : How to create a user after a Facebook Connect

'm using the Facebook SDK 6.0 for Unity3D.
After my user accept the connection, I want to save his ID, email etc ... on a Parse database, and have the possibility to get some info from this database for this player (for exemple : the list of unlock levels).
How can I do it ?
I know how create an object, but I want to know, after a connection on Facebook, how to save the user and some details on Parse, without using the Parse login (as on the official Parse tutorial).
I can't understand.
Thank you very very much in advance for your help.
Best regards,
AB
You can try saving this information in the local ParseUser Object. Just add custom fields with the necessary information. For more complex solutions you can create a custom parse object to save the information.
https://parse.com/docs/unity_guide#users
var user = new ParseUser()
{
Username = "my name",
Password = "my pass",
Email = "email#example.com"
};
// other fields can be set just like with ParseObject
user["IsUnlocked"] = true;
Task signUpTask = user.SignUpAsync();
and for Facebook there is a special signup method in the ParseFacebookUtils class
https://parse.com/docs/unity_guide#fbusers
Task<ParseUser> logInTask = ParseFacebookUtils.LogInAsync(userId, accessToken, tokenExpiration);