Skip to content

Commit

Permalink
feat(space-nuxt-base): add appBridgeSession (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
eunjae-lee committed Aug 23, 2024
1 parent 47968ff commit 828c197
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion space-plugins/nuxt-base/composables/useAppBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const useAppBridgeAuth = ({

const isAuthenticated = () => {
try {
const payload: DecodedToken = JSON.parse(
const payload: AppBridgeSession = JSON.parse(
sessionStorage.getItem(KEY_VALIDATED_PAYLOAD) || '',
);
return payload && new Date().getTime() / 1000 < payload.exp;
Expand Down
3 changes: 2 additions & 1 deletion space-plugins/nuxt-base/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { AppSession } from '@storyblok/app-extension-auth';

declare module 'h3' {
interface H3EventContext {
appSession: AppSession;
appSession?: AppSession;
appBridgeSession?: AppBridgeSession;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export default defineEventHandler(async (event) => {
if (!result.ok) {
throw createError({ statusCode: 401 });
}
event.context.appBridgeSession = result.result;
});
4 changes: 2 additions & 2 deletions space-plugins/nuxt-base/server/utils/appBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export const verifyAppBridgeToken = async (
async function verifyToken(
token: string,
secret: string,
): Promise<DecodedToken> {
): Promise<AppBridgeSession> {
return new Promise((resolve, reject) => {
const verifyCallback: VerifyCallback = (err, decoded) =>
err ? reject(err) : resolve(decoded as DecodedToken);
err ? reject(err) : resolve(decoded as AppBridgeSession);
jwt.verify(token, secret, verifyCallback);
});
}
4 changes: 2 additions & 2 deletions space-plugins/nuxt-base/types/appBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export type AppBridgeConfig = {
};

export type VerifyResponse =
| { ok: true; result: DecodedToken }
| { ok: true; result: AppBridgeSession }
| { ok: false; error: unknown };

export type DecodedToken = {
export type AppBridgeSession = {
app_id: number;
space_id: number;
user_id: number;
Expand Down

0 comments on commit 828c197

Please sign in to comment.