Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
Base headers (#20)
Browse files Browse the repository at this point in the history
* base headers

* commit file

* bump-react

* bump-react
  • Loading branch information
asafshen authored Mar 24, 2024
1 parent 8a14c4b commit de35c17
Showing 7 changed files with 34 additions and 8 deletions.
3 changes: 0 additions & 3 deletions src/server/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Replaced in build time
declare const BUILD_VERSION: string;

export const DESCOPE_SESSION_HEADER = 'x-descope-session';

export const baseHeaders = {
2 changes: 1 addition & 1 deletion src/server/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import descopeSdk from '@descope/node-sdk';
import { baseHeaders } from './constants';
import { baseHeaders } from '../shared/constants';

type Sdk = ReturnType<typeof descopeSdk>;
type CreateSdkParams = Omit<Parameters<typeof descopeSdk>[0], 'projectId'> & {
8 changes: 7 additions & 1 deletion src/shared/AuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
'use client';

import React, { ComponentProps } from 'react';
import {
AuthProvider as AuthProviderComp,
baseHeaders
} from '@descope/react-sdk';
import { baseHeaders as nextBaseHeaders } from './constants';

import { AuthProvider as AuthProviderComp } from '@descope/react-sdk';
// Override baseHeaders
Object.assign(baseHeaders, nextBaseHeaders);

const AuthProvider = (props: ComponentProps<typeof AuthProviderComp>) => (
// by default we use sessionTokenViaCookie, so middleware will work out of the box
5 changes: 5 additions & 0 deletions src/shared/DescopeFlows.tsx
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import {
SignUpFlow as SignUpFlowWC,
SignUpOrInFlow as SignUpOrInFlowWC
} from '@descope/react-sdk';
import { baseHeaders as nextBaseHeaders } from './constants';

type DescopeWCProps = ComponentProps<typeof DescopeWC>;
type SignInFlowProps = ComponentProps<typeof SignInFlowWC>;
@@ -36,6 +37,10 @@ const dynamicDescopeComponent = <
dynamic<ComponentProps<T> & AdditionalProps>(
async () => {
const DescopeComponents = await import('@descope/react-sdk');

// Override baseHeaders
Object.assign(DescopeComponents.baseHeaders, nextBaseHeaders);

const Component = DescopeComponents[componentName];
return ({
redirectAfterSuccess = '',
8 changes: 8 additions & 0 deletions src/shared/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Replaced in build time
declare const BUILD_VERSION: string;

// eslint-disable-next-line import/prefer-default-export
export const baseHeaders = {
'x-descope-sdk-name': 'nextjs',
'x-descope-sdk-version': BUILD_VERSION
};
2 changes: 1 addition & 1 deletion test/server/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import descopeSdk from '@descope/node-sdk';
import { baseHeaders } from '../../src/server/constants';
import { baseHeaders } from '../../src/shared/constants';
import { createSdk, getGlobalSdk } from '../../src/server/sdk';

jest.mock('@descope/node-sdk', () => jest.fn());
14 changes: 12 additions & 2 deletions test/shared/AuthProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import React from 'react';
import '@testing-library/jest-dom';
import { render } from '@testing-library/react';
import { AuthProvider as AuthProviderComp } from '@descope/react-sdk';
import {
AuthProvider as AuthProviderComp,
baseHeaders
} from '@descope/react-sdk';
import AuthProvider from '../../src/shared/AuthProvider';
import { baseHeaders as nextBaseHeaders } from '../../src/shared/constants';

jest.mock('@descope/react-sdk', () => ({
AuthProvider: jest.fn()
AuthProvider: jest.fn(),
baseHeaders: {}
}));

describe('AuthProvider', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should set base headers', () => {
render(<AuthProvider projectId="project1" />);
expect(baseHeaders).toEqual(nextBaseHeaders);
});

it('should render and pass sessionTokenViaCookie as true by default', () => {
render(<AuthProvider projectId="project1" />);
expect(AuthProviderComp).toHaveBeenCalledWith(

0 comments on commit de35c17

Please sign in to comment.