mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-12 09:54:00 +00:00
auth methods
This commit is contained in:
@@ -22,6 +22,8 @@ async function loadSessions(): Promise<Session[]> {
|
|||||||
|
|
||||||
export default async function Page() {
|
export default async function Page() {
|
||||||
let sessions = await loadSessions();
|
let sessions = await loadSessions();
|
||||||
|
|
||||||
|
console.log(sessions);
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col items-center space-y-4">
|
<div className="flex flex-col items-center space-y-4">
|
||||||
<h1>Accounts</h1>
|
<h1>Accounts</h1>
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ export default async function Page({
|
|||||||
return response.session;
|
return response.session;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log(recent);
|
||||||
}
|
}
|
||||||
const title = !!prompt
|
const title = !!prompt
|
||||||
? "Authenticate with a passkey"
|
? "Authenticate with a passkey"
|
||||||
|
|||||||
114
apps/login/app/methods/route.ts
Normal file
114
apps/login/app/methods/route.ts
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
import {
|
||||||
|
createSession,
|
||||||
|
getSession,
|
||||||
|
listAuthenticationMethodTypes,
|
||||||
|
server,
|
||||||
|
} from "#/lib/zitadel";
|
||||||
|
import {
|
||||||
|
SessionCookie,
|
||||||
|
addSessionToCookie,
|
||||||
|
getSessionCookieById,
|
||||||
|
} from "#/utils/cookies";
|
||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
|
export async function GET(request: NextRequest) {
|
||||||
|
const { searchParams } = new URL(request.url);
|
||||||
|
const sessionId = searchParams.get("sessionId");
|
||||||
|
if (sessionId) {
|
||||||
|
const sessionCookie = await getSessionCookieById(sessionId);
|
||||||
|
|
||||||
|
const session = await getSession(
|
||||||
|
server,
|
||||||
|
sessionCookie.id,
|
||||||
|
sessionCookie.token
|
||||||
|
);
|
||||||
|
|
||||||
|
const userId = session?.session?.factors?.user?.id;
|
||||||
|
|
||||||
|
if (userId) {
|
||||||
|
return listAuthenticationMethodTypes(userId)
|
||||||
|
.then((methods) => {
|
||||||
|
return NextResponse.json(methods);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
return NextResponse.json(error, { status: 500 });
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ details: "could not get session" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return NextResponse.json({}, { status: 400 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function POST(request: NextRequest) {
|
||||||
|
const body = await request.json();
|
||||||
|
if (body) {
|
||||||
|
const { loginName } = body;
|
||||||
|
|
||||||
|
const domain: string = request.nextUrl.hostname;
|
||||||
|
|
||||||
|
const createdSession = await createSession(
|
||||||
|
server,
|
||||||
|
loginName,
|
||||||
|
undefined,
|
||||||
|
domain
|
||||||
|
);
|
||||||
|
|
||||||
|
if (createdSession) {
|
||||||
|
return getSession(
|
||||||
|
server,
|
||||||
|
createdSession.sessionId,
|
||||||
|
createdSession.sessionToken
|
||||||
|
).then((response) => {
|
||||||
|
if (response?.session && response.session?.factors?.user?.loginName) {
|
||||||
|
const userId = response?.session?.factors?.user?.id;
|
||||||
|
|
||||||
|
const sessionCookie: SessionCookie = {
|
||||||
|
id: createdSession.sessionId,
|
||||||
|
token: createdSession.sessionToken,
|
||||||
|
changeDate: response.session.changeDate?.toString() ?? "",
|
||||||
|
loginName: response.session?.factors?.user?.loginName ?? "",
|
||||||
|
};
|
||||||
|
return addSessionToCookie(sessionCookie)
|
||||||
|
.then(() => {
|
||||||
|
return listAuthenticationMethodTypes(userId)
|
||||||
|
.then((methods) => {
|
||||||
|
return NextResponse.json({
|
||||||
|
authMethodTypes: methods.authMethodTypes,
|
||||||
|
sessionId: createdSession.sessionId,
|
||||||
|
factors: response?.session?.factors,
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
return NextResponse.json(error, { status: 500 });
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
details: "could not add session to cookie",
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
details:
|
||||||
|
"could not get session or session does not have loginName",
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return NextResponse.error();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return NextResponse.error();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -265,4 +265,19 @@ export async function registerPasskey(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param server
|
||||||
|
* @param userId the id of the user where the email should be set
|
||||||
|
* @returns the newly set email
|
||||||
|
*/
|
||||||
|
export async function listAuthenticationMethodTypes(
|
||||||
|
userId: string
|
||||||
|
): Promise<any> {
|
||||||
|
const userservice = user.getUser(server);
|
||||||
|
return userservice.listAuthenticationMethodTypes({
|
||||||
|
userId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export { server };
|
export { server };
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ import { TextInput } from "./Input";
|
|||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { Spinner } from "./Spinner";
|
import { Spinner } from "./Spinner";
|
||||||
|
import {
|
||||||
|
ListAuthenticationMethodTypesResponse,
|
||||||
|
AuthenticationMethodType,
|
||||||
|
} from "@zitadel/server";
|
||||||
|
|
||||||
type Inputs = {
|
type Inputs = {
|
||||||
loginName: string;
|
loginName: string;
|
||||||
@@ -18,11 +22,11 @@ export default function UsernameForm() {
|
|||||||
|
|
||||||
const [loading, setLoading] = useState<boolean>(false);
|
const [loading, setLoading] = useState<boolean>(false);
|
||||||
|
|
||||||
const router = useRouter();
|
async function submitUsernameAndGetAuthenticationMethods(
|
||||||
|
values: Inputs
|
||||||
async function submitUsername(values: Inputs) {
|
): Promise<ListAuthenticationMethodTypesResponse> {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
const res = await fetch("/session", {
|
const res = await fetch("/methods", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -34,18 +38,20 @@ export default function UsernameForm() {
|
|||||||
|
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
throw new Error("Failed to set user");
|
throw new Error("Failed to load authentication methods");
|
||||||
}
|
}
|
||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
function submitUsernameAndContinue(value: Inputs): Promise<boolean | void> {
|
function submitUsernameAndContinue(value: Inputs): Promise<boolean | void> {
|
||||||
return submitUsername(value).then(({ factors }) => {
|
return submitUsernameAndGetAuthenticationMethods(value).then(
|
||||||
return router.push(
|
({ factors, sessionId, authMethodTypes }) => {
|
||||||
`/password?` +
|
console.log(factors, sessionId, authMethodTypes);
|
||||||
new URLSearchParams({ loginName: `${factors.user.loginName}` })
|
if (authMethodTypes.length === 1) {
|
||||||
);
|
} else {
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { errors } = formState;
|
const { errors } = formState;
|
||||||
|
|||||||
@@ -35,6 +35,9 @@ export {
|
|||||||
RegisterPasskeyResponse,
|
RegisterPasskeyResponse,
|
||||||
CreatePasskeyRegistrationLinkResponse,
|
CreatePasskeyRegistrationLinkResponse,
|
||||||
CreatePasskeyRegistrationLinkRequest,
|
CreatePasskeyRegistrationLinkRequest,
|
||||||
|
ListAuthenticationMethodTypesResponse,
|
||||||
|
ListAuthenticationMethodTypesRequest,
|
||||||
|
AuthenticationMethodType,
|
||||||
} from "./proto/server/zitadel/user/v2alpha/user_service";
|
} from "./proto/server/zitadel/user/v2alpha/user_service";
|
||||||
export {
|
export {
|
||||||
SetHumanPasswordResponse,
|
SetHumanPasswordResponse,
|
||||||
|
|||||||
Reference in New Issue
Block a user