Start a new topic

Can a logged in user create a new user?

Hi



I have a scenario where the app already has an active user logged in. Now this user needs to create a new user in the User collection without itself getting logged off from the app. Is this possible? From the documentation I see that if a new user is created it automatically becomes the active user which is not what I want. I want the active user to be able to create new User accounts in the User collection. One way I can see it possible is using a custom endpoint but I would really prefer to use the User object from the SDK itself if possible.



Thanks

Pankaj

Good evening Pankaj:



At this time we do not offer library support for user creation, so using a custom BL endpoint as you mentioned above is the best option.



Thanks,
Followup question:

Is the User object accessible via BL or I need to use the modules.collectionAccess.collection('user') to get a 'raw' handle to the User collection and use insert/save/delete to actually add/delete the users? Is this a safe way of adding users?



Thanks

Pankaj
Pankaj,

We do not recommend using BL to create users directly. We recommend using the REST API to create / delete users while remaining within the same user context.



We have added a feature request to the Android library to create a user without changing user context.



Thanks,
Ok, so you mean to say I should directly use the Rest API from my android device to create the new user instead of doing a custom endpoint? Just trying to ensure I understood you correctly.
For the time being, we're recommending that you do it manually as doing so within the app isn't something that we've done before. We are working on implementing this soon, as it's something we could see value for in the future.
Any update on when this can be made available in the SDK?
So I created a custom endpoint called createUserAccount as below:



`function onRequest(request, response, modules) {

var req = modules.request;

var uriString = 'https://baas.kinvey.com/user/' + modules.backendContext.getAppKey();

var encoded = modules.utils.base64.encode(modules.backendContext.getAppKey()+ ":" + modules.backendContext.getAppSecret());

var opts = {

uri: uriString,

method: 'POST',

headers: {Authorization:"Basic " + encoded},

json:true,

body: {"username":request.body.username, "password":request.body.password}

};

modules.request.request(opts, function(error, res, body) {

if (error)

modules.logger.info("Error: " + error);

modules.logger.info("Res: " + JSON.stringify(res));

modules.logger.info("Body: " + JSON.stringify(body));

response.body = body;

response.complete(res.status);

});

}`



While this was working a while back, its now failing with ETIMEDOUT error for the POST request. Is there something that I can do to fix this situation.
Login or Signup to post a comment