Create an user session (login)


#1

Hi,

I have a Custom Scriping Service and im trying to create an user session, the script is:

var my_payload = {"email":"my@email.com",
"password":"mypassword",
"duration":"0"
};
var result = platform.api.post("user/session", my_payload);
return result;

But i receive error 500 and the log shows:

exception ‘Symfony\Component\Debug\Exception\FatalErrorException’ with message ‘Call to a member function query() on a non-object’ in …dreamfactory-2.0.2-0/apps/dreamfactory/htdocs/app/Http/Middleware/AuthCheck.php:29
Stack trace:
#0 {main}

I have tried JSON.stringify(my_payload), the exception does not appear, but i received error 400, “Login request is missing required email.”

If i call /user/session in Live API Documentation or postman it works fine.

Thanks!


Platform.api.post("user/session",{}) is broken after updating from DF 2.0.1 -> DF 2.0.4
How to authenticate with v8js script?
#2

I tried to reproduce the error and the same occurs here. In my opinion your code is correct and I couldn’t find a way to make it work.

But here are my two cents: are you sure you want to authenticate in the script itself? Isn’t better if the user authenticate before calling your script service? This should be the regular flow of the request, unless you want to give guest access to some resources. In this case you can enable guest access to all of those resources.

My point is: I think you shouldn’t try to authenticate using this script service, but the request itself should be already authenticated.


#3

@Elvis_Fernandes thanks for answering.

I want to provide some mockdata to a working APP, so the response must match.

I wouldn’t need to use authentication in the script, if I can change the endpoint path (/user/session/ to /my_service/something/), and also I need be able to change the response and/or add some custom data.

Thank you.


#4

Sure you can change the response from any request! Just use the Scripts option to do that. This is an example you can see by clicking the “View Samples” button under Scripts tab:

//****************** Post-processing script on a table ******************

//  A script that is triggered by a GET on /api/v2/db/_table/<tablename>. Runs after the db call is made.
//  The script adds a new field to each record in the response.

var_dump(event.response); // outputs to file in storage/log of dreamfactory install directory

var lodash = require("lodash.min.js");

if (event.response.resource) {

    lodash._.each (event.response.resource, function( record ) {

        record.extraField = 'Feed the dog.';
    });
}

I’m not sure if I understand what you mean by “change the endpoint path” (do you want to change the endpoint for login?), but just make sure you follow the appropriate procedures to accessing a resource:

  1. Send login information to /user/session and grab the session token from its response
  2. Send a request to any endpoint using this session token
  3. Change the response from any endpoint at your will using Scripts (not Custom Scripting Service)

Hope this helps.


#5

Thanks, Im aware about pre/post-processing scripts.

Yes, i want to change (or create a “slang”) for the url /user/session and send login information to this new address.