diff --git a/components/common/user/list.vue b/components/common/user/list.vue index 816fd51..07a39a3 100644 --- a/components/common/user/list.vue +++ b/components/common/user/list.vue @@ -10,7 +10,7 @@ - {{ user.nickname }} + @@ -48,7 +48,7 @@ export default { name: "UserList", computed: { ...mapState({ - userInfo: state => state.oauth + userInfo: state => state.auth.user }) }, props: { diff --git a/components/layouts/mobile/header.vue b/components/layouts/mobile/header.vue index a4a13e8..377d78e 100644 --- a/components/layouts/mobile/header.vue +++ b/components/layouts/mobile/header.vue @@ -9,7 +9,7 @@ - 首页 专题 @@ -19,8 +19,9 @@ - - 发帖 + + 发帖 @@ -37,15 +38,17 @@ - + - + - {{ nickname }} + + {{ user.nickname }} + 系统管理 资料与账号 @@ -70,203 +73,165 @@ diff --git a/components/layouts/pc/header.vue b/components/layouts/pc/header.vue index 39b4e15..5a1bc39 100644 --- a/components/layouts/pc/header.vue +++ b/components/layouts/pc/header.vue @@ -20,7 +20,7 @@ - + - + - + - {{ nickname }} + {{ user.nickname }} 个人中心 @@ -138,251 +138,213 @@ diff --git a/middleware/authenticated.js b/middleware/authenticated.js deleted file mode 100644 index 22e4512..0000000 --- a/middleware/authenticated.js +++ /dev/null @@ -1,6 +0,0 @@ -export default function ({ store, redirect }) { - // If the user is not authenticated - if (!store.state.oauth) { - return redirect('/login') - } -} diff --git a/middleware/notAuthenticated.js b/middleware/notAuthenticated.js deleted file mode 100644 index 624fd92..0000000 --- a/middleware/notAuthenticated.js +++ /dev/null @@ -1,6 +0,0 @@ -export default function ({ store, redirect }) { - // If the user is authenticated redirect to home page - if (store.state.oauth) { - return redirect('/') - } -} diff --git a/nuxt.config.js b/nuxt.config.js index 17f4ab4..f23eb4f 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -70,9 +70,44 @@ export default { modules: [ '@nuxtjs/axios', '@nuxtjs/proxy', - 'js-cookie', - 'cookieparser' + '@nuxtjs/auth-next' ], + auth: { + redirect: { + login: '/login', + logout: false, + home: false + }, + strategies: { + local: { + // scope: true, + scheme: 'refresh', + token: { + property: 'token', + global: true, + maxAge: 60 * 15, + // required: true, + type: false + }, + refreshToken: { + property: 'refreshToken', + data: 'refreshToken', + maxAge: 60 * 60 * 2 + }, + user: { + property: 'user', + autoFetch: false + }, + endpoints: { + login: { url: '/api/auth/login', method: 'post' }, + logout: { url: '/api/auth/logout', method: 'post' }, + refresh: { url: '/api/auth/refresh-token', method: 'post' }, + user: { url: '/api/auth/user', method: 'get' } + }, + autoLogout: false + } + } + }, axios: { proxy: true // 开启proxy }, diff --git a/package.json b/package.json index 8b6f0dd..ab3b621 100644 --- a/package.json +++ b/package.json @@ -11,13 +11,12 @@ }, "dependencies": { "@chenfengyuan/vue-qrcode": "^1.0.2", + "@nuxtjs/auth-next": "^5.0.0-1648802546.c9880dc", "@nuxtjs/axios": "^5.13.1", "babel-plugin-lodash": "^3.3.4", - "cookieparser": "^0.1.0", "echarts": "^4.9.0", "element-ui": "^2.15.8", "express": "^4.18.1", - "js-cookie": "^2.2.1", "lodash": "^4.17.21", "net": "^1.0.2", "nuxt": "^2.15.8", diff --git a/pages/admin.vue b/pages/admin.vue index 4df8de0..e968c88 100644 --- a/pages/admin.vue +++ b/pages/admin.vue @@ -45,6 +45,7 @@ import _ from 'lodash' export default { name: "Admin", + middleware: 'auth', data() { return { menus: [ @@ -144,7 +145,7 @@ export default { return this.$store.state.activeMenu; }, hasPermissions() { - return this.$store.getters.hasPermissions('blog_admin'); + return this.$auth.hasScope('admin') || this.$auth.hasScope('blog_admin'); } }, methods: { diff --git a/pages/admin/articles.vue b/pages/admin/articles.vue index d21a11a..bb5b231 100644 --- a/pages/admin/articles.vue +++ b/pages/admin/articles.vue @@ -107,6 +107,7 @@ import EditTags from '~/components/widget/tags'; export default { name: "articles", + middleware: 'auth', components: { EditTags }, diff --git a/pages/admin/bank-accounts.vue b/pages/admin/bank-accounts.vue index bd91284..0059f7b 100644 --- a/pages/admin/bank-accounts.vue +++ b/pages/admin/bank-accounts.vue @@ -75,6 +75,7 @@ import Records from "../../components/common/bank/account/records"; export default { name: "bank-accounts", + middleware: 'auth', components: {Records}, fetch() { let {store, params, error} = this.$nuxt.context diff --git a/pages/admin/banks.vue b/pages/admin/banks.vue index 4e53bff..ce2f04a 100644 --- a/pages/admin/banks.vue +++ b/pages/admin/banks.vue @@ -87,6 +87,7 @@ import {mapState} from 'vuex'; export default { name: "banks", + middleware: 'auth', fetch() { let {store, params, error} = this.$nuxt.context return Promise.all([ diff --git a/pages/admin/comments.vue b/pages/admin/comments.vue index db406d0..8a50fec 100644 --- a/pages/admin/comments.vue +++ b/pages/admin/comments.vue @@ -97,6 +97,7 @@ import {mapState} from 'vuex'; export default { name: "comments", + middleware: 'auth', fetch() { let {store, params, error} = this.$nuxt.context return Promise.all([ diff --git a/pages/admin/currency-rules.vue b/pages/admin/currency-rules.vue index 546923f..63e0e45 100644 --- a/pages/admin/currency-rules.vue +++ b/pages/admin/currency-rules.vue @@ -81,6 +81,7 @@ import {mapState} from 'vuex'; export default { name: "currency-rules", + middleware: 'auth', fetch() { let {store, params, error} = this.$nuxt.context return Promise.all([ diff --git a/pages/admin/dashboard.vue b/pages/admin/dashboard.vue index e007989..5228568 100644 --- a/pages/admin/dashboard.vue +++ b/pages/admin/dashboard.vue @@ -248,6 +248,7 @@ import EditTags from '~/components/widget/tags'; Vue.prototype.$echarts = echarts; export default { name: "Dashboard", + middleware: 'auth', components: { EditTags }, diff --git a/pages/admin/products.vue b/pages/admin/products.vue index c586118..8469984 100644 --- a/pages/admin/products.vue +++ b/pages/admin/products.vue @@ -80,6 +80,7 @@ import {mapState} from 'vuex'; export default { name: "products", + middleware: 'auth', fetch() { let {store, params, error} = this.$nuxt.context return Promise.all([ diff --git a/pages/admin/roles.vue b/pages/admin/roles.vue index 2d2d40b..be0093a 100644 --- a/pages/admin/roles.vue +++ b/pages/admin/roles.vue @@ -91,6 +91,7 @@ export default { name: "roles", + middleware: 'auth', fetch() { let {store, params, error} = this.$nuxt.context return Promise.all([ diff --git a/pages/admin/tag/post/_tag_id.vue b/pages/admin/tag/post/_tag_id.vue index 6356ed9..1da85b2 100644 --- a/pages/admin/tag/post/_tag_id.vue +++ b/pages/admin/tag/post/_tag_id.vue @@ -126,6 +126,7 @@ import apiConfig from '~/config/api.config'; export default { name: "PostTag", + middleware: 'auth', components: { VueCropper }, diff --git a/pages/admin/tags.vue b/pages/admin/tags.vue index 96e9996..4e5ee15 100644 --- a/pages/admin/tags.vue +++ b/pages/admin/tags.vue @@ -48,6 +48,7 @@ export default { name: "tags", + middleware: 'auth', fetch() { let {store, params, error} = this.$nuxt.context return Promise.all([ diff --git a/pages/admin/topic/_topic_uri.vue b/pages/admin/topic/_topic_uri.vue index 30f7e89..5ffbbd3 100644 --- a/pages/admin/topic/_topic_uri.vue +++ b/pages/admin/topic/_topic_uri.vue @@ -71,6 +71,7 @@ import {mapState} from 'vuex'; export default { name: "adminTopicDetail", + middleware: 'auth', validate({params, store}) { if (typeof params.topic_uri === 'undefined') { return true; @@ -94,7 +95,7 @@ export default { tags: state => state.topic.tags.data }), hasPermissions() { - return this.$store.getters.hasPermissions('topic'); + return this.$auth.hasScope('topic'); } }, methods: { diff --git a/pages/admin/topic/bind/_topic_id.vue b/pages/admin/topic/bind/_topic_id.vue index ceec887..3d34064 100644 --- a/pages/admin/topic/bind/_topic_id.vue +++ b/pages/admin/topic/bind/_topic_id.vue @@ -53,6 +53,7 @@ import {mapState} from 'vuex'; export default { name: "adminTopicBindTag", + middleware: 'auth', fetch({store, params, error}) { let _ts = this; return Promise.all([ diff --git a/pages/admin/topic/post/_topic_id.vue b/pages/admin/topic/post/_topic_id.vue index a92434f..e3f186e 100644 --- a/pages/admin/topic/post/_topic_id.vue +++ b/pages/admin/topic/post/_topic_id.vue @@ -124,6 +124,7 @@ import apiConfig from '~/config/api.config'; export default { name: "adminTopicPost", + middleware: 'auth', components: { VueCropper }, diff --git a/pages/admin/topics.vue b/pages/admin/topics.vue index 347a5a2..2b864b4 100644 --- a/pages/admin/topics.vue +++ b/pages/admin/topics.vue @@ -39,6 +39,7 @@ export default { name: "topics", + middleware: 'auth', fetch() { let {store, params, error} = this.$nuxt.context return Promise.all([ diff --git a/pages/admin/users.vue b/pages/admin/users.vue index 0f9cb1f..e740506 100644 --- a/pages/admin/users.vue +++ b/pages/admin/users.vue @@ -119,6 +119,7 @@ import {mapState} from 'vuex'; export default { name: "users", + middleware: 'auth', fetch() { let {store, params, error} = this.$nuxt.context return Promise.all([ diff --git a/pages/answer.vue b/pages/answer.vue index aafc324..6dec761 100644 --- a/pages/answer.vue +++ b/pages/answer.vue @@ -62,6 +62,7 @@ import {mapState} from 'vuex'; export default { name: "answer", + middleware: 'auth', fetch({store, params, error}) { return Promise.all([ store diff --git a/pages/article/_article_id.vue b/pages/article/_article_id.vue index 1c6840a..a3d7ee1 100644 --- a/pages/article/_article_id.vue +++ b/pages/article/_article_id.vue @@ -82,7 +82,7 @@ - + state.article.detail.data, isFetching: state => state.article.detail.fetching, isMobile: state => state.global.isMobile, - user: state => state.oauth, - avatar: state => state.userInfo?.avatarURL + loggedIn: state => state.auth.loggedIn, + user: state => state.auth.user, + avatar: state => state.auth.user.avatarUrl }), hasPermissions() { - let account = this.$store.state.userInfo?.nickname; + let account = this.$store.state.auth.user?.nickname; if (account) { if (account === this.article.articleAuthor.userNickname) { return true; @@ -205,7 +206,7 @@ export default { return false; }, isAdmin() { - return this.$store.getters.hasPermissions('blog_admin'); + return this.$auth.hasScope('admin') || this.$auth.hasScope('blog_admin'); }, routeArticleId() { return Number(this.$route.params.article_id); diff --git a/pages/article/post/_article_id.vue b/pages/article/post/_article_id.vue index 729d134..823a57d 100644 --- a/pages/article/post/_article_id.vue +++ b/pages/article/post/_article_id.vue @@ -63,6 +63,7 @@ export default { name: "PostArticle", + middleware: 'auth', validate({params, store}) { if (typeof params.article_id === 'undefined') { return true; @@ -80,7 +81,7 @@ article: state => state.article.detail.data }), hasPermissions() { - let account = this.$store.state.userInfo?.nickname; + let account = this.$store.state.auth.user?.nickname; if (account) { if (this.$route.params.article_id) { if (account === this.article.articleAuthor.userNickname) { @@ -90,7 +91,7 @@ return true; } } - return this.$store.getters.hasPermissions('blog_admin'); + return this.$auth.hasScope('blog_admin'); } }, data() { diff --git a/pages/chats/_account.vue b/pages/chats/_account.vue index ba60630..e300bd1 100644 --- a/pages/chats/_account.vue +++ b/pages/chats/_account.vue @@ -45,7 +45,7 @@ export default { name: "Chat", computed: { ...mapState({ - user: state => state.userInfo + user: state => state.auth.user }) }, data() { diff --git a/pages/draft/_draft_id.vue b/pages/draft/_draft_id.vue index c9189f2..54dae58 100644 --- a/pages/draft/_draft_id.vue +++ b/pages/draft/_draft_id.vue @@ -71,6 +71,7 @@ export default { name: "DraftDetail", + middleware: 'auth', validate({params, store}) { return params.draft_id && !isNaN(Number(params.draft_id)) }, @@ -86,17 +87,17 @@ article: state => state.draft.detail.data, isFetching: state => state.draft.detail.fetching, isMobile: state => state.global.isMobile, - user: state => state.oauth, - avatar: state => state.userInfo?.avatarURL + user: state => state.auth.user, + avatar: state => state.auth.user?.avatarURL }), hasPermissions() { - let account = this.$store.state.userInfo?.nickname; + let account = this.$store.state.auth.user?.nickname; if (account) { if (account === this.article?.articleAuthor?.userNickname) { return true; } } - return this.$store.getters.hasPermissions('blog_admin'); + return this.$auth.hasScope('blog_admin'); }, routeArticleId() { return Number(this.$route.params.draft_id) diff --git a/pages/drafts.vue b/pages/drafts.vue index 7b6f141..8c032d4 100644 --- a/pages/drafts.vue +++ b/pages/drafts.vue @@ -13,6 +13,7 @@ export default { name: "Drafts", + middleware: 'auth', components: { DraftList }, @@ -27,7 +28,7 @@ computed: { ...mapState({ articles: state => state.draft.list.data, - user: state => state.oauth + user: state => state.auth.user }) }, mounted() { diff --git a/pages/login.vue b/pages/login.vue index d0cd43f..8347005 100644 --- a/pages/login.vue +++ b/pages/login.vue @@ -54,10 +54,9 @@ -
-
+
+
取消关注 关注 聊天 @@ -176,7 +176,7 @@ export default { portfolios: state => state.user.portfolios, followers: state => state.user.followers, followings: state => state.user.followings, - oauth: state => state.oauth + auth: state => state.auth.user }) }, data() { @@ -227,7 +227,7 @@ export default { }, followUser(idUser) { let _ts = this; - if (_ts.oauth) { + if (_ts.auth) { _ts.$axios.$post('/api/follow', { followingId: idUser, followingType: 0 @@ -241,7 +241,7 @@ export default { }, cancelFollowUser(idUser) { let _ts = this; - if (_ts.oauth) { + if (_ts.auth) { _ts.$axios.$post('/api/follow/cancel-follow', { followingId: idUser, followingType: 0 @@ -265,7 +265,7 @@ export default { mounted() { let _ts = this; this.$store.commit('setActiveMenu', 'user'); - if (_ts.oauth) { + if (_ts.auth) { _ts.$axios.$get('/api/follow/is-follow', { params: { followingId: _ts.user.idUser, diff --git a/pages/user/settings.vue b/pages/user/settings.vue index b76ef4a..4a5eac6 100644 --- a/pages/user/settings.vue +++ b/pages/user/settings.vue @@ -47,12 +47,13 @@