mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 20:49:16 +00:00
preparing login screen
This commit is contained in:
parent
6c61854593
commit
c01a6e6e8f
@ -26,7 +26,7 @@
|
|||||||
-->
|
-->
|
||||||
<title>Databag</title>
|
<title>Databag</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body style="background-color:#8fbea7;">
|
||||||
<noscript>You need to enable JavaScript to run this app.</noscript>
|
<noscript>You need to enable JavaScript to run this app.</noscript>
|
||||||
<div id="root"></div>
|
<div id="root"></div>
|
||||||
<!--
|
<!--
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
import login from './login.png';
|
|
||||||
|
import 'antd/dist/antd.min.css';
|
||||||
|
import { HashRouter as Router, Routes, Route } from "react-router-dom";
|
||||||
|
|
||||||
import { AppContextProvider } from 'context/AppContext';
|
import { AppContextProvider } from 'context/AppContext';
|
||||||
import { AccountContextProvider } from 'context/AccountContext';
|
import { AccountContextProvider } from 'context/AccountContext';
|
||||||
import { ProfileContextProvider } from 'context/ProfileContext';
|
import { ProfileContextProvider } from 'context/ProfileContext';
|
||||||
@ -9,7 +12,10 @@ import { ChannelContextProvider } from 'context/ChannelContext';
|
|||||||
import { StoreContextProvider } from 'context/StoreContext';
|
import { StoreContextProvider } from 'context/StoreContext';
|
||||||
import { UploadContextProvider } from 'context/UploadContext';
|
import { UploadContextProvider } from 'context/UploadContext';
|
||||||
import { ViewportContextProvider } from 'context/ViewportContext';
|
import { ViewportContextProvider } from 'context/ViewportContext';
|
||||||
import 'antd/dist/antd.min.css';
|
|
||||||
|
import { AppWrapper } from 'App.styled';
|
||||||
|
import { Root } from './root/Root';
|
||||||
|
import { Login } from './login/Login';
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
|
||||||
@ -24,11 +30,14 @@ function App() {
|
|||||||
<StoreContextProvider>
|
<StoreContextProvider>
|
||||||
<ViewportContextProvider>
|
<ViewportContextProvider>
|
||||||
<AppContextProvider>
|
<AppContextProvider>
|
||||||
<div style={{ position: 'absolute', width: '100vw', height: '100vh', backgroundColor: '#8fbea7' }}>
|
<AppWrapper>
|
||||||
<img src={login} alt="" style={{ position: 'absolute', width: '33%', bottom: 0, right: 0 }}/>
|
<Router>
|
||||||
</div>
|
<Routes>
|
||||||
<div style={{ position: 'absolute', width: '100vw', height: '100vh' }}>
|
<Route path="/" element={ <Root /> } />
|
||||||
</div>
|
<Route path="/login" element={ <Login /> } />
|
||||||
|
</Routes>
|
||||||
|
</Router>
|
||||||
|
</AppWrapper>
|
||||||
</AppContextProvider>
|
</AppContextProvider>
|
||||||
</ViewportContextProvider>
|
</ViewportContextProvider>
|
||||||
</StoreContextProvider>
|
</StoreContextProvider>
|
||||||
|
8
net/web/src/App.styled.js
Normal file
8
net/web/src/App.styled.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import styled from 'styled-components';
|
||||||
|
import Colors from 'constants/Colors';
|
||||||
|
|
||||||
|
export const AppWrapper = styled.div`
|
||||||
|
position: absolute;
|
||||||
|
width: 100vw;
|
||||||
|
height: 100vh;
|
||||||
|
`;
|
5
net/web/src/constants/Colors.js
Normal file
5
net/web/src/constants/Colors.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
const Colors = {
|
||||||
|
background: '#8fbea7',
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Colors;
|
@ -14,7 +14,7 @@ import { StoreContext } from './StoreContext';
|
|||||||
import { UploadContext } from './UploadContext';
|
import { UploadContext } from './UploadContext';
|
||||||
|
|
||||||
export function useAppContext() {
|
export function useAppContext() {
|
||||||
const [state, setState] = useState(null);
|
const [state, setState] = useState({});
|
||||||
const [appRevision, setAppRevision] = useState();
|
const [appRevision, setAppRevision] = useState();
|
||||||
|
|
||||||
const delay = useRef(2);
|
const delay = useRef(2);
|
||||||
@ -45,63 +45,13 @@ export function useAppContext() {
|
|||||||
setState({});
|
setState({});
|
||||||
}
|
}
|
||||||
|
|
||||||
const userActions = {
|
const actions = {
|
||||||
logout: () => {
|
logout: () => {
|
||||||
appLogout(updateState, clearWebsocket);
|
appLogout(updateState, clearWebsocket);
|
||||||
storeContext.actions.clear();
|
storeContext.actions.clear();
|
||||||
uploadContext.actions.clear();
|
uploadContext.actions.clear();
|
||||||
resetData();
|
resetData();
|
||||||
},
|
},
|
||||||
}
|
|
||||||
|
|
||||||
const adminActions = {
|
|
||||||
logout: () => {
|
|
||||||
appLogout(updateState, clearWebsocket);
|
|
||||||
resetData();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const appCreate = async (username, password, token) => {
|
|
||||||
await addAccount(username, password, token);
|
|
||||||
let access = await setLogin(username, password)
|
|
||||||
updateState({ token: access.appToken, access: 'user' });
|
|
||||||
storeContext.actions.setValue('login:timestamp', access.created);
|
|
||||||
setWebsocket(access.appToken)
|
|
||||||
localStorage.setItem("session", JSON.stringify({
|
|
||||||
token: access.appToken,
|
|
||||||
access: 'user',
|
|
||||||
timestamp: access.created,
|
|
||||||
}));
|
|
||||||
return access.created;
|
|
||||||
}
|
|
||||||
|
|
||||||
const appLogin = async (username, password) => {
|
|
||||||
let access = await setLogin(username, password)
|
|
||||||
updateState({ token: access.appToken, access: 'user' });
|
|
||||||
storeContext.actions.setValue('login:timestamp', access.created);
|
|
||||||
setWebsocket(access.appToken)
|
|
||||||
localStorage.setItem("session", JSON.stringify({
|
|
||||||
token: access.appToken,
|
|
||||||
access: 'user',
|
|
||||||
timestamp: access.created,
|
|
||||||
}));
|
|
||||||
return access.created;
|
|
||||||
}
|
|
||||||
|
|
||||||
const appAccess = async (token) => {
|
|
||||||
let access = await setAccountAccess(token)
|
|
||||||
updateState({ token: access, access: 'user' });
|
|
||||||
setWebsocket(access)
|
|
||||||
localStorage.setItem("session", JSON.stringify({ token: access, access: 'user' }));
|
|
||||||
}
|
|
||||||
|
|
||||||
function appLogout(updateState) {
|
|
||||||
updateState({ token: null, access: null });
|
|
||||||
clearWebsocket()
|
|
||||||
localStorage.removeItem("session");
|
|
||||||
}
|
|
||||||
|
|
||||||
const accessActions = {
|
|
||||||
access: async (token) => {
|
access: async (token) => {
|
||||||
await appAccess(token, updateState, setWebsocket)
|
await appAccess(token, updateState, setWebsocket)
|
||||||
},
|
},
|
||||||
@ -115,6 +65,44 @@ export function useAppContext() {
|
|||||||
available: getAvailable,
|
available: getAvailable,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const appCreate = async (username, password, token) => {
|
||||||
|
await addAccount(username, password, token);
|
||||||
|
let access = await setLogin(username, password)
|
||||||
|
updateState({ access: access.appToken });
|
||||||
|
storeContext.actions.setValue('login:timestamp', access.created);
|
||||||
|
setWebsocket(access.appToken)
|
||||||
|
localStorage.setItem("session", JSON.stringify({
|
||||||
|
token: access.appToken,
|
||||||
|
timestamp: access.created,
|
||||||
|
}));
|
||||||
|
return access.created;
|
||||||
|
}
|
||||||
|
|
||||||
|
const appLogin = async (username, password) => {
|
||||||
|
let access = await setLogin(username, password)
|
||||||
|
updateState({ access: access.appToken });
|
||||||
|
storeContext.actions.setValue('login:timestamp', access.created);
|
||||||
|
setWebsocket(access.appToken)
|
||||||
|
localStorage.setItem("session", JSON.stringify({
|
||||||
|
token: access.appToken,
|
||||||
|
timestamp: access.created,
|
||||||
|
}));
|
||||||
|
return access.created;
|
||||||
|
}
|
||||||
|
|
||||||
|
const appAccess = async (token) => {
|
||||||
|
let access = await setAccountAccess(token)
|
||||||
|
updateState({ access });
|
||||||
|
setWebsocket(access)
|
||||||
|
localStorage.setItem("session", JSON.stringify({ token: access }));
|
||||||
|
}
|
||||||
|
|
||||||
|
const appLogout = () => {
|
||||||
|
updateState({ access: null });
|
||||||
|
clearWebsocket()
|
||||||
|
localStorage.removeItem("session");
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (appRevision) {
|
if (appRevision) {
|
||||||
accountContext.actions.setRevision(appRevision.account);
|
accountContext.actions.setRevision(appRevision.account);
|
||||||
@ -156,7 +144,7 @@ export function useAppContext() {
|
|||||||
ws.current.onopen = () => {}
|
ws.current.onopen = () => {}
|
||||||
ws.current.onerror = () => {}
|
ws.current.onerror = () => {}
|
||||||
setWebsocket(token);
|
setWebsocket(token);
|
||||||
if (delay.current < 60) {
|
if (delay.current < 15) {
|
||||||
delay.current += 1;
|
delay.current += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -182,10 +170,8 @@ export function useAppContext() {
|
|||||||
if (storage != null) {
|
if (storage != null) {
|
||||||
try {
|
try {
|
||||||
const session = JSON.parse(storage)
|
const session = JSON.parse(storage)
|
||||||
if (session?.access === 'admin') {
|
if (session?.token) {
|
||||||
setState({ token: session.token, access: session.access })
|
setState({ token: session.token })
|
||||||
} else if (session?.access === 'user') {
|
|
||||||
setState({ token: session.token, access: session.access })
|
|
||||||
setWebsocket(session.token);
|
setWebsocket(session.token);
|
||||||
} else {
|
} else {
|
||||||
setState({})
|
setState({})
|
||||||
@ -203,13 +189,7 @@ export function useAppContext() {
|
|||||||
if (state == null) {
|
if (state == null) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
if (state.access === 'user') {
|
return { state, actions }
|
||||||
return { state, actions: userActions }
|
|
||||||
}
|
|
||||||
if (state.access === 'admin') {
|
|
||||||
return { state, actions: adminActions }
|
|
||||||
}
|
|
||||||
return { state, actions: accessActions }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,18 +3,18 @@ import { useEffect, useState } from 'react';
|
|||||||
export function useViewportContext() {
|
export function useViewportContext() {
|
||||||
|
|
||||||
const [state, setState] = useState({ });
|
const [state, setState] = useState({ });
|
||||||
|
const SMALL_MEDIUM = 600;
|
||||||
|
const MEDIUM_LARGE = 1600
|
||||||
|
|
||||||
const updateState = (value) => {
|
const updateState = (value) => {
|
||||||
setState((s) => ({ ...s, ...value }));
|
setState((s) => ({ ...s, ...value }));
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleResize = () => {
|
const handleResize = () => {
|
||||||
console.log(window.innerWidth);
|
if (window.innerWidth < SMALL_MEDIUM) {
|
||||||
console.log(window.innerHeight);
|
|
||||||
if (window.innerWidth < 600) {
|
|
||||||
updateState({ display: 'small' });
|
updateState({ display: 'small' });
|
||||||
}
|
}
|
||||||
else if (window.innerWidth < 1600) {
|
else if (window.innerWidth < MEDIUM_LARGE) {
|
||||||
updateState({ display: 'medium' });
|
updateState({ display: 'medium' });
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -28,7 +28,7 @@ export function useViewportContext() {
|
|||||||
return () => window.removeEventListener('resize', handleResize)
|
return () => window.removeEventListener('resize', handleResize)
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return state
|
return { state, actions: {} }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 256 KiB |
35
net/web/src/login/Login.jsx
Normal file
35
net/web/src/login/Login.jsx
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import React, { useContext, useEffect } from 'react';
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { AppContext } from 'context/AppContext';
|
||||||
|
import { ViewportContext } from 'context/ViewportContext';
|
||||||
|
import { LoginWrapper } from './Login.styled';
|
||||||
|
|
||||||
|
export function Login() {
|
||||||
|
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const app = useContext(AppContext);
|
||||||
|
const viewport = useContext(ViewportContext);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (app.state) {
|
||||||
|
if (app.state.access) {
|
||||||
|
navigate('/user');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [app]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<LoginWrapper>
|
||||||
|
{ viewport.state.display === 'large' && (
|
||||||
|
<div>LARGE</div>
|
||||||
|
)}
|
||||||
|
{ viewport.state.display === 'medium' && (
|
||||||
|
<div>MEDIUM</div>
|
||||||
|
)}
|
||||||
|
{ viewport.state.display === 'small' && (
|
||||||
|
<div>SMALL</div>
|
||||||
|
)}
|
||||||
|
</LoginWrapper>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
5
net/web/src/login/Login.styled.js
Normal file
5
net/web/src/login/Login.styled.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import styled from 'styled-components';
|
||||||
|
import Colors from 'constants/Colors';
|
||||||
|
|
||||||
|
export const LoginWrapper = styled.div`
|
||||||
|
`;
|
23
net/web/src/root/Root.jsx
Normal file
23
net/web/src/root/Root.jsx
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import React, { useContext, useEffect } from 'react';
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { AppContext } from 'context/AppContext';
|
||||||
|
|
||||||
|
export function Root() {
|
||||||
|
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const app = useContext(AppContext);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (app.state) {
|
||||||
|
if (app.state.access) {
|
||||||
|
navigate('/user');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
navigate('/login');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [app]);
|
||||||
|
|
||||||
|
return <></>
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user