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