feat: 登录界面
This commit is contained in:
45
src/commons/auth/auth.provider.tsx
Normal file
45
src/commons/auth/auth.provider.tsx
Normal file
@ -0,0 +1,45 @@
|
||||
import { createContext, useContext, useState } from "react";
|
||||
import { FC } from "react";
|
||||
import { Login } from "./login";
|
||||
|
||||
export interface AuthContext {
|
||||
accessToken: string | undefined;
|
||||
setAccessToken: (token: string) => void;
|
||||
setRefreshToken: (token: string) => void;
|
||||
refreshToken: string | undefined;
|
||||
login: (dto: any) => void;
|
||||
account?: any;
|
||||
setAccount: (dto: any) => void;
|
||||
}
|
||||
const Context = createContext({} as AuthContext);
|
||||
|
||||
export const useAuth = () => useContext(Context);
|
||||
|
||||
export const AuthProvider: FC = ({ children }) => {
|
||||
const [accessToken, setAccessToken] = useState<string>();
|
||||
const [refreshToken, setRefreshToken] = useState<string>();
|
||||
const [account, setAccount] = useState<any>();
|
||||
|
||||
const login = (dto: any) => {
|
||||
setAccessToken(dto.accessToken);
|
||||
setRefreshToken(dto.refreshToken);
|
||||
setAccount(dto.account);
|
||||
};
|
||||
|
||||
return (
|
||||
<Context.Provider
|
||||
value={{
|
||||
accessToken,
|
||||
setAccessToken,
|
||||
refreshToken,
|
||||
setRefreshToken,
|
||||
login,
|
||||
account,
|
||||
setAccount,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
{/* {accessToken ? null : <Login />} */}
|
||||
</Context.Provider>
|
||||
);
|
||||
};
|
51
src/commons/auth/login.tsx
Normal file
51
src/commons/auth/login.tsx
Normal file
@ -0,0 +1,51 @@
|
||||
import { makeStyles } from "@material-ui/core";
|
||||
import { FC, useEffect, useRef } from "react";
|
||||
import { useAuth } from "./auth.provider";
|
||||
const useStyles = makeStyles({
|
||||
iframe: {
|
||||
height: "300px",
|
||||
width: "500px",
|
||||
position: "absolute",
|
||||
top: "100px",
|
||||
left: "50%",
|
||||
transform: "translateX(-50%)",
|
||||
},
|
||||
});
|
||||
|
||||
export const Login: FC = () => {
|
||||
const iframeRef = useRef<HTMLIFrameElement>(null);
|
||||
const { login } = useAuth();
|
||||
useEffect(() => {
|
||||
const iframe = iframeRef.current;
|
||||
if (!iframe) {
|
||||
return;
|
||||
}
|
||||
let messagePort: MessagePort;
|
||||
const onLoad = (ev: MessageEvent) => {
|
||||
if (ev.data !== "init-channel") {
|
||||
return;
|
||||
}
|
||||
messagePort = ev.ports?.[0] as MessagePort;
|
||||
messagePort.onmessage = (ev: MessageEvent) => {
|
||||
if (ev.data?.type === "logged") {
|
||||
login(ev.data.payload);
|
||||
}
|
||||
};
|
||||
};
|
||||
window.addEventListener("message", onLoad);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener("message", onLoad);
|
||||
};
|
||||
}, [login]);
|
||||
|
||||
const classes = useStyles();
|
||||
return (
|
||||
<iframe
|
||||
ref={iframeRef}
|
||||
className={classes.iframe}
|
||||
title="Auth"
|
||||
src="http://user.localhost/auth/login"
|
||||
></iframe>
|
||||
);
|
||||
};
|
Reference in New Issue
Block a user