Skip to content

Commit

Permalink
Add koa session examples
Browse files Browse the repository at this point in the history
  • Loading branch information
simov committed Apr 1, 2015
1 parent 8c8de52 commit 85beb58
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 0 deletions.
38 changes: 38 additions & 0 deletions example/koa-session/README.md
@@ -0,0 +1,38 @@

# Koa Session Example


## Install

```bash
$ cd examples/koa-session
$ npm install
```

## OAuth Application

Create OAuth application for Facebook and set the application domain to be `dummy.com`

In your `hosts` file add this line `dummy.com 127.0.0.1`


## Configure

Edit the `config.json` file with your own OAuth application credentials


## Run the App

You need at least NodeJS version 11 or higher!

```bash
$ node --harmony koa-session.js
$ node --harmony koa-generic-session-redis.js
$ node --harmony koa-generic-session-mongo.js
$ node --harmony koa-session-store.js
$ node --harmony koa-session-store-mongo.js
```

## Start the Flow

To start the OAuth flow for Facebook navigate to `http://dummy.com:3000/connect/facebook` in your browser
15 changes: 15 additions & 0 deletions example/koa-session/config.json
@@ -0,0 +1,15 @@
{
"server": {
"protocol": "http",
"host": "dummy.com:3000"
},
"facebook": {
"key": "[APP_ID]",
"secret": "[APP_SECRET]",
"callback": "/handle_facebook_callback",
"scope": [
"user_groups",
"user_likes"
]
}
}
28 changes: 28 additions & 0 deletions example/koa-session/koa-generic-session-mongo.js
@@ -0,0 +1,28 @@

// https://github.com/koajs/generic-session
// https://github.com/freakycue/koa-generic-session-mongo

var koa = require('koa')
, route = require('koa-route')
, mount = require('koa-mount')
, session = require('koa-generic-session')
, MongoStore = require('koa-generic-session-mongo')

var Grant = require('grant-koa')
, grant = new Grant(require('./config.json'))

var app = koa()
app.keys = ['whatever']
app.use(session({
store: new MongoStore()
}))
app.use(mount(grant))

app.use(route.get('/handle_facebook_callback', function* (next) {
console.log(this.query)
this.body = JSON.stringify(this.query, null, 2)
}))

app.listen(3000, function() {
console.log('Koa server listening on port ' + 3000)
})
28 changes: 28 additions & 0 deletions example/koa-session/koa-generic-session-redis.js
@@ -0,0 +1,28 @@

// https://github.com/koajs/generic-session
// https://github.com/koajs/koa-redis

var koa = require('koa')
, route = require('koa-route')
, mount = require('koa-mount')
, session = require('koa-generic-session')
, redisStore = require('koa-redis')

var Grant = require('grant-koa')
, grant = new Grant(require('./config.json'))

var app = koa()
app.keys = ['whatever']
app.use(session({
store: redisStore()
}))
app.use(mount(grant))

app.use(route.get('/handle_facebook_callback', function* (next) {
console.log(this.query)
this.body = JSON.stringify(this.query, null, 2)
}))

app.listen(3000, function() {
console.log('Koa server listening on port ' + 3000)
})
30 changes: 30 additions & 0 deletions example/koa-session/koa-session-store-mongo.js
@@ -0,0 +1,30 @@

// https://github.com/hiddentao/koa-session-store
// https://github.com/hiddentao/koa-session-mongo

var koa = require('koa')
, route = require('koa-route')
, mount = require('koa-mount')
, session = require('koa-session-store')
, mongoStore = require('koa-session-mongo')

var Grant = require('grant-koa')
, grant = new Grant(require('./config.json'))

var app = koa()
app.keys = ['whatever']
app.use(session({
store: mongoStore.create({
db: 'test'
})
}))
app.use(mount(grant))

app.use(route.get('/handle_facebook_callback', function* (next) {
console.log(this.query)
this.body = JSON.stringify(this.query, null, 2)
}))

app.listen(3000, function() {
console.log('Koa server listening on port ' + 3000)
})
24 changes: 24 additions & 0 deletions example/koa-session/koa-session-store.js
@@ -0,0 +1,24 @@

// https://github.com/hiddentao/koa-session-store

var koa = require('koa')
, route = require('koa-route')
, mount = require('koa-mount')
, session = require('koa-session-store')

var Grant = require('grant-koa')
, grant = new Grant(require('./config.json'))

var app = koa()
app.keys = ['whatever']
app.use(session())
app.use(mount(grant))

app.use(route.get('/handle_facebook_callback', function* (next) {
console.log(this.query)
this.body = JSON.stringify(this.query, null, 2)
}))

app.listen(3000, function() {
console.log('Koa server listening on port ' + 3000)
})
24 changes: 24 additions & 0 deletions example/koa-session/koa-session.js
@@ -0,0 +1,24 @@

// https://github.com/koajs/session

var koa = require('koa')
, route = require('koa-route')
, mount = require('koa-mount')
, session = require('koa-session')

var Grant = require('grant-koa')
, grant = new Grant(require('./config.json'))

var app = koa()
app.keys = ['whatever']
app.use(session(app))
app.use(mount(grant))

app.use(route.get('/handle_facebook_callback', function* (next) {
console.log(this.query)
this.body = JSON.stringify(this.query, null, 2)
}))

app.listen(3000, function() {
console.log('Koa server listening on port ' + 3000)
})
37 changes: 37 additions & 0 deletions example/koa-session/package.json
@@ -0,0 +1,37 @@
{
"name": "grant-koa-session-example",
"version": "0.0.0",
"description": "Koa Session Example",
"private": true,

"keywords": [],

"license": "MIT",
"homepage": "https://github.com/simov/grant",

"author": "Simeon Velichkov <simeonvelichkov@gmail.com> (http://simov.github.io)",

"repository": {
"type": "git",
"url": "git://github.com/simov/grant.git"
},

"dependencies": {
"koa" : "0.14.0",
"koa-route" : "2.4.0",
"koa-mount" : "1.3.0",

"koa-session" : "3.0.0",

"koa-generic-session" : "1.8.0",
"koa-generic-session-mongo" : "0.1.1",
"koa-redis" : "1.0.0",

"koa-session-store" : "1.1.1",
"koa-session-mongo" : "1.0.1",

"grant-koa" : "*"
},

"main": "koa-session.js"
}

0 comments on commit 85beb58

Please sign in to comment.