Skip to content

Commit 88bfbd2

Browse files
authored
Merge pull request #34 from clarkdo/nuxt-1.0.0
chore: code refacor
2 parents 2c8c730 + 7e15d76 commit 88bfbd2

File tree

11 files changed

+65
-55
lines changed

11 files changed

+65
-55
lines changed

.vscode/cSpell.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"tooltips",
2323
"chartjs",
2424
"contenthash",
25-
"mixins"
25+
"mixins",
26+
"consts"
2627
],
2728
// flagWords - list of words to be always considered incorrect
2829
// This is useful for offensive words and common spelling errors.

client/components/Navbar.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</nuxt-link>
2222
</div>
2323
</el-menu>
24-
<ul class="el-menu el-menu-demo el-menu--dark">
24+
<ul v-else class="el-menu el-menu-demo el-menu--dark">
2525
<header>
2626
<img src="~assets/img/logo.svg" alt="Element">
2727
</header>
@@ -63,6 +63,8 @@ export default class Navbar extends Vue {
6363
<style lang="scss" scoped>
6464
.navbar {
6565
position: fixed;
66+
overflow-y: auto;
67+
overflow-x: hidden;
6668
width: 16.66667%;
6769
height: 100%;
6870
z-index: 9999;

client/pages/login.vue

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,9 @@ export default class Login extends Vue {
8080
}
8181
})
8282
}
83-
getCaptcha () {
84-
axios.get('/hpi/captcha')
85-
.then((res) => {
86-
this.captchaSvg = res.data
87-
})
83+
async getCaptcha () {
84+
const {data: captcha} = await axios.get('/hpi/captcha')
85+
this.captchaSvg = captcha
8886
}
8987
9088
refreshCaptcha = debounce(this.getCaptcha, 500)

client/store/index.js

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,27 @@ export const getters = {
3636
export const actions = {
3737
nuxtServerInit ({ commit }, { req }) {
3838
},
39-
login ({ commit }, { userName, password, captcha }) {
40-
return axios.post('/hpi/login', {
41-
userName,
42-
password,
43-
captcha
44-
})
45-
.then((res) => {
46-
let token = res.data['access_token']
47-
setToken(token)
48-
commit('SET_USER', getUserFromToken(token))
49-
})
50-
.catch((error) => {
51-
let message = error.message
52-
if (error.response.data) {
53-
message = (error.response.data.message || message)
54-
}
55-
throw new Error(message)
56-
})
39+
async login ({ commit }, { userName, password, captcha }) {
40+
try {
41+
const {data: {access_token: token}} = await axios.post(
42+
'/hpi/login',
43+
{userName, password, captcha}
44+
)
45+
setToken(token)
46+
commit('SET_USER', getUserFromToken(token))
47+
} catch (error) {
48+
let message = error.message
49+
if (error.response.data) {
50+
message = (error.response.data.message || message)
51+
}
52+
throw new Error(message)
53+
}
5754
},
58-
logout ({ commit }, callback) {
59-
return axios.post('/hpi/logout')
60-
.then(() => {
61-
commit('SET_USER', null)
62-
unsetToken()
63-
callback()
64-
})
55+
async logout ({ commit }, callback) {
56+
await axios.post('/hpi/logout')
57+
commit('SET_USER', null)
58+
unsetToken()
59+
callback()
6560
},
6661
toggleMenu ({ commit }) {
6762
commit('SET_MENU_HIDDEN')

client/utils/auth.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import axios from 'axios'
22
import cookie from 'cookie'
33
import cookies from 'js-cookie'
44
import jwtDecode from 'jwt-decode'
5+
import consts from '~/utils/consts'
56

67
const inBrowser = typeof window !== 'undefined'
8+
const jwtKey = consts.COOKIE_JWT
79

810
export const setToken = (token) => {
911
if (process.SERVER_BUILD) return
@@ -12,14 +14,14 @@ export const setToken = (token) => {
1214
value: token,
1315
exp: exp
1416
}))
15-
cookies.set('jwt', token, { expires: new Date(exp) })
17+
cookies.set(jwtKey, token, { expires: new Date(exp) })
1618
setAuthHeader()
1719
}
1820

1921
export const unsetToken = () => {
2022
if (process.SERVER_BUILD) return
2123
window.localStorage.removeItem('token')
22-
cookies.remove('jwt')
24+
cookies.remove(jwtKey)
2325
window.localStorage.setItem('logout', Date.now())
2426
setAuthHeader()
2527
}
@@ -45,7 +47,7 @@ export const getTokenFromSession = (req) => {
4547
export const getTokenFromCookie = (req) => {
4648
const cookieStr = inBrowser ? document.cookie : req.headers.cookie
4749
const cookies = cookie.parse(cookieStr || '') || {}
48-
return cookies.jwt
50+
return cookies[jwtKey]
4951
}
5052

5153
export const getUserFromLocalStorage = () => {

client/utils/consts.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default Object.freeze({
2+
APP: 'hare',
3+
API: 'hpi',
4+
BASE_API: '/hpi',
5+
SESS_KEY: 'hare:sess',
6+
COOKIE_JWT: 'hare_jwt'
7+
})

server/api/routes-auth.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import axios from 'axios'
55
import querystring from 'querystring'
66
import koaRouter from 'koa-router'
77
import svgCaptcha from 'svg-captcha'
8-
import constants from '../utils/constants'
8+
import consts from '../utils/consts'
99

1010
const router = koaRouter({
11-
prefix: constants.BASE_API
11+
prefix: consts.BASE_API
1212
}) // router middleware for koa
1313

1414
var request = axios.create({
15-
baseURL: constants.LB_ADDR,
15+
baseURL: consts.LB_ADDR,
1616
timeout: 5000,
1717
headers: {
1818
'Authorization': 'Basic YmFzLWNsaWVudDpYMmNYeW1nWkRrRkE3RWR0',

server/api/routes-demo.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
/* Route to handle authentication /auth element */
33
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
44
import koaRouter from 'koa-router'
5-
import constants from '../utils/constants'
5+
import consts from '../utils/consts'
66

77
const router = koaRouter({
8-
prefix: constants.BASE_API
8+
prefix: consts.BASE_API
99
}) // router middleware for koa
1010

1111
router.get('/activities', async function getActivities (ctx) {

server/app.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import compose from 'koa-compose'// middleware composer
1010
import compress from 'koa-compress'// HTTP compression
1111
import session from 'koa-session'// session for flash messages
1212
import api from './api'
13-
import constants from './utils/constants'
13+
import consts from './utils/consts'
1414
import config from '../nuxt.config.js'
1515
import debugModule from 'debug'// small debugging utility
1616

@@ -19,8 +19,8 @@ import proxy from 'koa-proxies'
1919
// Start nuxt.js
2020
async function start () {
2121
const isWin = /^win/.test(process.platform)
22-
const host = constants.HOST
23-
const port = constants.PORT
22+
const host = consts.HOST
23+
const port = consts.PORT
2424
const debug = debugModule('app')
2525
const app = new Koa()
2626

@@ -65,8 +65,11 @@ async function start () {
6565
await next()
6666
})
6767

68+
const SESSION_CONFIG = {
69+
key: consts.SESS_KEY
70+
}
6871
// session for flash messages (uses signed session cookies, with no server storage)
69-
app.use(session(app))// note [email protected] is v1 middleware which generates deprecation notice
72+
app.use(session(SESSION_CONFIG, app))// note [email protected] is v1 middleware which generates deprecation notice
7073

7174
const nuxt = new Nuxt(config)
7275
// Build only in dev mode
@@ -83,7 +86,7 @@ async function start () {
8386

8487
app.use(async (ctx, next) => {
8588
await next()
86-
if (ctx.state.subapp !== constants.API) {
89+
if (ctx.state.subapp !== consts.API) {
8790
ctx.status = 200 // koa defaults to 404 when it sees that status is unset
8891
ctx.req.session = ctx.session
8992
await nuxt.render(ctx.req, ctx.res)
@@ -118,7 +121,7 @@ async function start () {
118121
// note no 'next' after composed subapp, this must be the last middleware
119122
app.use(async function composeSubapp (ctx, next) {
120123
switch (ctx.state.subapp) {
121-
case constants.API: await compose(api.middleware)(ctx); break
124+
case consts.API: await compose(api.middleware)(ctx); break
122125
}
123126
})
124127

server/utils/constants.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)