add login redirect

This commit is contained in:
AceDataCloud committed 2021-09-11 01:59:39 +08:00
1 parent a94be71c98
commit 2a5a5c601c
12 files changed
+113 -15

No files matched your search

+1
View File
@@ -25,6 +25,7 @@
},
"devDependencies": {
"@rollup/plugin-replace": "^3.0.0",
"@types/lodash": "^4.14.172",
"@types/node": "^16.7.4",
"@vitejs/plugin-vue": "^1.6.0",
"@vue/compiler-sfc": "^3.0.5",
+25 -5
View File
@@ -14,7 +14,9 @@
import { defineComponent } from 'vue';
import QrCode from 'qrcode.vue';
import WechatService from '@/services/common/wechat/service';
import UserService from '@/services/common/user/service';
import { IWechatQRCode, IWechatQRCodeResponse, IWechatLoginStatusResponse } from '@/services/common/wechat/types';
import { IUserDetailResponse, IUser } from '@/services/common/user/types';
interface IData {
qrLink: string | undefined;
@@ -49,17 +51,35 @@ export default defineComponent({
this.ticket = data.ticket;
});
},
getUserInfo(id: string) {
UserService.get(id).then(async ({ data }: { data: IUserDetailResponse }) => {
await this.$store.dispatch('setUser', data as IUser);
this.$router.push({
name: 'home'
});
});
},
detectLoginStatus() {
if (!this.ticket) {
return;
}
// return;
WechatService.getLoginStatus(this.ticket)
.then(({ data: { access_token, refresh_token, user } }: { data: IWechatLoginStatusResponse }) => {
this.$store.dispatch('setRefreshToken', refresh_token);
this.$store.dispatch('setAccessToken', access_token);
this.$store.dispatch('setUser', user);
})
.then(
async ({
data: {
access_token,
refresh_token,
user: { id }
}
}: {
data: IWechatLoginStatusResponse;
}) => {
await this.$store.dispatch('setRefreshToken', refresh_token);
await this.$store.dispatch('setAccessToken', access_token);
this.getUserInfo(id);
}
)
.catch((error) => {
setTimeout(() => {
this.detectLoginStatus();
+1 -1
View File
@@ -10,7 +10,7 @@ export default {
children: [
{
path: 'login',
name: 'auth-login',
name: 'login',
meta: {
title: t('common.nav.article')
},
+2 -1
View File
@@ -4,7 +4,8 @@ export default {
path: '/content',
name: 'content',
meta: {
title: t('common.nav.content')
title: t('common.nav.content'),
auth: true
},
component: () => import('@/layouts/Content.vue'),
children: [
+17
View File
@@ -1,6 +1,7 @@
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router';
import content from './content';
import auth from './auth';
import store from '@/store';
const routes = [
{
@@ -19,4 +20,20 @@ const router = createRouter({
history: createWebHashHistory(),
routes
});
router.beforeEach((to, from, next) => {
if (to.matched.some((record) => record.meta.auth)) {
if (!store.getters.authenticated) {
next({
name: 'login',
query: { redirect: to.fullPath }
});
} else {
next();
}
} else {
next();
}
});
export default router;
+13
View File
@@ -0,0 +1,13 @@
import { AxiosResponse } from 'axios';
import httpClient from '../../instance';
import { IUserDetailResponse, IUser } from './types';
class UserService {
key = 'users';
async get(id: string | string[]): Promise<AxiosResponse<IUserDetailResponse>> {
return httpClient.get(`/${this.key}/${id}`);
}
}
export default new UserService();
+15
View File
@@ -0,0 +1,15 @@
export interface IUser {
id: string;
username?: string;
first_name?: string;
last_name?: string;
email?: string;
nickname?: string;
gender?: number;
city?: string;
province?: string;
country?: string;
avatar?: string;
}
export interface IUserDetailResponse extends IUser {}
+3 -3
View File
@@ -1,3 +1,5 @@
import { IUser } from '../user/types';
export interface IWechatQRCode {
ticket: string;
url: string;
@@ -8,7 +10,5 @@ export interface IWechatQRCodeResponse extends IWechatQRCode {}
export interface IWechatLoginStatusResponse {
refresh_token: string;
access_token: string;
user: {
id: string;
};
user: IUser;
}
+10
View File
@@ -1,3 +1,4 @@
import store from '@/store';
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
const httpClient: AxiosInstance = axios.create({
@@ -8,6 +9,15 @@ const httpClient: AxiosInstance = axios.create({
}
});
httpClient.interceptors.request.use((config) => {
const accessToken = store.getters.accessToken;
console.log('accesssssss', accessToken);
if (accessToken) {
config.headers['Authorization'] = `Bearer ${accessToken}`;
}
return config;
});
// const httpClient = (options: AxiosRequestConfig): Promise<AxiosResponse> => instance.request(options);
export default httpClient;
+13 -5
View File
@@ -1,10 +1,7 @@
import { createStore, ActionContext } from 'vuex';
import { IUser } from '@/services/common/user/types';
interface IUser {
id: string;
}
interface IState {
export interface IState {
count: number;
accessToken: string | undefined;
refreshToken: string | undefined;
@@ -47,6 +44,17 @@ const store = createStore({
setUser({ commit }: ActionContext<IState, IState>, payload: IUser) {
commit('setUser', payload);
}
},
getters: {
authenticated(state) {
return !!state.accessToken;
},
accessToken(state) {
return state.accessToken;
},
refreshToken(state) {
return state.refreshToken;
}
}
});
+8
View File
@@ -0,0 +1,8 @@
import { Store } from 'vuex';
import { IState } from './store/index';
declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$store: Store<IState>;
}
}
+5
View File
@@ -205,6 +205,11 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.48.tgz#18dc8091b285df90db2f25aa7d906cfc394b7f74"
integrity sha512-LfZwXoGUDo0C3me81HXgkBg5CTQYb6xzEl+fNmbO4JdRiSKQ8A0GD1OBBvKAIsbCUgoyAty7m99GqqMQe784ew==
"@types/lodash@^4.14.172":
version "4.14.172"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.172.tgz#aad774c28e7bfd7a67de25408e03ee5a8c3d028a"
integrity sha512-/BHF5HAx3em7/KkzVKm3LrsD6HZAXuXO1AJZQ3cRRBZj4oHZDviWPYu0aEplAqDFNHZPW6d3G7KN+ONcCCC7pw==
"@types/node@^16.7.4":
version "16.7.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.7.4.tgz#68a9384694af63ceab8848e95d76d9a3519e84b6"