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/#274 SSR에서 토큰 탑재 자동화 및 마이페이지 리팩토링 #279

Merged
merged 7 commits into from
Jan 25, 2024
Prev Previous commit
Next Next commit
Feat: SSR에서 토큰을 탑재하는 HOF 추가
  • Loading branch information
navyjeongs committed Jan 18, 2024
commit 9d0dad65a78bfc6cd0b37b922a44418f15ba54c1
25 changes: 25 additions & 0 deletions src/utils/withAuthentication.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import authAPI from '@apis/authAPI';
import { parse } from 'cookie';
import { GetServerSidePropsContext } from 'next';

const withAuthServerSideProps = (getServerSidePropsFunction: () => Promise<any>) => {
return async (context: GetServerSidePropsContext) => {
const cookies = context.req.headers.cookie || '';

const accessToken = parse(cookies).accessToken;

if (!accessToken) {
throw new Error('401 Unauthorized');
}

authAPI.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`;

const res = await getServerSidePropsFunction();

authAPI.defaults.headers.common['Authorization'] = '';

return res;
};
};

export default withAuthServerSideProps;