From 96e52a14cc950b89e45470732b6149d705381898 Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Fri, 22 Feb 2019 22:38:56 +0300 Subject: [PATCH] Authorization --- build/webpack.dev.conf.js | 2 +- build/webpack.prod.conf.js | 2 +- config/dev.env.js | 2 +- index.html | 2 +- src/api/login.js | 45 ++++++++++------ src/lang/en.js | 3 +- src/permission.js | 2 +- src/router/index.js | 38 +++++--------- src/store/modules/user.js | 17 +++--- src/utils/request.js | 4 +- src/views/login/index.vue | 103 ++++--------------------------------- 11 files changed, 67 insertions(+), 153 deletions(-) diff --git a/build/webpack.dev.conf.js b/build/webpack.dev.conf.js index 26a5584a..5df16341 100644 --- a/build/webpack.dev.conf.js +++ b/build/webpack.dev.conf.js @@ -57,7 +57,7 @@ const devWebpackConfig = merge(baseWebpackConfig, { template: 'index.html', inject: true, favicon: resolve('favicon.ico'), - title: 'vue-element-admin', + title: 'Admin FE', templateParameters: { BASE_URL: config.dev.assetsPublicPath + config.dev.assetsSubDirectory, }, diff --git a/build/webpack.prod.conf.js b/build/webpack.prod.conf.js index 4f84e0c6..5e991636 100644 --- a/build/webpack.prod.conf.js +++ b/build/webpack.prod.conf.js @@ -55,7 +55,7 @@ const webpackConfig = merge(baseWebpackConfig, { template: 'index.html', inject: true, favicon: resolve('favicon.ico'), - title: 'vue-element-admin', + title: 'Admin FE', templateParameters: { BASE_URL: config.build.assetsPublicPath + config.build.assetsSubDirectory, }, diff --git a/config/dev.env.js b/config/dev.env.js index 68ddea56..13ea61f7 100644 --- a/config/dev.env.js +++ b/config/dev.env.js @@ -1,5 +1,5 @@ module.exports = { NODE_ENV: '"development"', ENV_CONFIG: '"dev"', - BASE_API: '"https://api-dev"' + BASE_API: '"http://localhost:4000"' } diff --git a/index.html b/index.html index 7a7ecacf..2f5a3a50 100644 --- a/index.html +++ b/index.html @@ -5,7 +5,7 @@ - vue-element-admin + Admin FE diff --git a/src/api/login.js b/src/api/login.js index a64935c3..caef8d8e 100644 --- a/src/api/login.js +++ b/src/api/login.js @@ -1,29 +1,42 @@ import request from '@/utils/request' -export function loginByUsername(username, password) { - const data = { - username, - password - } - return request({ - url: '/login/login', +export async function loginByUsername(username, password) { + const appsRequest = await request({ + url: '/api/v1/apps', method: 'post', - data + data: { + client_name: `AdminFE_${Math.random()}`, + redirect_uris: `${window.location.origin}/oauth-callback`, + scopes: 'read write follow' + } + }) + + const app = appsRequest.data + + return request({ + url: '/oauth/token', + method: 'post', + data: { + client_id: app.client_id, + client_secret: app.client_secret, + grant_type: 'password', + username: username, + password: password + } }) } -export function logout() { +export function getUserInfo() { return request({ - url: '/login/logout', + url: '/api/account/verify_credentials', method: 'post' }) } -export function getUserInfo(token) { - return request({ - url: '/user/info', - method: 'get', - params: { token } - }) +export function logout() { + } +const oauth = { loginByUsername, getUserInfo, logout } + +export default oauth diff --git a/src/lang/en.js b/src/lang/en.js index 05b34598..e9890373 100644 --- a/src/lang/en.js +++ b/src/lang/en.js @@ -63,7 +63,8 @@ export default { theme: 'Theme', clipboardDemo: 'Clipboard', i18n: 'I18n', - externalLink: 'External Link' + externalLink: 'External Link', + users: 'Users' }, navbar: { logOut: 'Log Out', diff --git a/src/permission.js b/src/permission.js index e556cb00..acd01b74 100644 --- a/src/permission.js +++ b/src/permission.js @@ -26,7 +26,7 @@ router.beforeEach((to, from, next) => { } else { if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息 store.dispatch('GetUserInfo').then(res => { // 拉取user_info - const roles = res.data.roles // note: roles must be a array! such as: ['editor','develop'] + const roles = res.data.rights.admin ? ['admin'] : [] store.dispatch('GenerateRoutes', { roles }).then(() => { // 根据roles权限生成可访问的路由表 router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表 next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record diff --git a/src/router/index.js b/src/router/index.js index 60524517..26243be7 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -76,32 +76,6 @@ export const constantRouterMap = [ meta: { title: 'dashboard', icon: 'dashboard', noCache: true, affix: true } } ] - }, - { - path: '/documentation', - component: Layout, - redirect: '/documentation/index', - children: [ - { - path: 'index', - component: () => import('@/views/documentation/index'), - name: 'Documentation', - meta: { title: 'documentation', icon: 'documentation', affix: true } - } - ] - }, - { - path: '/guide', - component: Layout, - redirect: '/guide/index', - children: [ - { - path: 'index', - component: () => import('@/views/guide/index'), - name: 'Guide', - meta: { title: 'guide', icon: 'guide', noCache: true } - } - ] } ] @@ -112,6 +86,18 @@ export default new Router({ }) export const asyncRouterMap = [ + { + path: '/users', + component: Layout, + children: [ + { + path: 'index', + component: () => import('@/views/users/index'), + name: 'Users', + meta: { title: 'users', icon: 'peoples', noCache: true } + } + ] + }, { path: '/permission', component: Layout, diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 38e81a36..bee8dbbc 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -44,14 +44,13 @@ const user = { }, actions: { - // 用户名登录 LoginByUsername({ commit }, userInfo) { const username = userInfo.username.trim() return new Promise((resolve, reject) => { loginByUsername(username, userInfo.password).then(response => { const data = response.data - commit('SET_TOKEN', data.token) - setToken(response.data.token) + commit('SET_TOKEN', data.access_token) + setToken(response.data.access_token) resolve() }).catch(error => { reject(error) @@ -59,25 +58,25 @@ const user = { }) }, - // 获取用户信息 GetUserInfo({ commit, state }) { return new Promise((resolve, reject) => { getUserInfo(state.token).then(response => { - // 由于mockjs 不支持自定义状态码只能这样hack if (!response.data) { reject('Verification failed, please login again.') } const data = response.data - if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组 - commit('SET_ROLES', data.roles) + if (data.rights) { + if (data.rights.admin) { + commit('SET_ROLES', ['admin']) + } } else { reject('getInfo: roles must be a non-null array!') } commit('SET_NAME', data.name) - commit('SET_AVATAR', data.avatar) - commit('SET_INTRODUCTION', data.introduction) + commit('SET_AVATAR', data.profile_image_url) + commit('SET_INTRODUCTION', '') resolve(response) }).catch(error => { reject(error) diff --git a/src/utils/request.js b/src/utils/request.js index 50f9ecec..e7966dc2 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -12,10 +12,8 @@ const service = axios.create({ // request interceptor service.interceptors.request.use( config => { - // Do something before request is sent if (store.getters.token) { - // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改 - config.headers['X-Token'] = getToken() + config.headers['Authorization'] = `Bearer ${getToken()}` } return config }, diff --git a/src/views/login/index.vue b/src/views/login/index.vue index 634c32da..78eae31d 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -1,11 +1,10 @@