fix: fix typos
This commit is contained in:
+148
-153
@@ -1,165 +1,160 @@
|
|||||||
import { createApi } from "@reduxjs/toolkit/query/react";
|
import { createApi } from "@reduxjs/toolkit/query/react";
|
||||||
import { nanoid } from "@reduxjs/toolkit";
|
import { nanoid } from "@reduxjs/toolkit";
|
||||||
import baseQuery from "./base.query";
|
import baseQuery from "./base.query";
|
||||||
import {
|
import { setAuthData, updateToken, resetAuthData, updateInitialized } from "../slices/auth.data";
|
||||||
setAuthData,
|
|
||||||
updateToken,
|
|
||||||
resetAuthData,
|
|
||||||
updateInitilize,
|
|
||||||
} from "../slices/auth.data";
|
|
||||||
import BASE_URL, { KEY_DEVICE_KEY } from "../config";
|
import BASE_URL, { KEY_DEVICE_KEY } from "../config";
|
||||||
const getDeviceId = () => {
|
const getDeviceId = () => {
|
||||||
let d = localStorage.getItem(KEY_DEVICE_KEY);
|
let d = localStorage.getItem(KEY_DEVICE_KEY);
|
||||||
if (!d) {
|
if (!d) {
|
||||||
d = `web:${nanoid()}`;
|
d = `web:${nanoid()}`;
|
||||||
localStorage.setItem(KEY_DEVICE_KEY, d);
|
localStorage.setItem(KEY_DEVICE_KEY, d);
|
||||||
}
|
}
|
||||||
return d;
|
return d;
|
||||||
};
|
};
|
||||||
export const authApi = createApi({
|
export const authApi = createApi({
|
||||||
reducerPath: "authApi",
|
reducerPath: "authApi",
|
||||||
baseQuery,
|
baseQuery,
|
||||||
endpoints: (builder) => ({
|
endpoints: (builder) => ({
|
||||||
login: builder.mutation({
|
login: builder.mutation({
|
||||||
query: (credentials) => ({
|
query: (credentials) => ({
|
||||||
url: "token/login",
|
url: "token/login",
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: {
|
body: {
|
||||||
credential: credentials,
|
credential: credentials,
|
||||||
device: getDeviceId(),
|
device: getDeviceId(),
|
||||||
device_token: "test",
|
device_token: "test"
|
||||||
},
|
}
|
||||||
}),
|
}),
|
||||||
transformResponse: (data) => {
|
transformResponse: (data) => {
|
||||||
const { avatar_updated_at } = data.user;
|
const { avatar_updated_at } = data.user;
|
||||||
data.user.avatar =
|
data.user.avatar =
|
||||||
avatar_updated_at == 0
|
avatar_updated_at == 0
|
||||||
? ""
|
? ""
|
||||||
: `${BASE_URL}/resource/avatar?uid=${data.user.uid}&t=${avatar_updated_at}`;
|
: `${BASE_URL}/resource/avatar?uid=${data.user.uid}&t=${avatar_updated_at}`;
|
||||||
return data;
|
return data;
|
||||||
},
|
},
|
||||||
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
||||||
try {
|
try {
|
||||||
const { data } = await queryFulfilled;
|
const { data } = await queryFulfilled;
|
||||||
if (data) {
|
if (data) {
|
||||||
console.log("login resp", data);
|
console.log("login resp", data);
|
||||||
dispatch(setAuthData(data));
|
dispatch(setAuthData(data));
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
console.log("login error");
|
console.log("login error");
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}),
|
|
||||||
// 更新token
|
|
||||||
renew: builder.mutation({
|
|
||||||
query: ({ token, refreshToken }) => ({
|
|
||||||
url: "/token/renew",
|
|
||||||
method: "POST",
|
|
||||||
body: {
|
|
||||||
token,
|
|
||||||
refresh_token: refreshToken,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
|
||||||
try {
|
|
||||||
const { data } = await queryFulfilled;
|
|
||||||
dispatch(updateToken(data));
|
|
||||||
} catch {
|
|
||||||
dispatch(resetAuthData());
|
|
||||||
console.log("renew token error");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
// 更新 device token
|
|
||||||
updateDeviceToken: builder.mutation({
|
|
||||||
query: (device_token) => ({
|
|
||||||
url: "/token/device_token",
|
|
||||||
method: "PUT",
|
|
||||||
body: {
|
|
||||||
device_token,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
// 获取openid
|
|
||||||
getOpenid: builder.mutation({
|
|
||||||
query: ({ issuer, redirect_uri }) => ({
|
|
||||||
url: "/token/openid/authorize",
|
|
||||||
method: "POST",
|
|
||||||
body: {
|
|
||||||
issuer,
|
|
||||||
redirect_uri,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
|
|
||||||
checkInviteTokenValid: builder.mutation({
|
|
||||||
query: (token) => ({
|
|
||||||
url: "user/check_invite_magic_token",
|
|
||||||
method: "POST",
|
|
||||||
body: { magic_token: token },
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
updatePassword: builder.mutation({
|
|
||||||
query: ({ old_password, new_password }) => ({
|
|
||||||
url: "user/change_password",
|
|
||||||
method: "POST",
|
|
||||||
body: { old_password, new_password },
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
sendMagicLink: builder.mutation({
|
|
||||||
query: (email) => ({
|
|
||||||
url: "token/send_magic_link",
|
|
||||||
method: "POST",
|
|
||||||
body: { email },
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
getMetamaskNonce: builder.query({
|
|
||||||
query: (address) => ({
|
|
||||||
url: `/token/metamask/nonce?public_address=${address}`,
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
getCredentials: builder.query({
|
|
||||||
query: () => ({
|
|
||||||
url: `/token/credentials`,
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
logout: builder.query({
|
|
||||||
query: () => ({ url: `token/logout` }),
|
|
||||||
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
|
||||||
try {
|
|
||||||
await queryFulfilled;
|
|
||||||
dispatch(resetAuthData());
|
|
||||||
} catch {
|
|
||||||
console.log("logout error");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
getInitialized: builder.query({
|
|
||||||
query: () => ({
|
|
||||||
url: `/admin/system/initialized`,
|
|
||||||
}),
|
|
||||||
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
|
||||||
try {
|
|
||||||
const { data: isInitized } = await queryFulfilled;
|
|
||||||
dispatch(updateInitilize(isInitized));
|
|
||||||
} catch {
|
|
||||||
console.log("api initialized error");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
}),
|
}),
|
||||||
|
// 更新token
|
||||||
|
renew: builder.mutation({
|
||||||
|
query: ({ token, refreshToken }) => ({
|
||||||
|
url: "/token/renew",
|
||||||
|
method: "POST",
|
||||||
|
body: {
|
||||||
|
token,
|
||||||
|
refresh_token: refreshToken
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
||||||
|
try {
|
||||||
|
const { data } = await queryFulfilled;
|
||||||
|
dispatch(updateToken(data));
|
||||||
|
} catch {
|
||||||
|
dispatch(resetAuthData());
|
||||||
|
console.log("renew token error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
// 更新 device token
|
||||||
|
updateDeviceToken: builder.mutation({
|
||||||
|
query: (device_token) => ({
|
||||||
|
url: "/token/device_token",
|
||||||
|
method: "PUT",
|
||||||
|
body: {
|
||||||
|
device_token
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
// 获取openid
|
||||||
|
getOpenid: builder.mutation({
|
||||||
|
query: ({ issuer, redirect_uri }) => ({
|
||||||
|
url: "/token/openid/authorize",
|
||||||
|
method: "POST",
|
||||||
|
body: {
|
||||||
|
issuer,
|
||||||
|
redirect_uri
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
|
||||||
|
checkInviteTokenValid: builder.mutation({
|
||||||
|
query: (token) => ({
|
||||||
|
url: "user/check_invite_magic_token",
|
||||||
|
method: "POST",
|
||||||
|
body: { magic_token: token }
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
updatePassword: builder.mutation({
|
||||||
|
query: ({ old_password, new_password }) => ({
|
||||||
|
url: "user/change_password",
|
||||||
|
method: "POST",
|
||||||
|
body: { old_password, new_password }
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
sendMagicLink: builder.mutation({
|
||||||
|
query: (email) => ({
|
||||||
|
url: "token/send_magic_link",
|
||||||
|
method: "POST",
|
||||||
|
body: { email }
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
getMetamaskNonce: builder.query({
|
||||||
|
query: (address) => ({
|
||||||
|
url: `/token/metamask/nonce?public_address=${address}`
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
getCredentials: builder.query({
|
||||||
|
query: () => ({
|
||||||
|
url: `/token/credentials`
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
logout: builder.query({
|
||||||
|
query: () => ({ url: `token/logout` }),
|
||||||
|
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
||||||
|
try {
|
||||||
|
await queryFulfilled;
|
||||||
|
dispatch(resetAuthData());
|
||||||
|
} catch {
|
||||||
|
console.log("logout error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
getInitialized: builder.query({
|
||||||
|
query: () => ({
|
||||||
|
url: `/admin/system/initialized`
|
||||||
|
}),
|
||||||
|
async onQueryStarted(params, { dispatch, queryFulfilled }) {
|
||||||
|
try {
|
||||||
|
const { data: isInitialized } = await queryFulfilled;
|
||||||
|
dispatch(updateInitialized(isInitialized));
|
||||||
|
} catch {
|
||||||
|
console.log("api initialized error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
export const {
|
export const {
|
||||||
useGetInitializedQuery,
|
useGetInitializedQuery,
|
||||||
useSendMagicLinkMutation,
|
useSendMagicLinkMutation,
|
||||||
useGetCredentialsQuery,
|
useGetCredentialsQuery,
|
||||||
useUpdateDeviceTokenMutation,
|
useUpdateDeviceTokenMutation,
|
||||||
useGetOpenidMutation,
|
useGetOpenidMutation,
|
||||||
useRenewMutation,
|
useRenewMutation,
|
||||||
useLazyGetMetamaskNonceQuery,
|
useLazyGetMetamaskNonceQuery,
|
||||||
useLoginMutation,
|
useLoginMutation,
|
||||||
useLazyLogoutQuery,
|
useLazyLogoutQuery,
|
||||||
useCheckInviteTokenValidMutation,
|
useCheckInviteTokenValidMutation,
|
||||||
useUpdatePasswordMutation,
|
useUpdatePasswordMutation
|
||||||
} = authApi;
|
} = authApi;
|
||||||
|
|||||||
+69
-80
@@ -1,89 +1,78 @@
|
|||||||
import { createSlice } from "@reduxjs/toolkit";
|
import { createSlice } from "@reduxjs/toolkit";
|
||||||
import {
|
import { KEY_PWA_INSTALLED, KEY_REFRESH_TOKEN, KEY_TOKEN, KEY_UID, KEY_EXPIRE } from "../config";
|
||||||
KEY_PWA_INSTALLED,
|
|
||||||
KEY_REFRESH_TOKEN,
|
|
||||||
KEY_TOKEN,
|
|
||||||
KEY_UID,
|
|
||||||
KEY_EXPIRE,
|
|
||||||
} from "../config";
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
initialized: true,
|
initialized: true,
|
||||||
uid: null,
|
uid: null,
|
||||||
token: localStorage.getItem(KEY_TOKEN),
|
token: localStorage.getItem(KEY_TOKEN),
|
||||||
expireTime: localStorage.getItem(KEY_EXPIRE) || new Date().getTime(),
|
expireTime: localStorage.getItem(KEY_EXPIRE) || new Date().getTime(),
|
||||||
refreshToken: localStorage.getItem(KEY_REFRESH_TOKEN),
|
refreshToken: localStorage.getItem(KEY_REFRESH_TOKEN)
|
||||||
};
|
};
|
||||||
const emptyState = {
|
const emptyState = {
|
||||||
initialized: true,
|
initialized: true,
|
||||||
uid: null,
|
uid: null,
|
||||||
token: null,
|
token: null,
|
||||||
expireTime: new Date().getTime(),
|
expireTime: new Date().getTime(),
|
||||||
refreshToken: null,
|
refreshToken: null
|
||||||
};
|
};
|
||||||
const authDataSlice = createSlice({
|
const authDataSlice = createSlice({
|
||||||
name: "authData",
|
name: "authData",
|
||||||
initialState,
|
initialState,
|
||||||
reducers: {
|
reducers: {
|
||||||
setAuthData(state, action) {
|
setAuthData(state, action) {
|
||||||
const {
|
const {
|
||||||
initialized = true,
|
initialized = true,
|
||||||
user: { uid },
|
user: { uid },
|
||||||
token,
|
token,
|
||||||
refresh_token,
|
refresh_token,
|
||||||
expired_in = 0,
|
expired_in = 0
|
||||||
} = action.payload;
|
} = action.payload;
|
||||||
state.initialized = initialized;
|
state.initialized = initialized;
|
||||||
state.uid = uid;
|
state.uid = uid;
|
||||||
state.token = token;
|
state.token = token;
|
||||||
state.refreshToken = refresh_token;
|
state.refreshToken = refresh_token;
|
||||||
// 当前时间往后推expire时长
|
// 当前时间往后推expire时长
|
||||||
console.log("expire", expired_in);
|
console.log("expire", expired_in);
|
||||||
const expireTime = new Date().getTime() + Number(expired_in) * 1000;
|
const expireTime = new Date().getTime() + Number(expired_in) * 1000;
|
||||||
state.expireTime = expireTime;
|
state.expireTime = expireTime;
|
||||||
// set local data
|
// set local data
|
||||||
localStorage.setItem(KEY_EXPIRE, expireTime);
|
localStorage.setItem(KEY_EXPIRE, expireTime);
|
||||||
localStorage.setItem(KEY_TOKEN, token);
|
localStorage.setItem(KEY_TOKEN, token);
|
||||||
localStorage.setItem(KEY_REFRESH_TOKEN, refresh_token);
|
localStorage.setItem(KEY_REFRESH_TOKEN, refresh_token);
|
||||||
localStorage.setItem(KEY_UID, uid);
|
localStorage.setItem(KEY_UID, uid);
|
||||||
},
|
|
||||||
resetAuthData() {
|
|
||||||
console.log("clear auth data");
|
|
||||||
// remove local data
|
|
||||||
localStorage.removeItem(KEY_EXPIRE);
|
|
||||||
localStorage.removeItem(KEY_TOKEN);
|
|
||||||
localStorage.removeItem(KEY_REFRESH_TOKEN);
|
|
||||||
localStorage.removeItem(KEY_UID);
|
|
||||||
localStorage.removeItem(KEY_PWA_INSTALLED);
|
|
||||||
|
|
||||||
return emptyState;
|
|
||||||
},
|
|
||||||
setUid(state, action) {
|
|
||||||
const uid = action.payload;
|
|
||||||
state.uid = uid;
|
|
||||||
console.log("set uid orginal");
|
|
||||||
},
|
|
||||||
updateInitilize(state, action) {
|
|
||||||
const isInitized = action.payload;
|
|
||||||
state.initialized = isInitized;
|
|
||||||
},
|
|
||||||
updateToken(state, action) {
|
|
||||||
const { token, refresh_token, expired_in } = action.payload;
|
|
||||||
console.log("refresh token");
|
|
||||||
state.token = token;
|
|
||||||
const et = new Date().getTime() + Number(expired_in) * 1000;
|
|
||||||
state.expireTime = et;
|
|
||||||
state.refreshToken = refresh_token;
|
|
||||||
localStorage.setItem(KEY_EXPIRE, et);
|
|
||||||
localStorage.setItem(KEY_TOKEN, token);
|
|
||||||
localStorage.setItem(KEY_REFRESH_TOKEN, refresh_token);
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
|
resetAuthData() {
|
||||||
|
console.log("clear auth data");
|
||||||
|
// remove local data
|
||||||
|
localStorage.removeItem(KEY_EXPIRE);
|
||||||
|
localStorage.removeItem(KEY_TOKEN);
|
||||||
|
localStorage.removeItem(KEY_REFRESH_TOKEN);
|
||||||
|
localStorage.removeItem(KEY_UID);
|
||||||
|
localStorage.removeItem(KEY_PWA_INSTALLED);
|
||||||
|
|
||||||
|
return emptyState;
|
||||||
|
},
|
||||||
|
setUid(state, action) {
|
||||||
|
const uid = action.payload;
|
||||||
|
state.uid = uid;
|
||||||
|
console.log("set uid orginal");
|
||||||
|
},
|
||||||
|
updateInitialized(state, action) {
|
||||||
|
const isInitialized = action.payload;
|
||||||
|
state.initialized = isInitialized;
|
||||||
|
},
|
||||||
|
updateToken(state, action) {
|
||||||
|
const { token, refresh_token, expired_in } = action.payload;
|
||||||
|
console.log("refresh token");
|
||||||
|
state.token = token;
|
||||||
|
const et = new Date().getTime() + Number(expired_in) * 1000;
|
||||||
|
state.expireTime = et;
|
||||||
|
state.refreshToken = refresh_token;
|
||||||
|
localStorage.setItem(KEY_EXPIRE, et);
|
||||||
|
localStorage.setItem(KEY_TOKEN, token);
|
||||||
|
localStorage.setItem(KEY_REFRESH_TOKEN, refresh_token);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
export const {
|
export const { updateInitialized, setAuthData, resetAuthData, setUid, updateToken } =
|
||||||
updateInitilize,
|
authDataSlice.actions;
|
||||||
setAuthData,
|
|
||||||
resetAuthData,
|
|
||||||
setUid,
|
|
||||||
updateToken,
|
|
||||||
} = authDataSlice.actions;
|
|
||||||
export default authDataSlice.reducer;
|
export default authDataSlice.reducer;
|
||||||
|
|||||||
Reference in New Issue
Block a user