-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wangzhao
committed
Aug 2, 2018
1 parent
8b67ac8
commit 0567a24
Showing
8 changed files
with
129 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,39 @@ | ||
|
||
var express = require('express'); | ||
const router = express.Router(); | ||
// Create express router | ||
const router = express.Router() | ||
|
||
// Transform req & res to have the same API as express | ||
// So we can use res.status() & res.json() | ||
var app = express(); | ||
var app = express() | ||
router.use((req, res, next) => { | ||
Object.setPrototypeOf(req, app.request); | ||
Object.setPrototypeOf(res, app.response); | ||
req.res = res; | ||
res.req = req; | ||
next(); | ||
}); | ||
Object.setPrototypeOf(req, app.request) | ||
Object.setPrototypeOf(res, app.response) | ||
req.res = res | ||
res.req = req | ||
next() | ||
}) | ||
console.log(111, router); | ||
|
||
//Add POST - /api/login | ||
// Add POST - /api/login | ||
router.post('/login', (req, res) => { | ||
if (req.body.username == 'demo' && req.body.password == 'demo') { | ||
req.session.authUser = { username: req.body.username }; | ||
return res.json({ username: req.body.password }); | ||
console.log(111888888); | ||
if (req.body.username === 'demo' && req.body.password === 'demo') { | ||
req.session.authUser = { username: 'demo' } | ||
return res.json({ username: 'demo' }) | ||
} | ||
res.status(401).json({ message: "Bad credentials" }); | ||
}); | ||
res.status(401).json({ message: 'Bad credentials' }) | ||
}) | ||
|
||
// Add POST - /api/logout | ||
router.post('/logout', (req, res) => { | ||
delete req.session.authUser; | ||
res.json({ ok: true }); | ||
}); | ||
//Export the server middleware | ||
return { | ||
delete req.session.authUser | ||
res.json({ ok: true }) | ||
}) | ||
|
||
var middleware = { | ||
path: '/api', | ||
handler: router | ||
}; | ||
}; | ||
//Export the server middleware | ||
module.exports = middleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
export default function ({ store, redirect }) { | ||
// if (typeof localStorage === "undefined" || localStorage === null) { | ||
// var LocalStorage = require('node-localstorage').LocalStorage; | ||
// localStorage = new LocalStorage('./scratch'); | ||
// } | ||
// if (!store.state.authUser && localStorage.getItem('loginState') == 'false') { | ||
if (!store.state.authUser) { | ||
return redirect('/login'); | ||
// return redirect('/login'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,42 @@ | ||
// let config = require('./nuxt.config'); | ||
// const app = require('express')(); | ||
// const { Nuxt, Builder } = require('nuxt'); | ||
// const port = process.env.PORT || 3000; | ||
|
||
|
||
// const nuxt = new Nuxt(config); | ||
// console.log(config); | ||
|
||
// // app.use(nuxt); | ||
// if (config.dev) { | ||
// new Builder(nuxt).build().catch(err => { | ||
// console.error(error); | ||
// process.exit(1); | ||
// }) | ||
// } | ||
|
||
// // console.log(config); | ||
|
||
// // app.listen(port, '0.0.0.0').then(()=>{ | ||
// // nuxt.showOpen(); | ||
// // }) | ||
|
||
// const express = require('express') | ||
// const request = require('request') | ||
// const app = express() | ||
|
||
// app.use('/', function (req, res) { | ||
// const url = 'https://nuxt-auth-routes.glitch.me' + req.url | ||
// req.pipe(request(url)).pipe(res.set('Access-Control-Allow-Origin', '*')) | ||
// }); | ||
|
||
// app.listen(process.env.PORT || 3001); | ||
|
||
// var express = require('express'); | ||
// var app = express(); | ||
|
||
// var cors = require('cors'); | ||
// var bodyParser = require('body-parser'); | ||
|
||
// //enables cors | ||
// app.use(cors({ | ||
// 'allowedHeaders': ['sessionId', 'Content-Type'], | ||
// 'exposedHeaders': ['sessionId'], | ||
// 'origin': '*', | ||
// 'methods': 'GET,HEAD,PUT,PATCH,POST,DELETE', | ||
// 'preflightContinue': false | ||
// })); | ||
|
||
// require('./router/index')(app); | ||
const { Nuxt, Builder } = require('nuxt') | ||
const bodyParser = require('body-parser') | ||
const session = require('express-session') | ||
const app = require('express')() | ||
|
||
// Body parser, to access `req.body` | ||
app.use(bodyParser.json()) | ||
|
||
// Sessions to create `req.session` | ||
app.use(session({ | ||
secret: 'super-secret-key', | ||
resave: false, | ||
saveUninitialized: false, | ||
cookie: { maxAge: 60000 } | ||
})) | ||
|
||
// 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' }) | ||
}) | ||
|
||
// POST `/api/logout` to log out the user and remove it from the `req.session` | ||
app.post('/api/logout', function (req, res) { | ||
delete req.session.authUser | ||
res.json({ ok: true }) | ||
}) | ||
|
||
// We instantiate Nuxt.js with the options | ||
const isProd = process.env.NODE_ENV === 'production' | ||
const nuxt = new Nuxt({ dev: !isProd }) | ||
// No build in production | ||
if (!isProd) { | ||
const builder = new Builder(nuxt) | ||
builder.build() | ||
} | ||
app.use(nuxt.render) | ||
app.listen(3000) | ||
console.log('Server is listening on http://localhost:3000') |