25 lines
723 B
TypeScript
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;
|