Skip to content

Commit 4cce9ec

Browse files
authored
Auto launch OIDC (#1285)
* refactor(frontend): simplify OIDC redirect URL retrieval in auth route - Extract OIDC redirect URL fetching into a separate function - Streamline the getOidcRedirectUrl action handler * feat(frontend): add auto OIDC launch option for authentication route - Introduce `autoOidcLaunch` search parameter with default value of true - Automatically redirect to OIDC login when local auth is disabled and OIDC is enabled - Enhance authentication route to support seamless OIDC authentication flow * docs: update OIDC authentication guide with auto launch option - Add documentation for `autoOidcLaunch` parameter - Clarify how to access authentication page when local auth is disabled - Improve explanation of OIDC authentication flow
1 parent 1a13bdd commit 4cce9ec

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

apps/frontend/app/routes/auth.tsx

+17-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
parseSearchQuery,
2424
processSubmission,
2525
startCase,
26+
zodBoolAsString,
2627
zodNumAsString,
2728
} from "@ryot/ts-utils";
2829
import { IconAt } from "@tabler/icons-react";
@@ -51,12 +52,18 @@ import {
5152
import type { Route } from "./+types/auth";
5253

5354
const searchParamsSchema = z.object({
55+
autoOidcLaunch: zodBoolAsString.default("true"),
5456
intent: z.enum(["login", "register"]).optional(),
5557
});
5658

5759
export type SearchParams = z.infer<typeof searchParamsSchema> &
5860
Record<string, string>;
5961

62+
const getOidcRedirectUrl = () =>
63+
serverGqlService
64+
.request(GetOidcRedirectUrlDocument)
65+
.then(({ getOidcRedirectUrl }) => getOidcRedirectUrl);
66+
6067
export const loader = async ({ request }: Route.LoaderArgs) => {
6168
const query = parseSearchQuery(request, searchParamsSchema);
6269
const isAuthenticated = !!getAuthorizationCookie(request);
@@ -75,6 +82,14 @@ export const loader = async ({ request }: Route.LoaderArgs) => {
7582
);
7683
}
7784
const [coreDetails] = await Promise.all([getCoreDetails()]);
85+
if (
86+
coreDetails.oidcEnabled &&
87+
coreDetails.localAuthDisabled &&
88+
query.autoOidcLaunch === true
89+
) {
90+
const url = await getOidcRedirectUrl();
91+
return redirect(url);
92+
}
7893
return {
7994
intent: query.intent || "login",
8095
oidcEnabled: coreDetails.oidcEnabled,
@@ -173,10 +188,8 @@ export const action = async ({ request }: Route.ActionArgs) => {
173188
});
174189
})
175190
.with("getOidcRedirectUrl", async () => {
176-
const { getOidcRedirectUrl } = await serverGqlService.request(
177-
GetOidcRedirectUrlDocument,
178-
);
179-
return redirect(getOidcRedirectUrl);
191+
const url = await getOidcRedirectUrl();
192+
return redirect(url);
180193
})
181194
.run();
182195
};

docs/content/guides/authentication.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ Once these are set, restart your Ryot instance and you should be able to see the
2525
"Continue with OpenID Connect" on the authentication pages. New users will have their
2626
username set to their email address. This can be changed later in the profile settings.
2727

28+
You can set `USERS_DISABLE_LOCAL_AUTH=true` to disable local authentication and only allow
29+
users to authenticate using OIDC. When OIDC is enabled and local authentication is
30+
disabled, users will be redirected to the OIDC provider when they visit the auth page. To
31+
see the authentication page anyway, you can visit
32+
`<FRONTEND_URL>/auth?autoOidcLaunch=false`.
33+
2834
!!! warning
2935

3036
A user can either have a username/password or it can use your OIDC provider to
3137
authenticate but not both.
3238

33-
You can set `USERS_DISABLE_LOCAL_AUTH=true` to disable local authentication and only allow
34-
users to authenticate using OIDC.
35-
3639
### Converting a local user to an OIDC user
3740

3841
- Setup OpenID on your instance using the the above guide.

0 commit comments

Comments
 (0)