Design and implementation of Android user login cookie Management

Source: Internet
Author: User

Functional Requirements

Basically every social APP has its own account system or allows users to log in to a third party. After the user login successfully, the APP should record the user login status, the user will not need to enter the password and account number when they log out.

Implementation design

Return registered user information when server-side login succeeds
The client persists the user information to the hard drive in a successful callback
Using a database to store user information, here I use a third-party library activeandroid
Encapsulates the Bean class to encrypt user-sensitive information
Check whether the user is logged on every time you log in, if so, skip the login interface directly
The application of maintaining a user login state in the application class is easy for the program to invoke dynamically
RESTful interface

The code is as follows Copy Code

Service side
POST [' email ', ' password '];
Normal registration to use this interface, the third party directly login can
$api->post (' register ', ' Usercontroller@createuser ');
POST [' email ', ' password ', ' Is_third ']
POST [' uid ', ' Is_third ']
$api->post (' login ', ' usercontroller@login ');
$api->get (' users ', ' usercontroller@getusers ');
Moving End
[' name ', ' email ', ' password ', ' is_third ', ' gender ', ' birthday '];
@FormUrlEncoded
@POST ("register")
Observable<user> CreateUser (@Field ("name") string name, @Field ("email") string email,
@Field ("password") string password, @Field ("Is_third") string Is_third,
@Field ("gender") string gender, @Field ("Birthday") string birthday);

local [' email ', ' password ', ' Is_third ']
@FormUrlEncoded
@POST ("Login")
Observable<user> Login (@Field ("email") string email, @Field ("password") string password,
@Field ("Is_third") String Is_third);

Third [' uid ', ' Is_third ']
@FormUrlEncoded
@POST ("Login")
Observable<user> Login (@Field ("UID") String uid, @Field ("Is_third") int is_third);
Accountbean User Class

@Table (name = account)
public class Accountbean extends Model {

Private final static int no_account = 0;

    @Expose
    @Column (name = "user_id")
    public int UserID ;
    @Column (name = "email")
    public String email;
    @ Column (name = "name")
    public String name;
    @Column (name = "Gender")
&nbs p;   public int gender;
    @Column (name = "Birthday")
    public String birthday
     @Column (name = "Is_third")
    public int isthird;

Public Accountbean (user user) {
This.userid = User.getid ();
This.email = User.getemail ();
This.birthday = User.getbirthday ();
This.gender = User.getgender ();
This.isthird = User.getisthird ();
}

public static void Cacheaccountinfo (user user) {
Accountbean Accountbean = new Accountbean (user);
Accountbean.save ();
}

public static void Logoutaccount () {
Long id = Getaccountid ();
if (ID!= no_account) {
Accountbean.delete (Accountbean.class, id);
}
}

public static Boolean Existaccountcache () {
list<accountbean> accounts = getallaccounts ();
Return (accounts.size () = = 1);
}

public static long Getaccountid () {
if (Existaccountcache ()) {
list<accountbean> accounts = getallaccounts ();
Return Accounts.get (0). GetId ();
}
return no_account;
}

public static list<accountbean> getallaccounts () {
Return to New Select (). from (Accountbean.class). Execute ();
}

}

Moving End application Subclass

The code is as follows Copy Code

public class Colortalkapp extends application {

public static Accountbean saccount = null;

public static int Getlogineduserid () {
if (Saccount = = null) {
return 0;
}
return Saccount.getuserid ();
}

}

Service-Side Userrepository

The code is as follows Copy Code

Public Function Loginintosystem ($payload) {
$isThird = $payload [' Is_third '];
if ($isThird = = user::third_true) {
$uid = $payload [' uid '];
$user = $this->thirdaccountregister ($uid);
return array (' result ' => true, ' content ' => $user);
} else{
$email = $payload [' email '];
$password = $payload [' Password '];
return $this->localaccountregister ($email, $password);
}
}

Private Function Localaccountregister ($email, $password) {
if (! User::isaccountregistered ($email)) {
Return Array ("result" => false, ' message ' => ' emails has not been registered! ');
}
if (! User::isaccountvalid ($email, $password)) {
Return Array ("result" => false, ' message ' => ' The email and password does not match! ');
}
$user = User::getlocaluserbyemail ($email);
return array (' result ' => true, ' content ' => $user);
}

Private Function Thirdaccountregister ($uid) {
if (! User::isthirdaccountregisted ($uid)) {
$user = User::create ([
' Name ' => user::third_new_user,
' UID ' => $uid,
' Is_third ' => user::third_true,
]);
return $user;
} else{
$user = User::getthirduserbyuid ($uid);
return $user;
}
}

Service-Side User class

The code is as follows Copy Code

public static function isaccountregistered ($email)
{
Return (user::where (' email ', $email)->first ()!= null);
}

public static function isthirdaccountregisted ($UID) {
Return (User::where (' uid ', $uid)->first ()!= null);
}

public static function Isaccountvalid ($email, $password)
{
$user = user::where (' email ', $email)->first ();
if ($user!= null) {
Return Hash::check ($password, $user->password);
}
return false;
}

public static function Isthirdaccountvalid ($UID)
{
Return user::isthirdaccountregisted ($UID);
}

public static function Getthirduserbyuid ($UID) {
return User::where (' uid ', $uid)->first ();
}

public static function Getlocaluserbyemail ($email) {
return user::where (' email ', $email)->first ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.