mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-07-19 08:21:20 +00:00
fix: make login_for optional
This commit is contained in:
@@ -32,8 +32,8 @@ import { useTranslation } from "react-i18next";
|
||||
import { useLocation } from "react-router";
|
||||
import { useRef } from "react";
|
||||
import {
|
||||
searchParamsFromObject,
|
||||
useScreenParams,
|
||||
recompileScreenParams,
|
||||
} from "@/lib/hooks/screen-params";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import axios from "axios";
|
||||
@@ -70,7 +70,13 @@ export const QuickActions = () => {
|
||||
const redirectTimer = useRef<number | null>(null);
|
||||
const searchParams = new URLSearchParams(search);
|
||||
const screenParams = useScreenParams(searchParams);
|
||||
const compiledParams = recompileScreenParams(screenParams);
|
||||
const compiledParams = (() => {
|
||||
const params = searchParamsFromObject(screenParams).toString();
|
||||
if (params.length > 0) {
|
||||
return `?${params}`;
|
||||
}
|
||||
return "";
|
||||
})();
|
||||
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
type UseLoginForProps = {
|
||||
login_for?: "oidc" | "app";
|
||||
compiledParams: string;
|
||||
params: URLSearchParams;
|
||||
};
|
||||
|
||||
export const useLoginFor = (props: UseLoginForProps): string => {
|
||||
const { login_for, compiledParams } = props;
|
||||
const { login_for, params } = props;
|
||||
const compiledParams = params.toString() ? "?" + params.toString() : "";
|
||||
|
||||
switch (login_for) {
|
||||
case "oidc":
|
||||
@@ -12,6 +13,9 @@ export const useLoginFor = (props: UseLoginForProps): string => {
|
||||
case "app":
|
||||
return "/continue" + compiledParams;
|
||||
default:
|
||||
if (params.get("redirect_uri")) {
|
||||
return "/continue" + compiledParams
|
||||
}
|
||||
return "/logout";
|
||||
}
|
||||
};
|
||||
|
||||
@@ -27,16 +27,10 @@ export function useScreenParams(params: URLSearchParams): ScreenParams {
|
||||
return parsed.data;
|
||||
}
|
||||
|
||||
export function recompileScreenParams(params: ScreenParams): string {
|
||||
const p = new URLSearchParams(
|
||||
export function searchParamsFromObject(obj: Object): URLSearchParams {
|
||||
return new URLSearchParams(
|
||||
Object.fromEntries(
|
||||
Object.entries(params).filter(([, v]) => v !== undefined),
|
||||
Object.entries(obj).filter(([, v]) => v !== undefined),
|
||||
) as Record<string, string>,
|
||||
).toString();
|
||||
|
||||
if (p.length > 0) {
|
||||
return "?" + p;
|
||||
}
|
||||
|
||||
return "";
|
||||
);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ import {
|
||||
TooltipTrigger,
|
||||
} from "@/components/ui/tooltip";
|
||||
import {
|
||||
recompileScreenParams,
|
||||
searchParamsFromObject,
|
||||
useScreenParams,
|
||||
} from "@/lib/hooks/screen-params";
|
||||
import { useEffect } from "react";
|
||||
@@ -89,7 +89,13 @@ export const AuthorizePage = () => {
|
||||
const searchParams = new URLSearchParams(search);
|
||||
const screenParams = useScreenParams(searchParams);
|
||||
const isOidc = screenParams.login_for === "oidc";
|
||||
const compiledParams = recompileScreenParams(screenParams);
|
||||
const compiledParams = (() => {
|
||||
const params = searchParamsFromObject(screenParams).toString();
|
||||
if (params.length > 0) {
|
||||
return `?${params}`;
|
||||
}
|
||||
return "";
|
||||
})();
|
||||
|
||||
// TODO: maybe a better way to do this
|
||||
const shouldAutoAuthorize =
|
||||
|
||||
@@ -13,7 +13,7 @@ import { Navigate, useLocation, useNavigate } from "react-router";
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { useRedirectUri } from "@/lib/hooks/redirect-uri";
|
||||
import {
|
||||
recompileScreenParams,
|
||||
searchParamsFromObject,
|
||||
useScreenParams,
|
||||
} from "@/lib/hooks/screen-params";
|
||||
|
||||
@@ -32,7 +32,13 @@ export const ContinuePage = () => {
|
||||
const screenParams = useScreenParams(searchParams);
|
||||
const redirectUri = screenParams.redirect_uri;
|
||||
const isAppLogin = screenParams.login_for === "app";
|
||||
const recompiledParams = recompileScreenParams(screenParams);
|
||||
const compiledParams = (() => {
|
||||
const params = searchParamsFromObject(screenParams).toString();
|
||||
if (params.length > 0) {
|
||||
return `?${params}`;
|
||||
}
|
||||
return "";
|
||||
})();
|
||||
|
||||
const { url, valid, trusted, allowedProto, httpsDowngrade } = useRedirectUri(
|
||||
redirectUri,
|
||||
@@ -89,7 +95,7 @@ export const ContinuePage = () => {
|
||||
}, [shouldAutoRedirect, redirectToTarget]);
|
||||
|
||||
if (!auth.authenticated) {
|
||||
return <Navigate to={`/login${recompiledParams}`} replace />;
|
||||
return <Navigate to={`/login${compiledParams}`} replace />;
|
||||
}
|
||||
|
||||
if (!hasValidRedirect || !isAppLogin) {
|
||||
|
||||
@@ -12,8 +12,8 @@ import { useTranslation } from "react-i18next";
|
||||
import Markdown from "react-markdown";
|
||||
import { useLocation } from "react-router";
|
||||
import {
|
||||
recompileScreenParams,
|
||||
useScreenParams,
|
||||
searchParamsFromObject,
|
||||
useScreenParams,
|
||||
} from "@/lib/hooks/screen-params";
|
||||
|
||||
export const ForgotPasswordPage = () => {
|
||||
@@ -22,7 +22,13 @@ export const ForgotPasswordPage = () => {
|
||||
const { search } = useLocation();
|
||||
const searchParams = new URLSearchParams(search);
|
||||
const screenParams = useScreenParams(searchParams);
|
||||
const compiledParams = recompileScreenParams(screenParams);
|
||||
const compiledParams = (() => {
|
||||
const params = searchParamsFromObject(screenParams).toString();
|
||||
if (params.length > 0) {
|
||||
return `?${params}`;
|
||||
}
|
||||
return "";
|
||||
})();
|
||||
|
||||
return (
|
||||
<Card>
|
||||
|
||||
@@ -26,7 +26,7 @@ import { useTranslation } from "react-i18next";
|
||||
import { Navigate, useLocation } from "react-router";
|
||||
import { toast } from "sonner";
|
||||
import {
|
||||
recompileScreenParams,
|
||||
searchParamsFromObject,
|
||||
useScreenParams,
|
||||
} from "@/lib/hooks/screen-params";
|
||||
import { useLoginFor } from "@/lib/hooks/login-for";
|
||||
@@ -63,13 +63,16 @@ export const LoginPage = () => {
|
||||
|
||||
const searchParams = new URLSearchParams(search);
|
||||
const screenParams = useScreenParams(searchParams);
|
||||
const compiledParams = recompileScreenParams({
|
||||
...screenParams,
|
||||
oidc_prompt: undefined,
|
||||
});
|
||||
const compiledParams = (() => {
|
||||
const params = searchParamsFromObject(screenParams).toString();
|
||||
if (params.length > 0) {
|
||||
return `?${params}`;
|
||||
}
|
||||
return "";
|
||||
})();
|
||||
const loginForUrl = useLoginFor({
|
||||
login_for: screenParams.login_for,
|
||||
compiledParams,
|
||||
params: searchParamsFromObject({ ...screenParams, oidc_prompt: undefined}),
|
||||
});
|
||||
|
||||
const [isOauthAutoRedirect, setIsOauthAutoRedirect] = useState(
|
||||
|
||||
@@ -17,8 +17,8 @@ import { type UseMutationResult } from "@tanstack/react-query";
|
||||
import { type AxiosResponse } from "axios";
|
||||
import { useLocation } from "react-router";
|
||||
import {
|
||||
searchParamsFromObject,
|
||||
useScreenParams,
|
||||
recompileScreenParams,
|
||||
} from "@/lib/hooks/screen-params";
|
||||
|
||||
export const LogoutPage = () => {
|
||||
@@ -29,7 +29,13 @@ export const LogoutPage = () => {
|
||||
const redirectTimer = useRef<number | null>(null);
|
||||
const searchParams = new URLSearchParams(search);
|
||||
const screenParams = useScreenParams(searchParams);
|
||||
const compiledParams = recompileScreenParams(screenParams);
|
||||
const compiledParams = (() => {
|
||||
const params = searchParamsFromObject(screenParams).toString();
|
||||
if (params.length > 0) {
|
||||
return `?${params}`;
|
||||
}
|
||||
return "";
|
||||
})();
|
||||
|
||||
const logoutMutation = useMutation({
|
||||
mutationFn: () => axios.post("/api/user/logout"),
|
||||
|
||||
@@ -17,7 +17,7 @@ import { useTranslation } from "react-i18next";
|
||||
import { Navigate, useLocation } from "react-router";
|
||||
import { toast } from "sonner";
|
||||
import {
|
||||
recompileScreenParams,
|
||||
searchParamsFromObject,
|
||||
useScreenParams,
|
||||
} from "@/lib/hooks/screen-params";
|
||||
import { useLoginFor } from "@/lib/hooks/login-for";
|
||||
@@ -32,10 +32,16 @@ export const TotpPage = () => {
|
||||
|
||||
const searchParams = new URLSearchParams(search);
|
||||
const screenParams = useScreenParams(searchParams);
|
||||
const compiledParams = recompileScreenParams(screenParams);
|
||||
const compiledParams = (() => {
|
||||
const params = searchParamsFromObject(screenParams).toString();
|
||||
if (params.length > 0) {
|
||||
return `?${params}`;
|
||||
}
|
||||
return "";
|
||||
})();
|
||||
const loginForUrl = useLoginFor({
|
||||
login_for: screenParams.login_for,
|
||||
compiledParams,
|
||||
params: searchParamsFromObject(screenParams),
|
||||
});
|
||||
|
||||
const totpMutation = useMutation({
|
||||
|
||||
Reference in New Issue
Block a user