User Management
At the core of many apps, there is a notion of user account management that lets users access their information in a secure manner. A specialized user class called ParseUser is available to automatically handles these kind of functionalities.
ParseUser is a subclass of the ParseObject, and has all the same features. All the methods that are on ParseObject also exist in ParseUser. The difference is that ParseUser has some special additions specific to user accounts.

Signing Up

Java
Swift
1
import com.parse.ParseException;
2
import com.parse.ParseUser;
3
import com.parse.SignUpCallback;
4
5
public void signUpUser(String username, String password, String email)
6
{
7
ParseUser user = new ParseUser();
8
9
user.setUsername(username);
10
user.setPassword(password);
11
user.setEmail(email);
12
13
// other fields can be set just like with ParseObject
14
user.put("phone", "650-253-0000");
15
16
user.signUpInBackground(new SignUpCallback()
17
{
18
public void done(ParseException e)
19
{
20
if (e == null)
21
{
22
// Hooray! Let them use the app now.
23
}
24
else
25
{
26
// Sign up didn't succeed. Look at the ParseException
27
// to figure out what went wrong
28
}
29
}
30
});
31
32
}
Copied!
1
import Parse
2
3
class testVC: UIViewController
4
{
5
func SignUpUser(email:String, pwd:String)
6
{
7
let user = PFUser.init()
8
user.username = "[email protected]"
9
user.email = "[email protected]"
10
user.password = "demo"
11
12
user.signUpInBackground
13
{
14
(isConnect, error) in
15
16
if let error = error
17
{
18
print (error.localizedDescription)
19
}
20
else
21
{
22
if isConnect
23
{
24
print ("user connect")
25
}
26
}
27
}
28
}
29
}
30
Copied!

Logging In

Java
Swift
1
import com.parse.LogInCallback;
2
import com.parse.ParseException;
3
import com.parse.ParseUser;
4
5
public void logInUser(String username, String password)
6
{
7
ParseUser.logInInBackground(username, password, new LogInCallback()
8
{
9
public void done(ParseUser user, ParseException e)
10
{
11
if (user != null)
12
{
13
// Hooray! The user is logged in.
14
}
15
else
16
{
17
// Signup failed. Look at the ParseException to see what happened.
18
}
19
}
20
});
21
}
Copied!
1
import Parse
2
3
class testVC: UIViewController
4
{
5
6
func connectUser(email: String, password: String, completion:@escaping (_ isSuccess: Bool, _ error: String) -> Void)
7
{
8
PFUser.logInWithUsername (inBackground:email, password: password, block:
9
{
10
(User, error) -> Void in
11
12
if ((error) != nil)
13
{
14
completion(false, error!.localizedDescription)
15
}
16
else
17
{
18
if let user = User
19
{
20
print ("user = ", user.description)
21
completion(true, "User connected")
22
}
23
else
24
{
25
completion(false, "User is empty")
26
}
27
}
28
})
29
}
30
31
}
Copied!

Get current User

Java
Swift
1
import com.parse.ParseUser;
2
3
public void getUser()
4
{
5
ParseUser currentUser = ParseUser.getCurrentUser();
6
7
if (currentUser != null)
8
{
9
// do stuff with the user
10
}
11
else
12
{
13
// show the signup or login screen
14
}
15
}
Copied!
1
import Parse
2
3
class testVC: UIViewController
4
{
5
func getUser ()
6
{
7
let currentUser = PFUser.currentUser
8
9
if currentUser != nil
10
{
11
if currentUser!.isNew
12
{
13
print ("is new user")
14
}
15
}
16
}
17
}
18
19
20
Copied!