Logout User FAULT = '3023' [Unable to logout user due to error: user must be logged in.]

func logoutUserAsync() {

backendless.userService.login(
"james.bond@mi6.co.uk", password:“iAmWatchingU”,
response: { ( user : BackendlessUser!) -> () in
print(“User has been logged in (ASYNC): (user)”)

self.backendless.userService.logout(
{ ( user : AnyObject!) -> () in
print(“Current user session expired.”)
},
error: { ( fault : Fault!) -> () in
print(“Server reported an error: (fault)”)
}
)
},
error: { ( fault : Fault!) -> () in
print(“Server reported an error: (fault)”)
}
)
}
I dont understand why does it need backendless.userservice.login again?

In this code you perform login and as soon as you get a response you do logout.

I see that you copied the code from the doc. In there we did it that way to show the logout function in the context. User has to be logged in, in order to log him out.

Mark

If i login a user then if i need to logout do i need to put the email and password agn?

Does the logout signature ask for email and password?

func logoutUserAsync() {

self.backendless.userService.logout(
{ ( user : AnyObject!) -> () in
print(“Current user session expired.”)
},
error: { ( fault : Fault!) -> () in
print(“Server reported an error: (fault)”)
}
)
},
error: { ( fault : Fault!) -> () in
print(“Server reported an error: (fault)”)
}
)
}

So Can i write my code like this to logout ?

Yes

But after login and i press logout it say Logout User FAULT = ‘3023’ [Unable to logout user due to error: user must be logged in.]

Do you use facebook login or classic backendless login?

Classic login

This error would happen only if user is not logged in. Do you check for errors in the code that does login? Perhaps using a try/catch block could help?

Is this correct ?

func loginUserAsync(email:String , password:String) {

    backendless.userService.login(
        "\(email)", password:"\(password)",
        response: { ( user : BackendlessUser!) -> () in
            
            let backendless = Backendless.sharedInstance()
            backendless.userService.setStayLoggedIn( true )
       


            print("User has been logged in (ASYNC): \(user)")
        },
        error: { ( fault : Fault!) -> () in
    
            print("Server reported an error: \(fault)")
        }
    )


























}

No, if you want to persist logins, this needs to be called before the login method:

backendless.userService.setStayLoggedIn( true )

func loginUserAsync(email:String , password:String) {

let backendless = Backendless.sharedInstance()
backendless.userService.setStayLoggedIn( true )

backendless.userService.login(
“(email)”, password:"(password)",
response: { ( user : BackendlessUser!) -> () in

print(“User has been logged in (ASYNC): (user)”)
},
error: { ( fault : Fault!) -> () in

print(“Server reported an error: (fault)”)
}
)

}

I try this but not working

Check the log and see if the order of messages about user logged in/logged out is the one you expect.

User has been logged in (ASYNC): <BackendlessUser> {

"__meta" = "{\"relationRemovalIds\":{},\"selectedProperties\":[\"fb_last_name\",\"gender\",\"displayName\",\"created\",\"fb_first_name\",\"facebookID\",\"last_name\",\"ownerId\",\"__updated__meta\",\"password\",\"name\",\"___class\",\"updated\",\"first_name\",\"objectId\",\"email\"],\"relatedObjects\":{}}";

created = "2016-02-15 00:45:13 +0000";

displayName = "&lt;null&gt;";

email = "123123123@live.com";

facebookID = "&lt;null&gt;";

"fb_first_name" = "&lt;null&gt;";

"fb_last_name" = "&lt;null&gt;";

"first_name" = "&lt;null&gt;";

gender = "&lt;null&gt;";

lastLogin = "2016-02-15 02:46:46 +0000";

"last_name" = "&lt;null&gt;";

name = 123123;

objectId = "722069B7-FAC4-FDB8-FFF4-3C077A27AC00";

ownerId = "&lt;null&gt;";

updated = "&lt;null&gt;";

"user-token" = "ABE98FC1-38C9-79B2-FF40-9ED03A75B000";

}
Ya User is logged in but when i press log out the error occur

I just tried this and it works exactly as expected:

Types.tryblock({ () -> Void in
   backendless.userService.login("mark@backendless.com", password: "password" );
   backendless.userService.logout()
   print("user logged out" );
},
catchblock: { (exception) -> Void in
   print("Server reported an error: \(exception as! Fault)")
})

Can you create a user in Backendless Console and try with the code I shared? (as a test)

Backendless Console ?

I try yours

Types.tryblock({ () -> Void in

backendless.userService.login("123123123@live.com", password: “123123123” );

backendless.userService.logout()

print(“user logged out” );

},

catchblock: { (exception) -> Void in

print(“Server reported an error: (exception as! Fault)”)

})

and it give me this error Server reported an error: FAULT = ‘3023’ [Unable to logout user due to error: user must be logged in.] <Unable to logout user due to error: user must be logged in.>

Do this first:

https://backendless.com/feature-64-registering-app-users-manually-using-backendless-console/

Then try my code with the user you create.

Mark