• Stars
    star
    187
  • Rank 206,464 (Top 5 %)
  • Language
    JavaScript
  • License
    Other
  • Created almost 11 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Modifies Meteor's Session to store variables in the browser's `localStorage`

This project is UNSUPPORTED!

We are no longer supporting the development of this package. Use at your own risk!

Purpose

Make Meteor's Session object persist its values locally and across page refreshes. Meteor's default implementation loses values whenever the page is refreshed.

Uses amplifyjs's store library to save values in the browsers localStorage, falling back to other solutions if it's not available.

Upgrading from 0.2.x to 0.3.x

The default behaviour of Session.set has been changed. default_method now defaults to temporary (as was mentioned in the docs), rather than persistent, which was what it was set to in the code.

This means that to keep the behaviour the same, you should set default_method to persistent:

config/settings.json file:

{
  "public": {
    "persistent_session": {
      "default_method": "persistent"
    }
  }
}

Installation

meteor add u2622:persistent-session

Note: To use persistent-session, your project must have Session already installed. You can add Meteor's Session package by meteor add session.

That's it! Now you can use Session.setPersistent to set a session variable that will save after a refresh.

If you'd like, you can have Session.set do this as well. See the Options section below.

For Meteor 1.3+

If your app is using the imports syntax, the persistent-session package will work by simply importing Session where it is used. e.g - import { Session } from 'meteor/session'

Types

  1. Temporary Session Variable
  • matches current Meteor implementation
  • are not available after a page reload
  1. Persistent Session Variable
  • content is stored in the localstorage until it is cleared
  1. Authenticated Session Variable
  • content is stored in the localstorage AND is cleared when a user logs out

Usage

Setting Session Values

  • Session.set(key, value)
    • stores a session var according to the default_method (see Options)
  • Session.setTemp(key, value)
    • stores a temporary session variable (non-persistent)
  • Session.setPersistent(key, value)
    • store a persistent session variable (persistent)
  • Session.setAuth(key, value)
    • stores a authenticated session variable (persistent + automatic deletion)

As of 3.3, you can use an object to set multiple values at once:

Session.setPersistent({foo: "foo", bar: "bar"});

This works with all of the set* methods. All key/values set as an object will have the same type of scoping (persistent/auth/temporary).

Updating Session Values

You can update the value of an existing session variable without changing or knowing its type. Note: If you call update on an non-existent variable, it will be created as a temporary variable.

  • Session.update(key, value)

Set Default

All of the set() functions have a setDefault() counterpart where the session variable will only be created if one doesn't already exist. Note: None of the setDefault() commands will change the type of an existing session variable.

  • Session.setDefault(key, value)
  • Session.setDefaultTemp(key, value)
  • Session.setDefaultPersistent(key, value)
  • Session.setDefaultAuth(key, value)

Change Types

Use these commands to change a session variable into a particular type.

  • Session.makeTemp(key)
  • Session.makePersistent(key)
  • Session.makeAuth(key)

Clear Values

  • Session.clear()
    • destroys all session variables of all types
  • Session.clear(key)
    • destroys a single session variable
  • Session.clearTemp()
    • destroys all temporary session variables
  • Session.clearPersistent()
    • destroys all persistent session variables
  • Session.clearAuth()
    • destroys all authenticated session variables

Other

These work the same as the current Meteor implementation:

  • Session.get(key)
  • Session.equals(key, value)

ReactiveDict's all method is also supported.

Options

To define the default type for session variables, set persistent_session.default_method to your preferred type in your config/settings.json file:

{
  "public": {
    "persistent_session": {
      "default_method": "your-preferred-type"
    }
  }
}

persistent_session.default_method can take one of the following values:

  • persistent
  • authenticated

In any other case the default_method will fall back to temporary

See EventedMind's screen on organizing environment variables and settings for more information.

TODO

  • Perhaps support saving session data in the URL ('setRoute' or something similar)

Contributing

Issues and Pull Requests are always welcome. Please read our contribution guidelines

License

The MIT License (MIT)

Copyright (c)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

analytics

UNMAINTAINED! - Complete Google Analytics, Mixpanel, KISSmetrics (and more) integration for Meteor
JavaScript
213
star
2

rest2ddp

DEPRECATED - this package is no longer supported
JavaScript
104
star
3

meteor-image-upload

DEPRECATED - this package is no longer supported
JavaScript
40
star
4

router-autoscroll

[DEPRECATED] Smart management of scroll position across route changes for iron and flow router
JavaScript
39
star
5

graphql-markdown

UNMAINTAINED! - Write markdown/frontmatter, generate GraphQL TypesDefs & Resolvers, query via GraphQL, and serve as html.🔥
JavaScript
39
star
6

iron-router-autoscroll

Deprecated - see okgrow/router-autoscroll instead.
JavaScript
38
star
7

auto-analytics

UNMAINTAINED! - Complete Google Analytics, Mixpanel, KISSmetrics (and more) integration for JavaScript applications.
JavaScript
28
star
8

meteor-react-demo

Source for the demo here: https://youtu.be/xcej5OboUVM
CSS
26
star
9

meteor-promise

DEPRECATED: Use deanius:promise instead
JavaScript
26
star
10

accounts-ui-react

UNMAINTAINED! - The Meteor accounts-ui we know and love, wrapped in React.
JavaScript
26
star
11

meteor-dotenv

DEPRECATED: - Load environment variables from .env for Meteor projects
JavaScript
23
star
12

railstoronto.com

Rails Toronto
CSS
22
star
13

meteor-text-search-demo

A demo app showing how to do full text search in Meteor 1.2
JavaScript
22
star
14

react-apollo-helpers

Experiments to simplify the DX of react-apollo
JavaScript
18
star
15

guides

How we do things at OK GROW!
JavaScript
16
star
16

graphql-fundamentals

A training repo for learning basic concepts in GraphQL on the client and server
JavaScript
15
star
17

advanced-graphql

A training repo for learning advanced concepts in GraphQL on the client and server
JavaScript
13
star
18

code-club

JavaScript
8
star
19

leaderboard-es6

Meteor's Leaderboard example ported to ES6, with notes
CSS
8
star
20

meteor-migrations

A zero-downtime database migrations package for Meteor
JavaScript
6
star
21

meteor-test-doubles

Meteor core API test doubles using testdouble.js
JavaScript
6
star
22

meteortoronto.com

A simple guide to the Meteor JS community in Toronto
CSS
5
star
23

rest2ddp-app

Publish a live updating DDP API for any existing REST API with no coding required
JavaScript
3
star
24

analytics-test-basic

Test app for okgrow:analytics package issue debugging
JavaScript
2
star
25

meteor-promise-docs

Deprecated. Use Deanius's repo instead
HTML
2
star
26

meteor-package-linter

Deprecated. Use Deanius' package instead
JavaScript
2
star
27

accounts-ui-react-example

Example for accounts-ui-react package
JavaScript
1
star
28

persistent-session-demo

A demo app for the meteor-persistent-session package
JavaScript
1
star