Skip to content

Sharing data

If you have data that you want to be provided as prop to your SPA (a common use-case is information about the authenticated user) you can use the sails.inertia.share method.

You can do this in api/hooks/custom/index.js.


Here is an example of sharing the logged in user data:

module.exports = function defineCustomHook(sails) {
  return {
     * Runs when this Sails app loads/lifts.
    initialize: async function () {'Initializing custom hook (`custom`)')
    routes: {
      before: {
        'GET /': {
          skipAssets: true,
          fn: async function (req, res, next) {
            if (req.session.userId) {
              const loggedInUser = await User.findOne({
                id: req.session.userId
              if (!loggedInUser) {
                delete req.session.userId
                return res.redirect('/login')
              sails.inertia.share('loggedInUser', loggedInUser) 
              return next()
            return next()


Learn more about sharing data on the Inertia docs.

All open source projects are released under the MIT License.