Skip to content

Commit

Permalink
wrap up initial
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcatbuzz committed Jul 2, 2018
1 parent 2cd953b commit 66a2cb8
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 49 deletions.
22 changes: 22 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const express = require('express');

const router = express.Router();

const app = express();
router.use((req, res, next) => {
Object.setPrototypeOf(req, app.request)
Object.setPrototypeOf(res, app.response)
req.res = res
res.req = req
next()
})

router.post('/track-data', (req, res) => {
console.log('Stored data!', req.body.data);
res.status(200).json({ messages: 'Success!' });
});

module.exports = {
path: '/api',
handler: router
}
21 changes: 21 additions & 0 deletions nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const pkg = require('./package')
const bodyParser = require('body-parser')
const axios = require('axios')

module.exports = {
mode: 'universal',
Expand Down Expand Up @@ -68,5 +70,24 @@ module.exports = {
transition: {
name: 'fade',
mode: 'out-in'
},
serverMiddleware: [
bodyParser.json(),
'~api'
],
generate: {
routes: function() {
return axios.get('https://nuxt-vue-blog.firebaseio.com/posts.json')
.then(res => {
const routes = []
for (const key in res.data) {
routes.push({
route: '/posts/' + key,
payload: {postData: res.data[key]}
});
}
return routes
});
}
}
}
120 changes: 74 additions & 46 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"dependencies": {
"@nuxtjs/axios": "^5.3.1",
"axios": "^0.18.0",
"body-parser": "^1.18.3",
"express": "^4.16.3",
"js-cookie": "^2.2.0",
"nuxt": "^1.0.0"
},
Expand Down
5 changes: 5 additions & 0 deletions pages/posts/_id/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<script>
export default {
asyncData(context) {
if (context.payload) {
return {
loadedPost: context.payload.postData
}
}
return context.app.$axios.$get('/posts/' + context.params.id + '.json')
.then(data => {
return {
Expand Down
9 changes: 6 additions & 3 deletions store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const createStore = () => {
localStorage.setItem('tokenExpiration', new Date().getTime() + Number.parseInt(result.expiresIn) * 1000);
Cookie.set('jwt', result.idToken);
Cookie.set('expirationDate', new Date().getTime() + Number.parseInt(result.expiresIn) * 1000);
return this.$axios.$post('http://localhost:3000/api/track-data', { data: 'Authenticated!!!' })
})
.catch(e => console.log(e.response));
},
Expand All @@ -97,7 +98,7 @@ const createStore = () => {
.split(';')
.find(c => c.trim().startsWith('expirationDate='))
.split('=')[1];
} else {
} else if (process.client) {
token = localStorage.getItem('token');
expirationDate = localStorage.getItem('tokenExpiration');
}
Expand All @@ -112,8 +113,10 @@ const createStore = () => {
vuexContext.commit('clearToken');
Cookie.remove('jwt');
Cookie.remove('expirationDate');
localStorage.removeItem('token');
localStorage.removeItem('tokenExpiration');
if (process.client) {
localStorage.removeItem('token');
localStorage.removeItem('tokenExpiration');
}
}
},
getters: {
Expand Down

0 comments on commit 66a2cb8

Please sign in to comment.