Refactor token handling and update login flow
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
import { isArray } from "~/utils/utils";
|
||||
import refreshToken from "~/utils/RefreshToken";
|
||||
|
||||
type FetchType = typeof $fetch;
|
||||
export type FetchOptions = Parameters<FetchType>[1];
|
||||
@ -7,16 +8,15 @@ const useClientRequest = async <T = unknown>(
|
||||
url: string,
|
||||
opts?: FetchOptions
|
||||
) => {
|
||||
const token = useCookie<string | undefined>("token");
|
||||
// const token = useCookie<string | undefined>("token");
|
||||
const runtimeConfig = useRuntimeConfig();
|
||||
|
||||
const defaultOptions: FetchOptions = {
|
||||
baseURL: runtimeConfig.public.apiBase,
|
||||
onRequest({ options }) {
|
||||
options.headers = options.headers || 'application/json';
|
||||
if (token.value) {
|
||||
// @ts-ignore
|
||||
options.headers["authorization"] = "Bearer " + token.value;
|
||||
if (refreshToken.getToken().token) {
|
||||
options.headers.set("Authorization", `Bearer ${refreshToken.getToken().token}`);
|
||||
}
|
||||
},
|
||||
onResponse({ response }) {
|
||||
|
||||
@ -4,14 +4,7 @@ import { getUserInfo } from "~/api/common/index";
|
||||
import type { AppMemberUserInfoRespVO } from "~/api/common/types";
|
||||
import MQTTClient from "~/utils/mqttClient";
|
||||
import { socialLoginByCode } from "~/api/pay";
|
||||
// const {
|
||||
// $openLogin,
|
||||
// $closeLogin,
|
||||
// $openRegister,
|
||||
// $closeRegister,
|
||||
// $openLoginEmail,
|
||||
// $closeLoginEmail,
|
||||
// } = useNuxtApp();
|
||||
const app = useNuxtApp();
|
||||
|
||||
export default defineStore("useUserStore", {
|
||||
state: () => ({
|
||||
@ -90,8 +83,7 @@ export default defineStore("useUserStore", {
|
||||
// 打开登录界面
|
||||
if (!data.accessToken) {
|
||||
ElMessage.error("因你未绑定手机号,请先绑定手机号");
|
||||
// @ts-ignore
|
||||
// $openLogin("verify", row.code, row.type, row.state);
|
||||
app.$openLogin("verify", row.code, row.type, row.state);
|
||||
} else {
|
||||
refreshToken.setToken(data.accessToken, data.refreshToken);
|
||||
refreshToken.setUserId(data.userId.toString());
|
||||
|
||||
Reference in New Issue
Block a user