Skip to content

Commit

Permalink
fix(backend,frontend): register/login flow for oidc
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnisDa committed Jul 27, 2024
1 parent 3d0d161 commit 69d9fad
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 18 deletions.
19 changes: 19 additions & 0 deletions apps/backend/src/miscellaneous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,16 @@ impl MiscellaneousQuery {
let service = gql_ctx.data_unchecked::<Arc<MiscellaneousService>>();
service.get_oidc_token(code).await
}

/// Get user by OIDC issuer ID.
async fn user_by_oidc_issuer_id(
&self,
gql_ctx: &Context<'_>,
oidc_issuer_id: String,
) -> Result<Option<String>> {
let service = gql_ctx.data_unchecked::<Arc<MiscellaneousService>>();
service.user_by_oidc_issuer_id(oidc_issuer_id).await
}
}

#[derive(Default)]
Expand Down Expand Up @@ -7166,6 +7176,15 @@ GROUP BY m.id;
}
}

async fn user_by_oidc_issuer_id(&self, oidc_issuer_id: String) -> Result<Option<String>> {
let user = User::find()
.filter(user::Column::OidcIssuerId.eq(oidc_issuer_id))
.one(&self.db)
.await?
.map(|u| u.id);
Ok(user)
}

async fn invalidate_import_jobs(&self) -> Result<()> {
let all_jobs = ImportReport::find()
.filter(import_report::Column::WasSuccess.is_null())
Expand Down
33 changes: 19 additions & 14 deletions apps/frontend/app/routes/api.auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
LoginUserDocument,
RegisterErrorVariant,
RegisterUserDocument,
UserByOidcIssuerIdDocument,
} from "@ryot/generated/graphql/backend/graphql";
import { z } from "zod";
import { zx } from "zodix";
Expand All @@ -30,21 +31,25 @@ export const loader = unstable_defineLoader(async ({ request }) => {
email: getOidcToken.email,
issuerId: getOidcToken.subject,
};
const [_, { registerUser }] = await Promise.all([
getCachedCoreDetails(),
serverGqlService.request(RegisterUserDocument, {
input: { data: { oidc: oidcInput } },
}),
]);
if (
registerUser.__typename === "RegisterError" &&
registerUser.error === RegisterErrorVariant.Disabled
) {
return redirectWithToast($path("/auth"), {
message: "Registration is disabled",
type: "error",
});
const { userByOidcIssuerId } = await serverGqlService.request(
UserByOidcIssuerIdDocument,
{ oidcIssuerId: oidcInput.issuerId },
);
if (!userByOidcIssuerId) {
const { registerUser } = await serverGqlService.request(
RegisterUserDocument,
{ input: { data: { oidc: oidcInput } } },
);
if (
registerUser.__typename === "RegisterError" &&
registerUser.error === RegisterErrorVariant.Disabled
)
return redirectWithToast($path("/auth"), {
message: "Registration is disabled",
type: "error",
});
}
await getCachedCoreDetails();
const { loginUser } = await serverGqlService.request(LoginUserDocument, {
input: { oidc: oidcInput },
});
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/app/routes/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export const action = unstable_defineAction(async ({ request }) => {
headers: await createToastHeaders({ message, type: "error" }),
});
},
getOauthRedirectUrl: async () => {
getOidcRedirectUrl: async () => {
const { getOidcRedirectUrl } = await serverGqlService.request(
GetOidcRedirectUrlDocument,
);
Expand Down Expand Up @@ -287,7 +287,7 @@ export default function Page() {
<Form
replace
method="POST"
action={withQuery("", { intent: "getOauthRedirectUrl" })}
action={withQuery("", { intent: "getOidcRedirectUrl" })}
>
<Button
variant="outline"
Expand Down
Loading

0 comments on commit 69d9fad

Please sign in to comment.