diff --git a/examples/auth-routes/package.json b/examples/auth-routes/package.json new file mode 100644 index 0000000000..2546c1b8aa --- /dev/null +++ b/examples/auth-routes/package.json @@ -0,0 +1,14 @@ +{ + "name": "auth-routes", + "description": "", + "dependencies": { + "body-parser": "^1.15.2", + "cookie-session": "^2.0.0-alpha.2", + "express": "^4.14.0", + "nuxt": "latest", + "whatwg-fetch": "^2.0.1" + }, + "scripts": { + "start": "node server.js" + } +} diff --git a/examples/auth-routes/pages/index.vue b/examples/auth-routes/pages/index.vue new file mode 100644 index 0000000000..7c0c1f00a9 --- /dev/null +++ b/examples/auth-routes/pages/index.vue @@ -0,0 +1,66 @@ + + + Please login to see the secret content + + {{ formError }} + To login, use demo as username and demo as password. + Username: + Password: + Login + + + Hello {{ authUser.username }}! + I am the secret content, I am shown only when the use is connected. + + + + + + + diff --git a/examples/auth-routes/server.js b/examples/auth-routes/server.js new file mode 100644 index 0000000000..199bd0893d --- /dev/null +++ b/examples/auth-routes/server.js @@ -0,0 +1,50 @@ +const Nuxt = require('nuxt') +const bodyParser = require('body-parser') +const cookieSession = require('cookie-session') +const app = require('express')() + +// Body parser, to access req.body +app.use(bodyParser.json()) + +// Sessions with cookies to have req.session +app.use(cookieSession({ + name: 'nuxt-session', + keys: ['nuxt-key-1', 'nuxt-key-2'], + maxAge: 24 * 60 * 60 * 1000 // 24 hours +})) + +// POST /api/login to log in the user and add him to the req.session.authUser +app.post('/api/login', function (req, res) { + if (req.body.username === 'demo' && req.body.password === 'demo') { + req.session.authUser = { username: 'demo' } + return res.json({ username: 'demo' }) + } + res.status(401).json({ error: 'Bad credentials' }) +}) + +app.use(function (req, res, next) { + req.session.views = (req.session.views || 0) + 1 + next() +}) + +app.all('/', function (req, res) { + console.log(req.session) + res.send('Hello') +}) + +new Nuxt() +.then((nuxt) => { + app.use(nuxt.render) + // app.use(function (req, res) { + // nuxt.render(req, res) + // }) + // .then((stream) => { + // stream.pipe(fs.createFile) + // }) + app.listen(3000) + console.log('Server is listening on http://localhost:3000') +}) +.catch((error) => { + console.error(error) + process.exit(1) +}) diff --git a/examples/custom-routes/nuxt.config.js b/examples/custom-routes/nuxt.config.js index 9c26b5ff34..3878b4ac0d 100644 --- a/examples/custom-routes/nuxt.config.js +++ b/examples/custom-routes/nuxt.config.js @@ -1,7 +1,7 @@ module.exports = { router: { routes: [ - { name: 'user', path: '/users/:id(\\d+)', component: 'pages/_user' } + { path: '/users/:id(\\d+)', component: 'pages/_user' } ] }, build: {
{{ formError }}
To login, use demo as username and demo as password.
Username:
Password:
I am the secret content, I am shown only when the use is connected.