refactor: login api with redux update

This commit is contained in:
zerosoul
2022-06-06 17:14:24 +08:00
parent bfc3bb08d1
commit 3263900719
3 changed files with 33 additions and 23 deletions
+13 -2
View File
@@ -1,7 +1,7 @@
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 { updateToken, resetAuthData } from "../slices/auth.data"; import { setAuthData, updateToken, resetAuthData } 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);
@@ -33,6 +33,17 @@ export const authApi = createApi({
: `${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 }) {
try {
const { data } = await queryFulfilled;
if (data) {
console.log("login resp", data);
dispatch(setAuthData(data));
}
} catch {
console.log("login error");
}
},
}), }),
// 更新token // 更新token
renew: builder.mutation({ renew: builder.mutation({
@@ -50,7 +61,7 @@ export const authApi = createApi({
dispatch(updateToken(data)); dispatch(updateToken(data));
} catch { } catch {
dispatch(resetAuthData()); dispatch(resetAuthData());
console.log("remove channel error"); console.log("renew token error");
} }
}, },
}), }),
+6 -3
View File
@@ -96,19 +96,22 @@ export default function InvitePage() {
} else if (isError) { } else if (isError) {
console.log("register failed", error); console.log("register failed", error);
switch (error.status) { switch (error.status) {
case 400:
toast.error("Register Failed: please check inputs");
break;
case 412: case 412:
toast.error("register failed: invalid token or expired"); toast.error("Register Failed: invalid token or expired");
break; break;
case 409: { case 409: {
const tips = { const tips = {
email_conflict: "email conflict", email_conflict: "email conflict",
name_conflict: "name conflict", name_conflict: "name conflict",
}; };
toast.error(`register failed: ${tips[error.data?.reason]}`); toast.error(`Register Failed: ${tips[error.data?.reason]}`);
break; break;
} }
default: default:
toast.error("register failed"); toast.error("Register Failed");
break; break;
} }
} }
+14 -18
View File
@@ -1,7 +1,5 @@
/* eslint-disable no-undef */ /* eslint-disable no-undef */
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { useDispatch } from "react-redux";
import { useNavigate } from "react-router-dom";
import toast from "react-hot-toast"; import toast from "react-hot-toast";
import BASE_URL from "../../app/config"; import BASE_URL from "../../app/config";
// import web3 from "web3"; // import web3 from "web3";
@@ -17,21 +15,18 @@ import {
useGetLoginConfigQuery, useGetLoginConfigQuery,
useGetSMTPStatusQuery, useGetSMTPStatusQuery,
} from "../../app/services/server"; } from "../../app/services/server";
import { setAuthData } from "../../app/slices/auth.data";
import useGoogleAuthConfig from "../../common/hook/useGoogleAuthConfig"; import useGoogleAuthConfig from "../../common/hook/useGoogleAuthConfig";
import GithubLoginButton from "./GithubLoginButton"; import GithubLoginButton from "./GithubLoginButton";
import useGithubAuthConfig from "../../common/hook/useGithubAuthConfig"; import useGithubAuthConfig from "../../common/hook/useGithubAuthConfig";
export default function LoginPage() { export default function LoginPage() {
const { data: enableSMTP } = useGetSMTPStatusQuery(); const { data: enableSMTP } = useGetSMTPStatusQuery();
const [login, { data, isSuccess, isLoading, error }] = useLoginMutation(); const [login, { isSuccess, isLoading, error }] = useLoginMutation();
const { clientId } = useGoogleAuthConfig(); const { clientId } = useGoogleAuthConfig();
const { config: githubAuthConfig } = useGithubAuthConfig(); const { config: githubAuthConfig } = useGithubAuthConfig();
const { const {
data: loginConfig, data: loginConfig,
isSuccess: loginConfigSuccess, isSuccess: loginConfigSuccess,
} = useGetLoginConfigQuery(undefined, { pollingInterval: 10000 }); } = useGetLoginConfigQuery(undefined, { pollingInterval: 10000 });
const navigateTo = useNavigate();
const dispatch = useDispatch();
const [input, setInput] = useState({ const [input, setInput] = useState({
email: "", email: "",
password: "", password: "",
@@ -105,14 +100,11 @@ export default function LoginPage() {
} }
}, [error]); }, [error]);
useEffect(() => { useEffect(() => {
if (isSuccess && data) { if (isSuccess) {
// 更新本地认证信息
console.log("login data", data);
toast.success("Login Successfully"); toast.success("Login Successfully");
dispatch(setAuthData(data)); // navigateTo("/");
navigateTo("/");
} }
}, [isSuccess, data]); }, [isSuccess]);
const handleLogin = (evt) => { const handleLogin = (evt) => {
evt.preventDefault(); evt.preventDefault();
@@ -136,12 +128,19 @@ export default function LoginPage() {
if (!loginConfigSuccess) return null; if (!loginConfigSuccess) return null;
const { const {
magic_link, magic_link,
github: enableGithubLogin,
google: enableGoogleLogin, google: enableGoogleLogin,
metamask: enableMetamaskLogin, metamask: enableMetamaskLogin,
oidc, oidc = [],
} = loginConfig; } = loginConfig;
const enableMagicLink = enableSMTP && magic_link; const enableMagicLink = enableSMTP && magic_link;
const googleLogin = enableGoogleLogin && clientId; const googleLogin = enableGoogleLogin && clientId;
const hasDivider =
enableMagicLink ||
googleLogin ||
enableMetamaskLogin ||
oidc.length > 0 ||
enableGithubLogin;
return ( return (
<StyledWrapper> <StyledWrapper>
<div className="form"> <div className="form">
@@ -178,13 +177,10 @@ export default function LoginPage() {
{isLoading ? "Signing" : `Sign in`} {isLoading ? "Signing" : `Sign in`}
</Button> </Button>
</form> </form>
{(enableMagicLink || {hasDivider && <hr className="or" />}
googleLogin ||
enableMetamaskLogin ||
oidc.length > 0) && <hr className="or" />}
{enableMagicLink && <MagicLinkLogin />} {enableMagicLink && <MagicLinkLogin />}
{googleLogin && <GoogleLoginButton login={login} clientId={clientId} />} {googleLogin && <GoogleLoginButton login={login} clientId={clientId} />}
<GithubLoginButton config={githubAuthConfig} /> {enableGithubLogin && <GithubLoginButton config={githubAuthConfig} />}
{enableMetamaskLogin && <MetamaskLoginButton login={login} />} {enableMetamaskLogin && <MetamaskLoginButton login={login} />}
{oidc.length > 0 && <SolidLoginButton issuers={oidc} />} {oidc.length > 0 && <SolidLoginButton issuers={oidc} />}
</div> </div>