Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Feat/user broker #4

Merged
merged 11 commits into from
May 23, 2022
Prev Previous commit
Next Next commit
perf(Auth): improve me API and homepage
  • Loading branch information
mateonunez committed May 23, 2022
commit 0d296d8acaf3d91e19b0afae233690b55afd81a8
26 changes: 16 additions & 10 deletions frontend/react/authentication/pages/api/users/me.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import cookie from 'cookie';
import { User } from 'lib/user/types';
import { NextApiRequest, NextApiResponse } from 'next';
import cookie from 'cookie';

export type MeResponse = {
success: boolean;
data?: User;
data?: User | {};
message: string;
};

export async function getMe(token: string): Promise<Response> {
const response = await fetch('http://architect_nginx_laravel/api/users/me', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
}
});

return response;
}

export default async function handler(req: NextApiRequest, res: NextApiResponse<MeResponse>) {
if (req.method !== 'GET') {
res.setHeader('Allow', ['GET']);
Expand All @@ -19,15 +31,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
return;
}

const { ARCHITOKEN: token } = cookie.parse(req.headers.cookie);
const { ARCHITOKEN: token } = cookie.parse(req.headers.cookie || '');

const response = await fetch('http://architect_nginx_laravel/api/users/me', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
}
});
const response = await getMe(token);

const data: MeResponse = await response.json();

Expand Down
39 changes: 25 additions & 14 deletions frontend/react/authentication/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import cookie from 'cookie';
import { AuthContext } from 'lib/contexts';
import type { GetServerSidePropsContext, NextPage } from 'next';
import { useContext } from 'react';
import type { GetServerSideProps, InferGetServerSidePropsType } from 'next';
import { getMe, MeResponse } from './api/users/me';

export async function getServerSideProps({ req }: GetServerSidePropsContext) {
export const getServerSideProps: GetServerSideProps = async ({ req }): Promise<any> => {
const { ARCHITOKEN: token = null } = cookie.parse(req.headers.cookie || '');

if (!token) {
Expand All @@ -14,23 +13,35 @@ export async function getServerSideProps({ req }: GetServerSidePropsContext) {
};
}

// TODO resolve user via server-side
const response = await getMe(token);

const data: MeResponse = await response.json();

const user = data?.data;

if (!user) {
console.error('User logged out?');

return {
redirect: {
destination: '/auth/login'
}
};
}

return {
props: {}
props: {
user
}
};
}

const HomePage: NextPage = () => {
const { user } = useContext(AuthContext);
};

export default function HomePage({ user }: InferGetServerSidePropsType<typeof getServerSideProps>) {
return (
<>
<div className="title">
Hello User: {user.name} [{user.email}]
Hello User: {user?.name} [{user?.email}]
</div>
</>
);
};

export default HomePage;
}