Redirect after login
This commit is contained in:
parent
6d39f8bae0
commit
0bd8800402
3 changed files with 69 additions and 20 deletions
33
src/App.jsx
33
src/App.jsx
|
|
@ -20,6 +20,7 @@ import {
|
||||||
Switch,
|
Switch,
|
||||||
Route,
|
Route,
|
||||||
Redirect,
|
Redirect,
|
||||||
|
useLocation,
|
||||||
} from "react-router-dom";
|
} from "react-router-dom";
|
||||||
import { useConferenceCallManager } from "./ConferenceCallManagerHooks";
|
import { useConferenceCallManager } from "./ConferenceCallManagerHooks";
|
||||||
import { JoinOrCreateRoom } from "./JoinOrCreateRoom";
|
import { JoinOrCreateRoom } from "./JoinOrCreateRoom";
|
||||||
|
|
@ -48,16 +49,34 @@ export default function App() {
|
||||||
<LoginOrRegister onRegister={register} onLogin={login} />
|
<LoginOrRegister onRegister={register} onLogin={login} />
|
||||||
)}
|
)}
|
||||||
</Route>
|
</Route>
|
||||||
<Route path="/room/:roomId">
|
<AuthenticatedRoute
|
||||||
{!authenticated ? (
|
authenticated={authenticated}
|
||||||
<Redirect to="/" />
|
path="/room/:roomId"
|
||||||
) : (
|
>
|
||||||
<Room manager={manager} />
|
<Room manager={manager} />
|
||||||
)}
|
</AuthenticatedRoute>
|
||||||
</Route>
|
|
||||||
</Switch>
|
</Switch>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</Router>
|
</Router>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function AuthenticatedRoute({ authenticated, children, ...rest }) {
|
||||||
|
const location = useLocation();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Route {...rest}>
|
||||||
|
{authenticated ? (
|
||||||
|
children
|
||||||
|
) : (
|
||||||
|
<Redirect
|
||||||
|
to={{
|
||||||
|
pathname: "/",
|
||||||
|
state: { from: location },
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</Route>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ export function useConferenceCallManager(homeserverUrl) {
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const login = useCallback(async (username, password) => {
|
const login = useCallback(async (username, password, cb) => {
|
||||||
setState((prevState) => ({
|
setState((prevState) => ({
|
||||||
...prevState,
|
...prevState,
|
||||||
authenticated: false,
|
authenticated: false,
|
||||||
|
|
@ -78,6 +78,10 @@ export function useConferenceCallManager(homeserverUrl) {
|
||||||
authenticated: true,
|
authenticated: true,
|
||||||
error: undefined,
|
error: undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (cb) {
|
||||||
|
cb();
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|
@ -91,7 +95,7 @@ export function useConferenceCallManager(homeserverUrl) {
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const register = useCallback(async (username, password) => {
|
const register = useCallback(async (username, password, cb) => {
|
||||||
setState((prevState) => ({
|
setState((prevState) => ({
|
||||||
...prevState,
|
...prevState,
|
||||||
authenticated: false,
|
authenticated: false,
|
||||||
|
|
@ -106,6 +110,10 @@ export function useConferenceCallManager(homeserverUrl) {
|
||||||
authenticated: true,
|
authenticated: true,
|
||||||
error: undefined,
|
error: undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (cb) {
|
||||||
|
cb();
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|
|
||||||
|
|
@ -15,25 +15,47 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React, { useCallback, useRef } from "react";
|
import React, { useCallback, useRef } from "react";
|
||||||
|
import { useHistory, useLocation } from "react-router-dom";
|
||||||
|
|
||||||
export function LoginOrRegister({ onRegister, onLogin }) {
|
export function LoginOrRegister({ onRegister, onLogin }) {
|
||||||
const registerUsernameRef = useRef();
|
const registerUsernameRef = useRef();
|
||||||
const registerPasswordRef = useRef();
|
const registerPasswordRef = useRef();
|
||||||
const loginUsernameRef = useRef();
|
const loginUsernameRef = useRef();
|
||||||
const loginPasswordRef = useRef();
|
const loginPasswordRef = useRef();
|
||||||
|
const history = useHistory();
|
||||||
|
const location = useLocation();
|
||||||
|
|
||||||
const onSubmitRegisterForm = useCallback((e) => {
|
const onSubmitRegisterForm = useCallback(
|
||||||
e.preventDefault();
|
(e) => {
|
||||||
onRegister(
|
e.preventDefault();
|
||||||
registerUsernameRef.current.value,
|
onRegister(
|
||||||
registerPasswordRef.current.value
|
registerUsernameRef.current.value,
|
||||||
);
|
registerPasswordRef.current.value,
|
||||||
});
|
() => {
|
||||||
|
if (location.state && location.state.from) {
|
||||||
|
history.replace(location.state.from);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
[onRegister, location, history]
|
||||||
|
);
|
||||||
|
|
||||||
const onSubmitLoginForm = useCallback((e) => {
|
const onSubmitLoginForm = useCallback(
|
||||||
e.preventDefault();
|
(e) => {
|
||||||
onLogin(loginUsernameRef.current.value, loginPasswordRef.current.value);
|
e.preventDefault();
|
||||||
});
|
onLogin(
|
||||||
|
loginUsernameRef.current.value,
|
||||||
|
loginPasswordRef.current.value,
|
||||||
|
() => {
|
||||||
|
if (location.state && location.state.from) {
|
||||||
|
history.replace(location.state.from);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
[onLogin, location, history]
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="page">
|
<div className="page">
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue