Skip to content

Commit

Permalink
concord-console2: add support for redirectTo to the login page (#1046)
Browse files Browse the repository at this point in the history
Also fixes the 'from' behavior.
  • Loading branch information
ibodrov authored Dec 4, 2024
1 parent eaf7e2e commit b44f1cf
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions console2/src/components/organisms/Login2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ const clearLastLoginType = () => {
localStorage.removeItem('lastLoginType');
};

const DEFAULT_DESTINATION = '/';
const DEFAULT_FROM_VALUE = '/';

const getDestination = (props: RouteComponentProps<{}>) => {
const getFrom = (props: RouteComponentProps<{}>): string => {
const location = props.location as any;

if (location && location.state && location.state.from && location.state.from.pathname) {
Expand All @@ -75,11 +75,19 @@ const getDestination = (props: RouteComponentProps<{}>) => {

const fromUrl = parseQueryString(props.location.search);

if (fromUrl && fromUrl.from) {
if (fromUrl && typeof fromUrl.from === 'string') {
return fromUrl.from;
}

return DEFAULT_DESTINATION;
return DEFAULT_FROM_VALUE;
};

const getRedirectTo = (props: RouteComponentProps<{}>): string | undefined => {
const qs = parseQueryString(props.location.search);
const redirectTo = qs ? qs.redirectTo : undefined;
if (typeof redirectTo === 'string') {
return redirectTo;
}
};

const Login = (props: RouteComponentProps<{}>) => {
Expand All @@ -90,7 +98,6 @@ const Login = (props: RouteComponentProps<{}>) => {
const [password, setPassword] = useState<string>('');
const [apiKey, setApiKey] = useState<string>('');
const [rememberMe, setRememberMe] = useState<boolean | undefined>();
const [destination] = useState(getDestination(props));
const { loggingIn, setLoggingIn, setUserInfo } = useContext(UserSessionContext);

const handleSubmit = useCallback(async () => {
Expand All @@ -106,7 +113,22 @@ const Login = (props: RouteComponentProps<{}>) => {

saveLastLoginType(nonEmpty(apiKey) ? 'apiKey' : 'username');

props.history.push(destination);
// with 'redirectTo' the user will be redirected to the specified href
// the values can be arbitrary endpoints
const redirectTo = getRedirectTo(props);

// the 'from' query parameter value will be pushed to history
// the values must be valid routes
// e.g. from=/org will be turned into http.../#/org
const from = getFrom(props);

if (redirectTo) {
setTimeout(() => {
window.location.href = redirectTo;
}, 100);
} else {
props.history.push(from);
}
} catch (e) {
let msg = e.message || 'Log in error';
if (e.status === 401) {
Expand All @@ -124,8 +146,7 @@ const Login = (props: RouteComponentProps<{}>) => {
apiKey,
setLoggingIn,
setUserInfo,
props.history,
destination
props,
]);

const onChangeLoginType = useCallback(() => {
Expand Down

0 comments on commit b44f1cf

Please sign in to comment.