Skip to content

Commit

Permalink
nextjs 1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dvird committed Jul 20, 2024
1 parent 55cab88 commit cfb94bf
Show file tree
Hide file tree
Showing 67 changed files with 483 additions and 481 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { handleAuth, handleLogin } from '@eartho/one-client-nextjs';
import { handleAccess, handleLogin } from '@eartho/one-client-nextjs';

export const GET = handleAuth({
export const GET = handleAccess({
login: handleLogin({
authorizationParams: {
access_id: 'QJkg3evAtIqJgF80iB1o'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { handleAuth, handleLogin, handleCallback } from '@eartho/one-client-nextjs/edge';
import { handleAccess, handleLogin, handleCallback } from '@eartho/one-client-nextjs/edge';

const redirectUri = `${process.env.EARTHO_BASE_URL}/api/edge-auth/callback`;

export const GET = handleAuth({
export const GET = handleAccess({
login: handleLogin({
authorizationParams: { redirect_uri: redirectUri }
}),
Expand Down
6 changes: 3 additions & 3 deletions one-client-nextjs/example-app/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './globals.css';
import { EarthoProvider } from '@eartho/one-client-nextjs/client';
import { EarthoClientProvider } from '@eartho/one-client-nextjs/client';
import Nav from '@/app/nav';

export const metadata = {
Expand All @@ -10,14 +10,14 @@ export const metadata = {
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<EarthoProvider>
<EarthoClientProvider>
<body>
<header>
<Nav />
</header>
<div className="container">{children}</div>
</body>
</EarthoProvider>
</EarthoClientProvider>
</html>
);
}
4 changes: 2 additions & 2 deletions one-client-nextjs/example-app/app/nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default function Nav() {
{user ? (
<>
<li>
<a href="/api/auth/logout" data-testid="logout">
<a href="/api/access/logout" data-testid="logout">
Logout
</a>
</li>
Expand All @@ -77,7 +77,7 @@ export default function Nav() {
) : (
<>
<li>
<a href="/api/auth/login" data-testid="login">
<a href="/api/access/login" data-testid="login">
Login
</a>
</li>
Expand Down
4 changes: 2 additions & 2 deletions one-client-nextjs/example-app/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { withMiddlewareAuthRequired } from '@eartho/one-client-nextjs/edge';
import { withMiddlewareAccessRequired } from '@eartho/one-client-nextjs/edge';

export default withMiddlewareAuthRequired();
export default withMiddlewareAccessRequired();

export const config = {
matcher: ['/page-router/profile-middleware', '/profile-middleware']
Expand Down
6 changes: 3 additions & 3 deletions one-client-nextjs/example-app/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import type { AppProps } from 'next/app';
import { EarthoProvider } from '@eartho/one-client-nextjs/client';
import { EarthoClientProvider } from '@eartho/one-client-nextjs/client';

export default function App({ Component, pageProps }: AppProps): React.ReactElement<AppProps> {
const { user } = pageProps;

return (
<EarthoProvider user={user} profileUrl="/api/page-router-auth/me" loginUrl="/api/page-router-auth/login">
<EarthoClientProvider user={user} profileUrl="/api/page-router-auth/me" loginUrl="/api/page-router-auth/login">
<Component {...pageProps} />
</EarthoProvider>
</EarthoClientProvider>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { pageRouterAuth } from '@/lib/eartho';

const redirectUri = `${process.env.EARTHO_BASE_URL}/api/page-router-auth/callback`;

export default pageRouterAuth.handleAuth({
export default pageRouterAuth.handleAccess({
login: pageRouterAuth.handleLogin({
authorizationParams: { redirect_uri: redirectUri }
}),
Expand Down
4 changes: 2 additions & 2 deletions one-client-nextjs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion one-client-nextjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eartho/one-client-nextjs",
"version": "1.2.1",
"version": "1.3.0",
"description": "Next.js SDK for signing in with Eartho",
"private": false,
"exports": {
Expand Down
4 changes: 2 additions & 2 deletions one-client-nextjs/scripts/oidc-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const config = {
client_id: 'testing',
client_secret: 'testing',
redirect_uris: [
'http://localhost:3000/api/auth/callback',
'http://localhost:3000/api/access/callback',
'http://localhost:3000/api/page-router-auth/callback',
'http://localhost:3000/api/edge-auth/callback'
],
Expand All @@ -34,7 +34,7 @@ const config = {
routes: {
authorization: '/authorize', // lgtm [js/hardcoded-credentials]
token: '/oauth/token',
end_session: '/v2/logout'
end_session: '/logout'
},
scopes: ['openid', 'offline_access'],
clientBasedCORS(ctx, origin, client) {
Expand Down
6 changes: 3 additions & 3 deletions one-client-nextjs/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';
import {
default as EarthoProvider,
EarthoProviderProps,
default as EarthoClientProvider,
EarthoClientProviderProps,
UserProfile,
UserContext,
RequestError,
Expand All @@ -12,5 +12,5 @@ import {
WithPageAuthRequired,
WithPageAuthRequiredOptions
} from './with-page-auth-required';
export { EarthoProvider, EarthoProviderProps, UserProfile, UserContext, RequestError, useUser };
export { EarthoClientProvider, EarthoClientProviderProps, UserProfile, UserContext, RequestError, useUser };
export { withPageAuthRequired, WithPageAuthRequired, WithPageAuthRequiredOptions };
2 changes: 1 addition & 1 deletion one-client-nextjs/src/client/use-config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const useConfig: UseConfig = () => useContext<ConfigContext>(Config);

export default ({
children,
loginUrl = process.env.NEXT_PUBLIC_EARTHO_LOGIN || '/api/auth/login'
loginUrl = process.env.NEXT_PUBLIC_EARTHO_LOGIN || '/api/access/login'
}: ConfigProviderProps): ReactElement<ConfigContext> => {
return <Config.Provider value={{ loginUrl }}>{children}</Config.Provider>;
};
50 changes: 25 additions & 25 deletions one-client-nextjs/src/client/use-user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,25 @@ export class RequestError extends Error {
* Fetches the user from the profile API route to fill the {@link useUser} hook with the
* {@link UserProfile} object.
*
* If needed, you can pass a custom fetcher to the {@link EarthoProvider} component via the
* {@link EarthoProviderProps.fetcher} prop.
* If needed, you can pass a custom fetcher to the {@link EarthoClientProvider} component via the
* {@link EarthoClientProviderProps.fetcher} prop.
*
* @throws {@link RequestError}
*/
type UserFetcher = (url: string) => Promise<UserProfile | undefined>;

/**
* Configure the {@link EarthoProvider} component.
* Configure the {@link EarthoClientProvider} component.
*
* If you have any server-side rendered pages (using `getServerSideProps` or Server Components), you should get the
* user from the server-side session and pass it to the `<EarthoProvider>` component via the `user`
* user from the server-side session and pass it to the `<EarthoClientProvider>` component via the `user`
* prop. This will prefill the {@link useUser} hook with the {@link UserProfile} object.
* For example:
*
* ```js
* // pages/_app.js
* import React from 'react';
* import { EarthoProvider } from '@eartho/one-client-nextjs/client';
* import { EarthoClientProvider } from '@eartho/one-client-nextjs/client';
*
* export default function App({ Component, pageProps }) {
* // If you've used `withPageAuthRequired`, `pageProps.user` can prefill the hook
Expand All @@ -85,9 +85,9 @@ type UserFetcher = (url: string) => Promise<UserProfile | undefined>;
* const { user } = pageProps;
*
* return (
* <EarthoProvider user={user}>
* <EarthoClientProvider user={user}>
* <Component {...pageProps} />
* </EarthoProvider>
* </EarthoClientProvider>
* );
* }
* ```
Expand All @@ -96,7 +96,7 @@ type UserFetcher = (url: string) => Promise<UserProfile | undefined>;
*
* ```js
* // app/layout.js
* import { EarthoProvider } from '@eartho/one-client-nextjs/client';
* import { EarthoClientProvider } from '@eartho/one-client-nextjs/client';
*
* export default async function RootLayout({ children }) {
* // this will emit a warning because Server Components cannot write to cookies
Expand All @@ -106,9 +106,9 @@ type UserFetcher = (url: string) => Promise<UserProfile | undefined>;
* return (
* <html lang="en">
* <body>
* <EarthoProvider user={session?.user}>
* <EarthoClientProvider user={session?.user}>
* {children}
* </EarthoProvider>
* </EarthoClientProvider>
* </body>
* </html>
* );
Expand All @@ -120,34 +120,34 @@ type UserFetcher = (url: string) => Promise<UserProfile | undefined>;
* `fetcher` option.
*
* **IMPORTANT** If you have used a custom url for your {@link HandleProfile} API route handler
* (the default is `/api/auth/me`) then you need to specify it here in the `profileUrl` option.
* (the default is `/api/access/me`) then you need to specify it here in the `profileUrl` option.
*
* @category Client
*/
export type EarthoProviderProps = React.PropsWithChildren<
export type EarthoClientProviderProps = React.PropsWithChildren<
{ user?: UserProfile; profileUrl?: string; fetcher?: UserFetcher } & ConfigContext
>;

/**
* @ignore
*/
const missingEarthoProvider = 'You forgot to wrap your app in <EarthoProvider>';
const missingEarthoClientProvider = 'You forgot to wrap your app in <EarthoClientProvider>';

/**
* @ignore
*/
export const UserContext = createContext<UserContext>({
get user(): never {
throw new Error(missingEarthoProvider);
throw new Error(missingEarthoClientProvider);
},
get error(): never {
throw new Error(missingEarthoProvider);
throw new Error(missingEarthoClientProvider);
},
get isLoading(): never {
throw new Error(missingEarthoProvider);
throw new Error(missingEarthoClientProvider);
},
checkSession: (): never => {
throw new Error(missingEarthoProvider);
throw new Error(missingEarthoClientProvider);
}
});

Expand All @@ -170,8 +170,8 @@ export type UseUser = () => UserContext;
*
* if (isLoading) return <div>Loading...</div>;
* if (error) return <div>{error.message}</div>;
* if (!user) return <Link href="/api/auth/login"><a>Login</a></Link>;
* return <div>Hello {user.name}, <Link href="/api/auth/logout"><a>Logout</a></Link></div>;
* if (!user) return <Link href="/api/access/login"><a>Login</a></Link>;
* return <div>Hello {user.name}, <Link href="/api/access/logout"><a>Logout</a></Link></div>;
* }
* ```
*
Expand All @@ -180,16 +180,16 @@ export type UseUser = () => UserContext;
export const useUser: UseUser = () => useContext<UserContext>(UserContext);

/**
* To use the {@link useUser} hook, you must wrap your application in a `<EarthoProvider>` component.
* To use the {@link useUser} hook, you must wrap your application in a `<EarthoClientProvider>` component.
*
* @category Client
*/
export type EarthoProvider = (props: EarthoProviderProps) => ReactElement<UserContext>;
export type EarthoClientProvider = (props: EarthoClientProviderProps) => ReactElement<UserContext>;

/**
* @ignore
*/
type EarthoProviderState = {
type EarthoClientProviderState = {
user?: UserProfile;
error?: Error;
isLoading: boolean;
Expand All @@ -213,11 +213,11 @@ const userFetcher: UserFetcher = async (url) => {
export default ({
children,
user: initialUser,
profileUrl = process.env.NEXT_PUBLIC_EARTHO_PROFILE || '/api/auth/me',
profileUrl = process.env.NEXT_PUBLIC_EARTHO_PROFILE || '/api/access/me',
loginUrl,
fetcher = userFetcher
}: EarthoProviderProps): ReactElement<UserContext> => {
const [state, setState] = useState<EarthoProviderState>({ user: initialUser, isLoading: !initialUser });
}: EarthoClientProviderProps): ReactElement<UserContext> => {
const [state, setState] = useState<EarthoClientProviderState>({ user: initialUser, isLoading: !initialUser });

const checkSession = useCallback(async (): Promise<void> => {
try {
Expand Down
18 changes: 9 additions & 9 deletions one-client-nextjs/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export interface NextConfig extends BaseConfig {
*
* ### 1. Environment Variables
*
* The simplest way to use the SDK is to use the named exports ({@link HandleAuth}, {@link HandleLogin},
* The simplest way to use the SDK is to use the named exports ({@link HandleAccess}, {@link HandleLogin},
* {@link HandleLogout}, {@link HandleCallback}, {@link HandleProfile}, {@link GetSession}, {@link GetAccessToken},
* {@link WithApiAuthRequired}, and {@link WithPageAuthRequired}).
*
* ```js
* // pages/api/auth/[eartho].js
* import { handleAuth } from '@eartho/one-client-nextjs';
* // pages/api/access/[eartho].js
* import { handleAccess } from '@eartho/one-client-nextjs';
*
* return handleAuth();
* return handleAccess();
* ```
*
* When you use these named exports, an instance of the SDK is created for you which you can configure using
Expand Down Expand Up @@ -101,15 +101,15 @@ export interface NextConfig extends BaseConfig {
* Then import it into your route handler:
*
* ```js
* // pages/api/auth/[eartho].js
* // pages/api/access/[eartho].js
* import eartho from '../../../../utils/eartho';
*
* export default eartho.handleAuth();
* export default eartho.handleAccess();
* ```
*
* **IMPORTANT** If you use {@link InitEartho}, you should *not* use the other named exports as they will use a different
* instance of the SDK. Also note - this is for the server side part of the SDK - you will always use named exports for
* the front end components: {@link EarthoProvider}, {@link UseUser} and the
* the front end components: {@link EarthoClientProvider}, {@link UseUser} and the
* front end version of {@link WithPageAuthRequired}
*
* @category Server
Expand Down Expand Up @@ -148,7 +148,7 @@ export const getConfig = (params: ConfigParameters = {}): NextConfig => {
// Don't use destructuring here so that the `DefinePlugin` can replace any env vars specified in `next.config.js`
const EARTHO_SECRET = process.env.EARTHO_SECRET;
const EARTHO_ISSUER_BASE_URL = 'https://one.eartho.io';
const EARTHO_BASE_URL = 'https://api.eartho.io';
const EARTHO_BASE_URL = process.env.EARTHO_BASE_URL;
const EARTHO_CLIENT_ID = process.env.EARTHO_CLIENT_ID;
const EARTHO_CLIENT_SECRET = process.env.EARTHO_CLIENT_SECRET;
const EARTHO_CLOCK_TOLERANCE = process.env.EARTHO_CLOCK_TOLERANCE;
Expand Down Expand Up @@ -237,7 +237,7 @@ export const getConfig = (params: ConfigParameters = {}): NextConfig => {
}
},
routes: {
callback: baseParams.routes?.callback || EARTHO_CALLBACK || '/api/auth/callback',
callback: baseParams.routes?.callback || EARTHO_CALLBACK || '/api/access/callback',
postLogoutRedirect: baseParams.routes?.postLogoutRedirect || EARTHO_POST_LOGOUT_REDIRECT
},
clientAssertionSigningKey: EARTHO_CLIENT_ASSERTION_SIGNING_KEY,
Expand Down
2 changes: 1 addition & 1 deletion one-client-nextjs/src/eartho-session/client/edge-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export class EdgeClient extends AbstractClient {
(this.config.earthoLogout || (issuerUrl.hostname.match('\\.eartho\\.com$') && this.config.earthoLogout !== false))
) {
const { id_token_hint, post_logout_redirect_uri, ...extraParams } = parameters;
const earthoLogoutUrl: URL = new URL(urlJoin(as.issuer, '/v2/logout'));
const earthoLogoutUrl: URL = new URL(urlJoin(as.issuer, '/logout'));
post_logout_redirect_uri && earthoLogoutUrl.searchParams.set('returnTo', post_logout_redirect_uri);
earthoLogoutUrl.searchParams.set('client_id', this.config.clientID);
Object.entries(extraParams).forEach(([key, value]: [string, string]) => {
Expand Down
2 changes: 1 addition & 1 deletion one-client-nextjs/src/eartho-session/client/node-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class NodeClient extends AbstractClient {
Object.defineProperty(this.client, 'endSessionUrl', {
value(params: EndSessionParameters) {
const { id_token_hint, post_logout_redirect_uri, ...extraParams } = params;
const parsedUrl = new URL(urlJoin(issuer.metadata.issuer, '/v2/logout'));
const parsedUrl = new URL(urlJoin(issuer.metadata.issuer, '/logout'));
parsedUrl.searchParams.set('client_id', config.clientID);
post_logout_redirect_uri && parsedUrl.searchParams.set('returnTo', post_logout_redirect_uri);
Object.entries(extraParams).forEach(([key, value]) => {
Expand Down
Loading

0 comments on commit cfb94bf

Please sign in to comment.