Mihai Andrei
08/10/2021, 6:19 AMehesp
08/10/2021, 6:21 AMMihai Andrei
08/10/2021, 6:25 AMjonny
08/10/2021, 7:23 AMfrubalu
08/10/2021, 3:47 PMfrubalu
08/10/2021, 3:47 PMclaud9
08/10/2021, 3:52 PMconst { user } = await supabase.auth.api.getUserByCookie(req);
const isAuthenticated = user ? user.role === 'authenticated' : false;
if (isAuthenticated) {
return { props: {} };
}
return {
props: {},
redirect: { destination: '/login', permanent: false },
};
frubalu
08/10/2021, 4:20 PMgetUserByCookie
method, but I need the token for backend authentication. If anyone's curious, here's kind of what my implementation is looking like:
export const getServerSideProps: GetServerSideProps = async (
ctx
): Promise<any> => {
const token = cookies(ctx)['sb:token'];
if (!token) return redirectToLogin({ reason: 'AccessDenied', server: true });
const client = initializeApollo({ headers: ctx?.req?.headers });
const context = {
headers: {
Authorization: `Bearer ${token}`,
},
};
try {
// query for profile page data
await client.query({
query: MeDocument,
context,
});
return addApolloState(client, {
props: {
context,
},
});
} catch (err) {
console.error({ err });
return { notFound: true };
}
};
claud9
08/10/2021, 4:54 PMgetServerSideProps
2. Protect the routes by using row level security in my APIs
So I guess the question is are you trying to protect pages or protect your data? In this code above it looks like you are just trying to protect your data and then if there is an error, it takes you to a 404.frubalu
08/10/2021, 5:01 PMnotFound: true
return as just a placeholder at the moment. I'm going to be reworking it soon to actually act as more of a page protectionMihai Andrei
08/10/2021, 5:24 PMMihai Andrei
08/10/2021, 8:04 PMclaud9
08/11/2021, 1:26 PMconst { user } = await supabase.auth.api.getUserByCookie(req);
const isAuthenticated = user ? user.role === 'authenticated' : false;
const cookies = new Cookies(req, res);
const token = cookies.get('sb:token');
if (isAuthenticated && token) {
supabase.auth.setAuth(token);
I like to handle everything server side with cookies. That way it's a lot better experience and everything is decoupled and has a single responsibility.Mihai Andrei
08/11/2021, 1:27 PMclaud9
08/11/2021, 1:38 PMexport async function getServerSideProps({
req,
}: NextPageContext): Promise<GetServerSidePropsResult<Props | {}>> {
const { user } = await supabase.auth.api.getUserByCookie(req);
const isAuthenticated = user ? user.role === 'authenticated' : false;
if (isAuthenticated) {
return { props: {}};
}
return {
props: {},
redirect: { destination: '/login', permanent: false },
};
}
APIs
const handler = async (
req: NextApiRequest,
res: NextApiResponse
): Promise<void> => {
const { user } = await supabase.auth.api.getUserByCookie(req);
const isAuthenticated = user ? user.role === 'authenticated' : false;
const cookies = new Cookies(req, res);
const token = cookies.get('sb:token');
if (isAuthenticated && token) {
supabase.auth.setAuth(token);
//DO something
res.status(200).end();
} else {
res.status(401).end();
}
};
export default handler;
And then the tutorial above, will show you how to set cookies but I did it this way, with an API endpoint.
const handler = (req: NextApiRequest, res: NextApiResponse): void => {
if (req.body.event) {
supabase.auth.api.setAuthCookie(req, res);
res.status(200).end();
} else {
res.status(400).end();
}
};
Mihai Andrei
08/11/2021, 6:33 PM