MCPcopy Create free account
hub / github.com/hashintel/hash / getUserAndSession

Function getUserAndSession

apps/hash-api/src/auth/create-auth-handlers.ts:130–206  ·  view source on GitHub ↗
({
  context,
  cookie,
  logger,
  sessionToken,
}: {
  context: ImpureGraphContext;
  cookie?: string;
  logger: Logger;
  sessionToken?: string;
})

Source from the content-addressed store, hash-verified

128};
129
130export const getUserAndSession = async ({
131 context,
132 cookie,
133 logger,
134 sessionToken,
135}: {
136 context: ImpureGraphContext;
137 cookie?: string;
138 logger: Logger;
139 sessionToken?: string;
140}): Promise<{
141 session?: Session;
142 user?: User;
143}> => {
144 const authentication = { actorId: systemAccountId };
145
146 const kratosSession = await kratosFrontendApi
147 .toSession({
148 cookie,
149 xSessionToken: sessionToken,
150 })
151 .then(({ data }) => data)
152 .catch((err: AxiosError) => {
153 if (err.response && err.response.status === 403) {
154 logger.debug(
155 "Session requires AAL2 but only has AAL1. Treating as unauthenticated.",
156 );
157 return undefined;
158 }
159 logger.debug(
160 `Kratos response error: Could not fetch session, got: [${
161 err.response?.status
162 }] ${JSON.stringify(err.response?.data)}`,
163 );
164 return undefined;
165 });
166
167 if (kratosSession) {
168 const { identity } = kratosSession;
169
170 if (!identity) {
171 throw new Error("Could not find kratos identity for session");
172 }
173
174 const { id: kratosIdentityId, traits } = identity as KratosUserIdentity;
175
176 let user = await getUser(context, authentication, {
177 kratosIdentityId,
178 emails: traits.emails,
179 });
180
181 if (!user) {
182 const hashInstance = await getHashInstance(context, authentication);
183
184 if (!hashInstance.userSelfRegistrationIsEnabled) {
185 throw new Error("User registration is disabled.");
186 }
187

Callers 2

createAuthMiddlewareFunction · 0.85

Calls 4

getUserFunction · 0.90
getHashInstanceFunction · 0.90
createUserFunction · 0.90
thenMethod · 0.80

Tested by

no test coverage detected