first
This commit is contained in:
commit
8538ab969a
434 changed files with 44051 additions and 0 deletions
60
apps/web/modules/saas/auth/lib/api.ts
Normal file
60
apps/web/modules/saas/auth/lib/api.ts
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import { authClient } from "@repo/auth/client";
|
||||
import { config } from "@repo/config";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
|
||||
export const sessionQueryKey = ["user", "session"] as const;
|
||||
|
||||
export const useSessionQuery = () => {
|
||||
return useQuery({
|
||||
queryKey: sessionQueryKey,
|
||||
queryFn: async () => {
|
||||
const { data, error } = await authClient.getSession({
|
||||
query: {
|
||||
disableCookieCache: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (error) {
|
||||
throw new Error(error.message || "Failed to fetch session");
|
||||
}
|
||||
|
||||
return data;
|
||||
},
|
||||
staleTime: Number.POSITIVE_INFINITY,
|
||||
refetchOnWindowFocus: false,
|
||||
retry: false,
|
||||
enabled: config.ui.saas.enabled,
|
||||
});
|
||||
};
|
||||
|
||||
export const userAccountQueryKey = ["user", "accounts"] as const;
|
||||
export const useUserAccountsQuery = () => {
|
||||
return useQuery({
|
||||
queryKey: userAccountQueryKey,
|
||||
queryFn: async () => {
|
||||
const { data, error } = await authClient.listAccounts();
|
||||
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
return data;
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export const userPasskeyQueryKey = ["user", "passkeys"] as const;
|
||||
export const useUserPasskeysQuery = () => {
|
||||
return useQuery({
|
||||
queryKey: userPasskeyQueryKey,
|
||||
queryFn: async () => {
|
||||
const { data, error } = await authClient.passkey.listUserPasskeys();
|
||||
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
return data;
|
||||
},
|
||||
});
|
||||
};
|
||||
75
apps/web/modules/saas/auth/lib/server.ts
Normal file
75
apps/web/modules/saas/auth/lib/server.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
import "server-only";
|
||||
import { auth } from "@repo/auth";
|
||||
import { getInvitationById } from "@repo/database";
|
||||
import { headers } from "next/headers";
|
||||
import { cache } from "react";
|
||||
|
||||
export const getSession = cache(async () => {
|
||||
const session = await auth.api.getSession({
|
||||
headers: await headers(),
|
||||
query: {
|
||||
disableCookieCache: true,
|
||||
},
|
||||
});
|
||||
|
||||
return session;
|
||||
});
|
||||
|
||||
export const getActiveOrganization = cache(async (slug: string) => {
|
||||
try {
|
||||
const activeOrganization = await auth.api.getFullOrganization({
|
||||
query: {
|
||||
organizationSlug: slug,
|
||||
},
|
||||
headers: await headers(),
|
||||
});
|
||||
|
||||
return activeOrganization;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
export const getOrganizationList = cache(async () => {
|
||||
try {
|
||||
const organizationList = await auth.api.listOrganizations({
|
||||
headers: await headers(),
|
||||
});
|
||||
|
||||
return organizationList;
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
});
|
||||
|
||||
export const getUserAccounts = cache(async () => {
|
||||
try {
|
||||
const userAccounts = await auth.api.listUserAccounts({
|
||||
headers: await headers(),
|
||||
});
|
||||
|
||||
return userAccounts;
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
});
|
||||
|
||||
export const getUserPasskeys = cache(async () => {
|
||||
try {
|
||||
const userPasskeys = await auth.api.listPasskeys({
|
||||
headers: await headers(),
|
||||
});
|
||||
|
||||
return userPasskeys;
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
});
|
||||
|
||||
export const getInvitation = cache(async (id: string) => {
|
||||
try {
|
||||
return await getInvitationById(id);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
12
apps/web/modules/saas/auth/lib/session-context.ts
Normal file
12
apps/web/modules/saas/auth/lib/session-context.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import type { Session } from "@repo/auth";
|
||||
import React from "react";
|
||||
|
||||
export const SessionContext = React.createContext<
|
||||
| {
|
||||
session: Session["session"] | null;
|
||||
user: Session["user"] | null;
|
||||
loaded: boolean;
|
||||
reloadSession: () => Promise<void>;
|
||||
}
|
||||
| undefined
|
||||
>(undefined);
|
||||
Loading…
Add table
Add a link
Reference in a new issue