This repository has been archived on 2024-06-14. You can view files and clone it, but cannot push or open issues or pull requests.
icynet-admin/lib/session.ts
2022-08-29 21:09:28 +03:00

25 lines
723 B
TypeScript

import { GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
import { getUserInfo } from './api/remote';
import { CurrentUserDto } from './types/user-response.interface';
import { getActiveCookie } from './utils/get-active-cookie';
function withUser(
handler: (
ctx: GetServerSidePropsContext,
user?: CurrentUserDto
) => Promise<GetServerSidePropsResult<any>>
) {
return async (ctx: GetServerSidePropsContext) => {
const accessToken = getActiveCookie(ctx, 'authorization');
let user: CurrentUserDto | undefined;
if (accessToken) {
try {
user = await getUserInfo(accessToken);
} catch (e) {}
}
return handler(ctx, user);
};
}
export default withUser;