forked from duddud11/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
23 lines (21 loc) · 919 Bytes
/
auth.ts
File metadata and controls
23 lines (21 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import type { GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
import { ParsedUrlQuery } from 'querystring';
import { validateFusionFeedToken } from './fusionfeed';
export function withAuth<Params, Props>(
f: (ctx: GetServerSidePropsContext<Params & ParsedUrlQuery>) => Promise<GetServerSidePropsResult<Props>>,
): (ctx: GetServerSidePropsContext<Params & ParsedUrlQuery>) => Promise<GetServerSidePropsResult<Props>> {
return async (ctx) => {
const { req } = ctx;
const token = req.cookies['fftoken'];
const dest = ctx.resolvedUrl || req.url || '/';
if (!token || !(await validateFusionFeedToken(token))) {
return {
redirect: {
destination: '/login#destination=' + encodeURIComponent(dest),
permanent: false,
},
};
}
return await f(ctx);
};
}