diff --git a/CHANGELOG.md b/CHANGELOG.md index 69250ac..7327a2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,18 @@ + +## [1.0.6](https://github.com/adonisjs/adonis-session/compare/v1.0.5...v1.0.6) (2017-08-01) + + +### Features + +* **instructions:** add instructions file for ace ([959d3ce](https://github.com/adonisjs/adonis-session/commit/959d3ce)) + + +### Reverts + +* **command:** remove config:session command ([3387118](https://github.com/adonisjs/adonis-session/commit/3387118)) + + + ## [1.0.5](https://github.com/adonisjs/adonis-session/compare/v1.0.4...v1.0.5) (2017-07-31) diff --git a/commands/ConfigSession.js b/commands/ConfigSession.js deleted file mode 100644 index 1da9805..0000000 --- a/commands/ConfigSession.js +++ /dev/null @@ -1,89 +0,0 @@ -'use strict' - -/* - * adonis-session - * - * (c) Harminder Virk - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. -*/ - -const path = require('path') -const { Command } = require('@adonisjs/ace') - -class ConfigSession extends Command { - constructor (Helpers) { - super() - this.Helpers = Helpers - } - /** - * The command signature - * - * @method signature - * - * @return {String} - */ - static get signature () { - return ` - config:session - { -e, --echo: Echo session file contents } - ` - } - - /** - * The command description - * - * @method description - * - * @return {String} - */ - static get description () { - return 'Save session config to the config file' - } - - /** - * IoC container injections - * - * @method inject - * - * @return {Array} - */ - static get inject () { - return ['Adonis/Src/Helpers'] - } - - /** - * Handle method called by ace when command is executed - * - * @method handle - * - * @param {Object} args - * @param {Boolean} options.echo - * - * @return {void} - */ - async handle (args, { echo }) { - const template = await this.readFile(path.join(__dirname, './templates/config.mustache'), 'utf-8') - - /** - * Echo template over creating the config file - */ - if (echo) { - return this.viaAce ? console.log(template) : 'echoed' - } - - /** - * Create config file - */ - const configPath = `${path.join(this.Helpers.configPath(), 'session.js')}` - await this.generateFile(configPath, template, {}) - - if (!this.viaAce) { - return configPath - } - this.completed('created', configPath.replace(this.Helpers.appRoot(), '').replace(path.sep, '')) - } -} - -module.exports = ConfigSession diff --git a/instructions.js b/instructions.js new file mode 100644 index 0000000..20bf3e4 --- /dev/null +++ b/instructions.js @@ -0,0 +1,20 @@ +'use strict' + +/* + * adonis-session + * + * (c) Harminder Virk + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. +*/ + +const path = require('path') + +module.exports = async function (cli) { + try { + await cli.makeConfig('session.js', path.join(__dirname, './templates/config.mustache')) + } catch (error) { + // ignore if session.js already exists + } +} diff --git a/instructions.md b/instructions.md new file mode 100644 index 0000000..b2081e0 --- /dev/null +++ b/instructions.md @@ -0,0 +1,34 @@ +## Registering provider + +Make sure to register the provider before you can make use of sessions. The providers are registered inside `start/app.js` file. + +```js +const providers = [ + '@adonisjs/session/providers/SessionProvider' +] +``` + +## Registering middleware + +The next thing you should do is register the global middleware inside `start/kernel.js` file. + +```js +const globalMiddleware = [ + 'Adonis/Middleware/Session' +] +``` + +## Using session + +Once done with provider and middleware registeration, you can make use of the session by grabbing an instance from the HTTP request context. + +```js +Route.get('/', async ({ session }) => { + session.get('username') + session.put('username', 'virk') +}) +``` + +## Config + +You can find the configuration inside `config/session.js` file. Feel free to tweak it as per your needs. diff --git a/package.json b/package.json index cf45b7c..a41038d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@adonisjs/session", - "version": "1.0.5", + "version": "1.0.6", "description": "This repo is the official session provider for Adonisjs apps. It supports multiple drivers to store session data.", "main": "providers/SessionProvider", "directories": { diff --git a/commands/templates/config.mustache b/templates/config.mustache similarity index 100% rename from commands/templates/config.mustache rename to templates/config.mustache diff --git a/test/config-command.spec.js b/test/config-command.spec.js deleted file mode 100644 index c39ca54..0000000 --- a/test/config-command.spec.js +++ /dev/null @@ -1,69 +0,0 @@ -'use strict' - -/* - * adonis-lucid - * - * (c) Harminder Virk - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. -*/ - -const test = require('japa') -const ace = require('@adonisjs/ace') -const fs = require('fs-extra') -const path = require('path') -const { ioc } = require('@adonisjs/fold') -const { setupResolver, Helpers, Env } = require('@adonisjs/sink') -const ConfigSession = require('../commands/ConfigSession') - -test.group('Make Config', (group) => { - group.before(async () => { - ioc.bind('Adonis/Src/Helpers', () => { - return new Helpers(path.join(__dirname)) - }) - - ioc.bind('Env', () => { - return new Env() - }) - setupResolver() - }) - - group.after(async () => { - try { - await fs.remove(path.join(__dirname, 'config')) - } catch (error) { - if (process.platform !== 'win32' || error.code !== 'EBUSY') { - throw error - } - } - }).timeout(0) - - test('create config file', async (assert) => { - ace.addCommand(ConfigSession) - const result = await ace.call('config:session') - const exists = await fs.pathExists(result) - const config = require(result) - assert.deepEqual(config, { - age: '2h', - clearWithBrowser: true, - cookie: { - httpOnly: true, - sameSite: true - }, - cookieName: 'adonis-session', - driver: 'cookie', - file: { - location: 'sessions' - }, - redis: 'self::redis.default' - }) - assert.isTrue(exists) - }) - - test('echo config file to console', async (assert) => { - ace.addCommand(ConfigSession) - const result = await ace.call('config:session', {}, { echo: true }) - assert.equal(result, 'echoed') - }) -})