Skip to content

Commit

Permalink
Make ssoservice export separate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianbw committed May 12, 2023
1 parent f639bb8 commit e96fb45
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 46 deletions.
70 changes: 35 additions & 35 deletions lib/ssoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@ import loadStytch from "./loadStytch";

const stytch = loadStytch();

export const SSOService = {
list: async function (organization_id: string) {
return stytch.sso.get({ organization_id });
},
createSaml: async function (display_name: string, organization_id: string) {
return stytch.sso.saml.create({
organization_id,
display_name,
});
},
createOidc: async function (display_name: string, organization_id: string) {
return stytch.sso.oidc.create({
organization_id,
display_name,
});
},
// delete: async function(member_id: string, organization_id: string) {
// return stytch.organizations.members.delete({
// organization_id,
// member_id,
// })
// },
// findBySessionToken: async function (sessionToken: string): Promise<Member | null> {
// return stytch.sessions.authenticate({
// session_duration_minutes: 30, // extend the session a bit
// session_token: sessionToken
// })
// .then(res => {
// return res.member
// })
// .catch(err => {
// console.error('Could not find member by session token', err)
// return null
// })
// }

export const list = async (organization_id: string) => {
return stytch.sso.get({ organization_id });
};
export const createSaml = async (display_name: string, organization_id: string) => {
return stytch.sso.saml.create({
organization_id,
display_name,
});
};
export const createOidc = async (display_name: string, organization_id: string) => {
return stytch.sso.oidc.create({
organization_id,
display_name,
});
};
// export const delete = async (member_id: string, organization_id: string) => {
// return stytch.organizations.members.delete({
// organization_id,
// member_id,
// })
// }
// export const findBySessionToken = async function (sessionToken: string): Promise<Member | null> => {
// return stytch.sessions.authenticate({
// session_duration_minutes: 30, // extend the session a bit
// session_token: sessionToken
// })
// .then(res => {
// return res.member
// })
// .catch(err => {
// console.error('Could not find member by session token', err)
// return null
// })
// }

4 changes: 2 additions & 2 deletions pages/[slug]/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
deleteMember,
invite,
} from "@/lib/api";
import { SSOService } from "@/lib/ssoService";
import { useAuth, withSession } from "@/lib/sessionService";
import {
Member,
Expand All @@ -23,6 +22,7 @@ import {
} from "@/lib/loadStytch";
import { SSO } from "stytch/types/lib/b2b/sso";
import { findAllMembers, findByID } from "@/lib/orgService";
import { list } from "@/lib/ssoService";

type Props = {
org: Organization;
Expand Down Expand Up @@ -316,7 +316,7 @@ export const getServerSideProps = withSession<Props, { slug: string; }>(

const [members, ssoConnections] = await Promise.all([
findAllMembers(org.organization_id),
SSOService.list(org.organization_id),
list(org.organization_id),
]);

return {
Expand Down
6 changes: 3 additions & 3 deletions pages/[slug]/dashboard/oidc/[connection_id].tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { findByID } from "@/lib/orgService";
import { SSOService } from "@/lib/ssoService";
import { BaseSyntheticEvent, FormEventHandler } from "react";
import { FormEventHandler } from "react";
import { updateOidcSSOConn } from "@/lib/api";
import { useRouter } from "next/router";
import { formatSSOStartURL, OIDCConnection } from "@/lib/loadStytch";
import { useAuth, withSession } from "@/lib/sessionService";
import Link from "next/link";
import { list } from "@/lib/ssoService";

type Props = { connection: OIDCConnection; };

Expand Down Expand Up @@ -169,7 +169,7 @@ export const getServerSideProps = withSession<
return { redirect: { statusCode: 307, destination: `/login` } };
}

const connection = await SSOService.list(org.organization_id).then((res) => {
const connection = await list(org.organization_id).then((res) => {
return res.oidc_connections.find(
(conn) => conn.connection_id === connection_id
);
Expand Down
4 changes: 2 additions & 2 deletions pages/[slug]/dashboard/saml/[connection_id].tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { findByID } from "@/lib/orgService";
import { SSOService } from "@/lib/ssoService";
import { FormEventHandler } from "react";
import { updateSamlSSOConn } from "@/lib/api";
import { useRouter } from "next/router";
import { formatSSOStartURL, SAMLConnection } from "@/lib/loadStytch";
import { useAuth, withSession } from "@/lib/sessionService";
import Link from "next/link";
import { list } from "@/lib/ssoService";

type Props = { connection: SAMLConnection; };

Expand Down Expand Up @@ -133,7 +133,7 @@ export const getServerSideProps = withSession<
return { redirect: { statusCode: 307, destination: `/login` } };
}

const connection = await SSOService.list(org.organization_id).then((res) =>
const connection = await list(org.organization_id).then((res) =>
res.saml_connections.find((conn) => conn.connection_id === connection_id)
);

Expand Down
4 changes: 2 additions & 2 deletions pages/api/sso/oidc/create.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This API route creates an OIDC connection
import type { NextApiRequest, NextApiResponse } from "next";
import { SSOService } from "@/lib/ssoService";
import { adminOnlyAPIRoute } from "@/lib/sessionService";
import { Member } from "@/lib/loadStytch";
import { createOidc } from "@/lib/ssoService";

async function handler(
member: Member,
Expand All @@ -11,7 +11,7 @@ async function handler(
) {
try {
const { display_name } = JSON.parse(req.body);
const { connection } = await SSOService.createOidc(
const { connection } = await createOidc(
display_name,
member.organization_id
);
Expand Down
4 changes: 2 additions & 2 deletions pages/api/sso/saml/create.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This API route creates a new SAML connection
import type { NextApiRequest, NextApiResponse } from "next";
import { SSOService } from "@/lib/ssoService";
import { adminOnlyAPIRoute } from "@/lib/sessionService";
import { Member } from "@/lib/loadStytch";
import { createSaml } from "@/lib/ssoService";

async function handler(
member: Member,
Expand All @@ -11,7 +11,7 @@ async function handler(
) {
try {
const { display_name } = JSON.parse(req.body);
const { connection } = await SSOService.createSaml(
const { connection } = await createSaml(
display_name,
member.organization_id
);
Expand Down

0 comments on commit e96fb45

Please sign in to comment.