Skip to content

Commit

Permalink
packages/js: next-auth 0.1.11
Browse files Browse the repository at this point in the history
  • Loading branch information
merlindru committed Dec 5, 2023
1 parent 38b6365 commit f489512
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
41 changes: 34 additions & 7 deletions packages/js/passkeys-next-auth-provider/client.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import { get } from "@github/webauthn-json";
import { type CredentialRequestOptionsJSON, get } from "@github/webauthn-json";
import { type JWTPayload } from "jose";
import { signIn } from "next-auth/react";
import { DEFAULT_PROVIDER_ID } from ".";

const headers = { "Content-Type": "application/json" };

/**
* Sign in with a passkey. Requires `PasskeyProvider` to be configured in `pages/api/auth/[...nextauth].ts`
*/
export async function signInWithPasskey(config: {
interface Common {
mediation?: CredentialRequestOptionsJSON["mediation"];
signal?: AbortSignal;
}

interface SignInConfig extends Common {
tenantId: string;

baseUrl?: string;
provider?: string;
callbackUrl?: string;
redirect?: boolean;
}) {
}

interface ClientFirstLoginConfig extends Common {
baseUrl?: string;
tenantId: string;
}

/**
* Sign in with a passkey. Requires `PasskeyProvider` to be configured in `pages/api/auth/[...nextauth].ts`
*/
export async function signInWithPasskey(config: SignInConfig) {
const finalizeJWT = await clientFirstPasskeyLogin(config);

await signIn(config.provider ?? DEFAULT_PROVIDER_ID, {
Expand All @@ -25,6 +37,13 @@ export async function signInWithPasskey(config: {
});
}

signInWithPasskey.autofill = async function (config: SignInConfig, signal?: AbortSignal) {
return signInWithPasskey({
...config,
mediation: "conditional",
});
};

/**
* You likely want to use {@link signInWithPasskey} instead.
*
Expand All @@ -33,14 +52,22 @@ export async function signInWithPasskey(config: {
* @returns a JWT that can be exchanged for a session on the backend.
* To verify the JWT, use the JWKS endpoint of the tenant. (`{tenantId}/.well-known/jwks.json`)
*/
export async function clientFirstPasskeyLogin(config: { baseUrl?: string; tenantId: string }): Promise<JWTPayload> {
export async function clientFirstPasskeyLogin(config: ClientFirstLoginConfig): Promise<JWTPayload> {
const baseUrl = config.baseUrl ?? "https://passkeys.hanko.io";

const loginOptions = await fetch(new URL(`${config.tenantId}/login/initialize`, baseUrl), {
method: "POST",
headers,
}).then((res) => res.json());

if (config.mediation) {
loginOptions.mediation = config.mediation;
}

if (config.signal) {
loginOptions.signal = config.signal;
}

// Open "select passkey" dialog
const credential = await get(loginOptions);

Expand Down
2 changes: 1 addition & 1 deletion packages/js/passkeys-next-auth-provider/dist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"license": "MIT",
"private": false,
"type": "module",
"version": "0.1.10",
"version": "0.1.11",
"exports": {
".": "./index.js",
"./client": "./client.js"
Expand Down

0 comments on commit f489512

Please sign in to comment.