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

Add withAuth HOC [SDK-2120] #189

Closed
wants to merge 5 commits into from
Closed

Add withAuth HOC [SDK-2120] #189

wants to merge 5 commits into from

Conversation

Widcket
Copy link
Contributor

@Widcket Widcket commented Nov 4, 2020

Description

This PR adds the withAuth frontend HOC, that allows to fetch the user profile on server-side rendered pages.

Testing

  • This change adds test coverage for new/changed/fixed functionality

Checklist

  • I have added documentation for new/changed functionality in this PR or in auth0.com/docs
  • All active GitHub checks for tests, formatting, and security are passing
  • The correct base branch is being used, if not master

@Widcket Widcket added the review:medium Medium review label Nov 4, 2020
@Widcket Widcket requested a review from a team as a code owner November 4, 2020 23:48
}

public componentDidMount(): void {
window.location.assign(createLoginUrl(Router.pathname));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since useEffect runs after the render, by using a class-based component with componentDidMount we're avoiding one extra render.

return { user: null, children: undefined };
}

return { user: session.user as NullableUserProfile, children: undefined };
Copy link
Contributor Author

@Widcket Widcket Nov 4, 2020

Choose a reason for hiding this comment

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

Session uses a Claims type for the profile.

return { user: result, children: undefined };
}

const session = await instance.getSession(context.req as NextApiRequest, context.res as NextApiResponse);
Copy link
Contributor

Choose a reason for hiding this comment

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

context.req in getInitialProps is an IncomingMessage, so we need to update the types od getSession to accept IncomingMessage | NextApiRequest (and the same for ServerResponse and NextApiResponse)

Also getSession will error if context.res is undefined, so we need to figure out when that would be and handle those cases

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In next they're also casting, so it seems safe. But it's probably better to use a union type.

Copy link
Contributor Author

@Widcket Widcket Nov 5, 2020

Choose a reason for hiding this comment

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

Also getSession will error if context.res is undefined, so we need to figure out when that would be and handle those cases

I'll look into that 👍🏼

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just tried the union type: Argument of type 'ServerResponse | undefined' is not assignable to parameter of type 'ServerResponse | NextApiResponse<any>'. Will have to keep the cast.

export default function withAuth(
InnerComponent: React.ElementType,
redirect: () => React.ReactElement,
instance: ISignInWithAuth0
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if it would be better for ISignInWithAuth0 to have a withAuth wrapper like the handlers (rather than having to pass the instance in) eg

// profile-ssr.jsx
import { withAuth } from '../lib/auth0';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, so the the instance would be providing the HOC (and it would already have the instance 'closed over').


export default function withAuth(
InnerComponent: React.ElementType,
redirect: () => React.ReactElement,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we go for an options object here fro the other params? (like https://github.com/auth0/auth0-react/blob/master/src/with-authentication-required.tsx#L75)

It gives us more flexibility when designing the API

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, I'll change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But if the instance will be providing the HOC, as in :

// profile-ssr.jsx
import { withAuth } from '../lib/auth0';

does it still make sense to use an options object? Because it will only be two params now (the inner component, and the render prop).

return <InnerComponent {...props} user={user} />;
};

Authenticated.getInitialProps = async (context: NextPageContext): Promise<AuthenticatedProps> => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, since we're targeting 9.4+ we should probably use getServerSideProps

If you're using Next.js 9.3 or newer, we recommend that you use getStaticProps or getServerSideProps instead of getInitialProps.
https://nextjs.org/docs/api-reference/data-fetching/getInitialProps

};

Authenticated.getInitialProps = async (context: NextPageContext): Promise<AuthenticatedProps> => {
if (!context.req) {
Copy link
Contributor

@adamjmcgrath adamjmcgrath Nov 5, 2020

Choose a reason for hiding this comment

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

I'm not sure about this either, context.req will be falsey when rendering the static page at build time, so we shouldn't be checking the session here - let's have a chat about this, because I'm not sure how this should work at the moment

@Widcket Widcket deleted the feature/frontend-hoc branch January 12, 2021 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review:medium Medium review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants