devise "flash messages" in json response in API-only rails apps

Created on 23 Feb 2018  路  4Comments  路  Source: heartcombo/devise

Hello there!

Is there a possibility to include devise's flash messages, into the devise json response, when using devise in an API-only rails application?

Searched the docs/issues and the web, without success.

For example when I make an http request to DELETE /users/sign_out, an empty response body gets returned.

Would be nice if there would be some confirmation in json format like "Signed out successfully." like it's returned in a normal rails app in the flash. Otherwise it's maybe not really clear for the consumer of the api if he really got logged out.

Additionally all the other messages in config/locales/devise.en.yml could be useful too, having them included in the json response.

With the advent of Rails API-only mode, and abundance of JS frontend frameworks, this would be a nice upgrade to devise to make it more suitable for API-only apps.

Thanks a lot!

Most helpful comment

Could you specify the reason why you don't think this should be an option in devise?
Is an API-only use not intended for devise in general?

All 4 comments

For API request the http response returns if something was successful or not. Flash messages only make sense for web pages.

Hey @rafaelfranca,

thanks for your answer!

ok leaving this bit of api consumer friendliness aside of having devise messages included in the json response.

Though this would be a nice side-effect by considering the following, which was actually my main intention about including devise messages in the json response:

What if you want to use the messages, devise gives you, in your frontend that gets served by your api? You would need to duplicate all of that message functionality, depending of the status code you get back.

Wouldn't this be the opposite of DRY, since this functionality is already built-in in devise?

Additionally the messages of devise are directly intended for the user of your app, so you will most likely want to include them in your frontend. Why not just include them in devise's json response by default then, like they are included in the flash in an html response by default ( no matter if you use the flash or not )?

This would extend an api app with the same functionality as a rendered HTML app ( regarding the message system of devise ).

Or am I missing smth?

Thanks!

If you need that behavior it is easy to override the default session controller to do so. But I don't think it should be default in devise.

Could you specify the reason why you don't think this should be an option in devise?
Is an API-only use not intended for devise in general?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BimaPanduW picture BimaPanduW  路  3Comments

spaquet picture spaquet  路  3Comments

ragesoss picture ragesoss  路  3Comments

Pedroknoll picture Pedroknoll  路  3Comments

Gorchel picture Gorchel  路  3Comments