Skip to content

Commit

Permalink
Merge pull request #22 from GuiMoraesDev/fix/fixRequestsWithoutBearer…
Browse files Browse the repository at this point in the history
…Token

fix: fix requests without bearer token
  • Loading branch information
GuiMoraesDev authored Sep 2, 2022
2 parents 13126b5 + 16a4516 commit 747e95c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/pages/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const Page = ({ user }: InferGetServerSidePropsType<ServerSideProps>) => {
};

export const getServerSideProps: ServerSideProps = async (context) => {
const { user: cookiesUser, token } = nookies.get(context);
const { user: cookiesUser } = nookies.get(context);

if (!cookiesUser) {
return {
Expand All @@ -35,8 +35,6 @@ export const getServerSideProps: ServerSideProps = async (context) => {

const user = JSON.parse(cookiesUser) as UserProps;

api.defaults.headers.common['authorization'] = `Bearer ${token}`;

return {
props: { user },
};
Expand Down
19 changes: 18 additions & 1 deletion src/services/api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import axios from 'axios';
import nookies from 'nookies';

if (!process.env.NEXT_PUBLIC_API_URL) {
throw new Error('NEXT_PUBLIC_API_URL is missing');
}

const api = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,

});

api.interceptors.request.use((config) => {
const { token: cookiesToken } = nookies.get();

if (!cookiesToken) {
return config;
}

const token = JSON.parse(cookiesToken) as string;

return {
...config,
headers: {
Authorization: `Bearer ${token}`,
},
};
});

export default api;

1 comment on commit 747e95c

@vercel
Copy link

@vercel vercel bot commented on 747e95c Sep 2, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

teller – ./

teller-six.vercel.app
teller-guimoraes.vercel.app
teller-git-main-guimoraes.vercel.app

Please sign in to comment.