laravel-json-api / laravel

JSON:API for Laravel applications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can you bcrypt password when adding new users?

genyded opened this issue · comments

Did we miss something in the docs? We cannot seem to find where/how to bcrpyt passwords when adding new users? Also is there a repo anywhere of a real working app using JSON API anywhere? The docs cover a lot of basics, but not integrations.

You can manipulate the value that will be saved to the DB using deserializeUsing function - https://laraveljsonapi.io/docs/2.0/schemas/attributes.html#attribute-hydration

Unfortunately, all the apps I've worked on are in private repos. What are you looking for regards 'integrations'?

Just something that shows what goes where and why. There are a LOT of 'parts' compared to say an app using Laravel resources. The JSON API docs show what's avaiable, but really how/why/when to use what or how the all relate to each other. BTW thanks forthe hydration info - that helps.

All the apps I've got in production with this are also private unfortunately.

There is a tutorial app here:
https://github.com/laravel-json-api/tutorial-app

Did you have any specific questions about using it?

Thanks for the rapid and helpful responses! We went through the tutorial, but it still not real clear what best practices are or even how to do some things. Such as:

  1. If we want a 'register' endpoint to create the new user via JSONAPI, send the verification email, then log them in and return the user along with the JWT token. From what we, we'd need to call the create JSONAPI endpoint from within the auth controller (internal JSONAPI call), then 'tweak' the response JSON before sending it back.
  2. Contrast that to say an Admin just adding new users which can easily be done via the typical JSONAPI create endpoint for the user type.

I think we may be missing a piece somewhere and once it 'clicks' we'll be fine. We just can't seem to find what or where it is. I guess in essence how can we 'customize' or 'include' a JSONAPI response from a non-standard endpoint (like /register)? The only way we have found so far is an internal call to the standard JSONAPI endpoint to get the resource in JSONAPI format and the tweaking it afterwards .

Calling another controller endpoint via HTTP from within the same app is massively overcomplicating things and potentially inefficient.

The simplest way would be something like:

return DataResponse::make($user)
    ->withMeta(['token' => $token]);

See https://laraveljsonapi.io/docs/3.0/responses/#data-response

That whole chapter contains information on how you return JSON:API responses from a non-standard endpoint.

I'm going to close this now, as it's a question rather than an actual issue. Feel free to ask any follow up questions, or maybe checkout the Slack community that's linked from https://laraveljsonapi.io/

If we try the suggestion of https://laraveljsonapi.io/docs/3.0/responses/#data-response, first of all the classes appear to be in LaravelJsonApi\Core\Responses but the docs say LaravelJsonApi\Core\Response for them. Second any time we try to use one we get:
"No bound JSON:API server - your application may not be handling an HTTP request."

@genyded can you create an issue in the docs repo about that namespace typo, so I remember to get it fixed?

The server thing is covered in the docs, here:
https://laraveljsonapi.io/docs/3.0/responses/#response-server