Extend protectedHandler to contain user object with email and permissions (#252)
This commit is contained in:
parent
215a410d58
commit
390fae2c97
5 changed files with 19 additions and 1 deletions
5
.changeset/wicked-jobs-exist.md
Normal file
5
.changeset/wicked-jobs-exist.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
"@saleor/app-sdk": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Extended context argument in createProtectedHandler. Now it contains "user" object with email and permissions
|
|
@ -15,6 +15,10 @@ First, create handler for your business logic. The only difference from usual Ne
|
||||||
export type ProtectedHandlerContext = {
|
export type ProtectedHandlerContext = {
|
||||||
baseUrl: string; // the URL your application is available
|
baseUrl: string; // the URL your application is available
|
||||||
authData: AuthData; // Auth Data which can be used to communicate with the Saleor API
|
authData: AuthData; // Auth Data which can be used to communicate with the Saleor API
|
||||||
|
user: {
|
||||||
|
email: string;
|
||||||
|
userPermissions: string[];
|
||||||
|
};
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -63,6 +63,10 @@ describe("processSaleorProtectedHandler", () => {
|
||||||
jwks: mockAPL.mockJwks,
|
jwks: mockAPL.mockJwks,
|
||||||
},
|
},
|
||||||
baseUrl: "https://some-saleor-host.cloud",
|
baseUrl: "https://some-saleor-host.cloud",
|
||||||
|
user: expect.objectContaining({
|
||||||
|
email: expect.any(String),
|
||||||
|
userPermissions: expect.any(Array),
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { AuthData } from "../../APL/apl";
|
||||||
import { createDebug } from "../../debug";
|
import { createDebug } from "../../debug";
|
||||||
import { getBaseUrl, getSaleorHeaders } from "../../headers";
|
import { getBaseUrl, getSaleorHeaders } from "../../headers";
|
||||||
import { Permission } from "../../types";
|
import { Permission } from "../../types";
|
||||||
|
import { extractUserFromJwt, TokenUserPayload } from "../../util/extract-user-from-jwt";
|
||||||
import { verifyJWT } from "../../verify-jwt";
|
import { verifyJWT } from "../../verify-jwt";
|
||||||
|
|
||||||
const debug = createDebug("processProtectedHandler");
|
const debug = createDebug("processProtectedHandler");
|
||||||
|
@ -34,6 +35,7 @@ export class ProtectedHandlerError extends Error {
|
||||||
export type ProtectedHandlerContext = {
|
export type ProtectedHandlerContext = {
|
||||||
baseUrl: string;
|
baseUrl: string;
|
||||||
authData: AuthData;
|
authData: AuthData;
|
||||||
|
user: TokenUserPayload;
|
||||||
};
|
};
|
||||||
|
|
||||||
interface ProcessSaleorProtectedHandlerArgs {
|
interface ProcessSaleorProtectedHandlerArgs {
|
||||||
|
@ -96,8 +98,11 @@ export const processSaleorProtectedHandler: ProcessAsyncSaleorProtectedHandler =
|
||||||
throw new ProtectedHandlerError("JWT verification failed: ", "JWT_VERIFICATION_FAILED");
|
throw new ProtectedHandlerError("JWT verification failed: ", "JWT_VERIFICATION_FAILED");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const userJwtPayload = extractUserFromJwt(token);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
baseUrl,
|
baseUrl,
|
||||||
authData,
|
authData,
|
||||||
|
user: userJwtPayload,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as jose from "jose";
|
||||||
|
|
||||||
import { Permission } from "../types";
|
import { Permission } from "../types";
|
||||||
|
|
||||||
type TokenUserPayload = {
|
export type TokenUserPayload = {
|
||||||
email: string;
|
email: string;
|
||||||
userPermissions: Permission[];
|
userPermissions: Permission[];
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue