Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(space-nuxt-base): add appBridgeSession #81

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor Author

@eunjae-lee eunjae-lee Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case we disable oauth on a project, this can should be optional.

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