{"id":15020345,"url":"https://github.com/rootstrap/mobx-session","last_synced_at":"2025-10-25T17:30:36.405Z","repository":{"id":34945638,"uuid":"188856203","full_name":"rootstrap/mobx-session","owner":"rootstrap","description":"mobx react session managment using localforage","archived":false,"fork":false,"pushed_at":"2023-01-01T08:00:08.000Z","size":1870,"stargazers_count":29,"open_issues_count":58,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-09-30T23:48:40.644Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rootstrap.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-27T14:14:43.000Z","updated_at":"2020-11-14T20:23:28.000Z","dependencies_parsed_at":"2023-01-15T10:52:47.743Z","dependency_job_id":null,"html_url":"https://github.com/rootstrap/mobx-session","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootstrap%2Fmobx-session","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootstrap%2Fmobx-session/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootstrap%2Fmobx-session/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootstrap%2Fmobx-session/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rootstrap","download_url":"https://codeload.github.com/rootstrap/mobx-session/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219865317,"owners_count":16555929,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T19:54:56.792Z","updated_at":"2025-10-25T17:30:36.025Z","avatar_url":"https://github.com/rootstrap.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.com/rootstrap/mobx-session.svg?branch=master)](https://travis-ci.com/rootstrap/mobx-session)\n[![Maintainability](https://api.codeclimate.com/v1/badges/7779ab5005e559a3104a/maintainability)](https://codeclimate.com/github/rootstrap/mobx-session/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/7779ab5005e559a3104a/test_coverage)](https://codeclimate.com/github/rootstrap/mobx-session/test_coverage)\n\n# Mobx Session\n\nMobx Session helps you manage your session data providing an API to save and access your info whenever and wherever you want.\n\nThe stored data will be saved with [localforage](https://github.com/localForage/localForage).\n\n## Installation\nyarn:\n\n`yarn add mobx-session`\n\nnpm:\n\n`npm install mobx-session`\n\n## Usage\n\nIt's really straight forward to use.\n\nFirst you must initialize the Storage. For that, you should call\n\n```javascript\nimport SessionStore from 'mobx-session';\n\nSessionStore.initialize();\n```\n\n**This should be called before any other method call, so I would recommend putting it on the index.js or App of your site, or in the top of another Store that uses SessionStore (like in [this example](examples/example/src/stores/UserStore.js)).**\n\nThere are several config options to customize this method, go to the [API doc](https://github.com/rootstrap/mobx-session#initializeconfig-object-promise) to see more.\n\nFor example\n\n```javascript\nimport SessionStore from 'mobx-session';\n\nSessionStore.initialize({ name: 'my-app-name' });\n```\n\nThen, you can use it wherever you want.\n\n### Inside a Component\n\n```javascript\nimport SessionStore from 'mobx-session';\n\nconst MyComponent = observer(() =\u003e {\n  if (SessionStore.initialized) {\n    return (\n      \u003cdiv\u003e\n        {\n          SessionStore.hasSession\n          ? \u003cp\u003eHello!\u003c/p\u003e\n          : \u003cp\u003eLogin\u003c/p\u003e\n        }\n      \u003c/div\u003e\n    );\n  }\n\n  return null;\n})\n```\n\nYou can use the decorator syntax\n\n```javascript\nimport SessionStore from 'mobx-session';\n\n@observer\nconst MyComponent = () =\u003e {\n  if (SessionStore.initialized) {\n    return (\n      \u003cdiv\u003e\n        {\n          SessionStore.hasSession\n          ? \u003cp\u003eHello!\u003c/p\u003e\n          : \u003cp\u003eLogin\u003c/p\u003e\n        }\n      \u003c/div\u003e\n    );\n  }\n\n  return null;\n}\n```\n\n*TIP: don't forget to make your component an observer so you don't lose the reference.*\n\n### Inside a Store\n\n```javascript\nimport SessionStore from 'mobx-session';\n\nclass UserStore {\n  constructor() {\n    extendObservable(this, {\n      user: null,\n      get loggedIn() {\n        return this.user !== null \u0026\u0026 SessionStore.hasSession;\n      },\n    });\n  }\n  ......\n}\n```\n\nYou can also use the decorator syntax\n\n```javascript\nimport SessionStore from 'mobx-session';\n\nclass UserStore {\n    @observable user = null;\n    @computed get loggedIn() {\n        return this.user !== null \u0026\u0026 SessionStore.hasSession;\n    }\n    ......\n}\n```\n\n*TIP: inside a store, don't forget to use it inside a computed value or an auto-run so you don't loose the reference.*\n\n## Examples\n\n- [Basic example using React](examples/example)\n- [Example using React and React Router v4](examples/react-router-v4-example) that uses the session data to determine if a user can access a private route.\n\n## API\n\n### initialize(config: object): Promise\n\nInitialize an instance of the storage inside the session.\n\nOptions can be the ones listed in the [localforage library](https://github.com/localForage/localForage#configuration)\n\n### saveSession(session: object): Promise\n\nSaves the session object in the store and storage\n\n### deleteSession(): Promise\n\nDeletes the session object from the store and the storage\n\n### getSession(): Promise(session: object)\n\nReturns the session object saved if there's any\n\n### initialized: boolean\n\nReturns true if the session store has been initialized. Could be useful to check this property before relying on other methods or properties from the store.\n\n### hasSession: boolean\n\nReturns true if there's a session object saved and false if there's not.\n\n## Contributing\nBug reports (please use Issues) and pull requests are welcome on GitHub at https://github.com/rootstrap/mobx-session. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\nThe library is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Credits\n**mobx-session** is maintained by [Rootstrap](http://www.rootstrap.com) with the help of our [contributors](https://github.com/rootstrap/mobx-session/contributors).\n\n[\u003cimg src=\"https://s3-us-west-1.amazonaws.com/rootstrap.com/img/rs.png\" width=\"100\"/\u003e](http://www.rootstrap.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frootstrap%2Fmobx-session","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frootstrap%2Fmobx-session","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frootstrap%2Fmobx-session/lists"}