Skip to content

feat(clerk-react): Add setSession to useSignIn, useSignUp, useSessionList #100

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

Merged
merged 1 commit into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions packages/react/src/hooks/useSessionList.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { SessionResource } from '@clerk/types';
import { SessionResource, SetSession } from '@clerk/types';

import { useClientContext } from '../contexts/ClientContext';
import { useIsomorphicClerkContext } from '../contexts/IsomorphicClerkContext';

type UseSessionListReturn =
| { isLoaded: false; sessions: undefined }
| { isLoaded: true; sessions: SessionResource[] };
| { isLoaded: false; sessions: undefined; setSession: undefined }
| { isLoaded: true; sessions: SessionResource[]; setSession: SetSession };

type UseSessionList = () => UseSessionListReturn;

export const useSessionList: UseSessionList = () => {
const isomorphicClerk = useIsomorphicClerkContext();
const client = useClientContext();

if (!client) {
return { isLoaded: false, sessions: undefined };
return { isLoaded: false, sessions: undefined, setSession: undefined };
}

return { isLoaded: true, sessions: client.sessions };
return { isLoaded: true, sessions: client.sessions, setSession: isomorphicClerk.setSession };
};
12 changes: 7 additions & 5 deletions packages/react/src/hooks/useSignIn.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { SignInResource } from '@clerk/types';
import { SetSession, SignInResource } from '@clerk/types';

import { useClientContext } from '../contexts/ClientContext';
import { useIsomorphicClerkContext } from '../contexts/IsomorphicClerkContext';

type UseSignInReturn =
| { isLoaded: false; signIn: undefined }
| { isLoaded: true; signIn: SignInResource };
| { isLoaded: false; signIn: undefined; setSession: undefined }
| { isLoaded: true; signIn: SignInResource; setSession: SetSession };

type UseSignIn = () => UseSignInReturn;

export const useSignIn: UseSignIn = () => {
const isomorphicClerk = useIsomorphicClerkContext();
const client = useClientContext();

if (!client) {
return { isLoaded: false, signIn: undefined };
return { isLoaded: false, signIn: undefined, setSession: undefined };
}

return { isLoaded: true, signIn: client.signIn };
return { isLoaded: true, signIn: client.signIn, setSession: isomorphicClerk.setSession };
};
12 changes: 7 additions & 5 deletions packages/react/src/hooks/useSignUp.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { SignUpResource } from '@clerk/types';
import { SetSession, SignUpResource } from '@clerk/types';

import { useClientContext } from '../contexts/ClientContext';
import { useIsomorphicClerkContext } from '../contexts/IsomorphicClerkContext';

type UseSignUpReturn =
| { isLoaded: false; signUp: undefined }
| { isLoaded: true; signUp: SignUpResource };
| { isLoaded: false; signUp: undefined; setSession: undefined }
| { isLoaded: true; signUp: SignUpResource; setSession: SetSession };

type UseSignUp = () => UseSignUpReturn;

export const useSignUp: UseSignUp = () => {
const isomorphicClerk = useIsomorphicClerkContext();
const client = useClientContext();

if (!client) {
return { isLoaded: false, signUp: undefined };
return { isLoaded: false, signUp: undefined, setSession: undefined };
}

return { isLoaded: true, signUp: client.signUp };
return { isLoaded: true, signUp: client.signUp, setSession: isomorphicClerk.setSession };
};
13 changes: 7 additions & 6 deletions packages/types/src/clerk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { OrganizationMembershipResource } from '.';
import { EnvironmentResource } from '.';
import { EnvironmentResource, OrganizationMembershipResource } from '.';
import { ClientResource } from './client';
import { DisplayThemeJSON } from './json';
import { OrganizationResource } from './organization';
Expand All @@ -15,6 +14,11 @@ export type BeforeEmitCallback = (
) => void | Promise<any>;
export type SignOutCallback = () => void | Promise<any>;

export type SetSession = (
session: ActiveSessionResource | string | null,
beforeEmit?: BeforeEmitCallback,
) => Promise<void>;

/**
* Main Clerk SDK object.
*/
Expand Down Expand Up @@ -167,10 +171,7 @@ export interface Clerk {
* @param session Passed session resource object, session id (string version) or null
* @param beforeEmit Callback run just before the active session is set to the passed object. Can be used to hook up for pre-navigation actions.
*/
setSession: (
session: ActiveSessionResource | string | null,
beforeEmit?: BeforeEmitCallback,
) => Promise<void>;
setSession: SetSession;

/**
* Function used to commit a navigation after certain steps in the Clerk processes.
Expand Down