fix search params

This commit is contained in:
Max Peintner
2023-05-19 10:13:05 +02:00
parent 99f66af644
commit d08abbfaa9
8 changed files with 37 additions and 19 deletions

View File

@@ -4,17 +4,22 @@ import UserAvatar from "#/ui/UserAvatar";
import { getMostRecentCookieWithLoginname } from "#/utils/cookies"; import { getMostRecentCookieWithLoginname } from "#/utils/cookies";
async function loadSession(loginName: string) { async function loadSession(loginName: string) {
const recent = await getMostRecentCookieWithLoginname(loginName); try {
const recent = await getMostRecentCookieWithLoginname(`${loginName}`);
return getSession(server, recent.id, recent.token).then(({ session }) => { return getSession(server, recent.id, recent.token).then(({ session }) => {
console.log("ss", session); console.log("ss", session);
return session; return session;
}); });
} catch (error) {
throw new Error("Session could not be loaded!");
}
} }
export default async function Page({ searchParams }: { searchParams: any }) { export default async function Page({ searchParams }: { searchParams: any }) {
const { loginName } = searchParams; const { loginName } = searchParams;
console.log(loginName);
const sessionFactors = await loadSession(loginName); const sessionFactors = await loadSession(loginName);
return ( return (

View File

@@ -2,7 +2,6 @@ import { createSession, getSession, server, setSession } from "#/lib/zitadel";
import { import {
SessionCookie, SessionCookie,
addSessionToCookie, addSessionToCookie,
getMostRecentCookieWithLoginname,
getMostRecentSessionCookie, getMostRecentSessionCookie,
updateSessionCookie, updateSessionCookie,
} from "#/utils/cookies"; } from "#/utils/cookies";

View File

@@ -136,7 +136,7 @@ export function addHumanUser(
return mgmt return mgmt
.addHumanUser( .addHumanUser(
{ {
email: { email, isVerified: false }, email: { email },
username: email, username: email,
profile: { firstName, lastName }, profile: { firstName, lastName },
password: { password }, password: { password },

View File

@@ -72,7 +72,7 @@ export default function RegisterForm({
function submitAndLink(value: Inputs): Promise<boolean | void> { function submitAndLink(value: Inputs): Promise<boolean | void> {
return submitRegister(value).then((resp: any) => { return submitRegister(value).then((resp: any) => {
return router.push(`/register/success?userid=${resp.userId}`); return router.push(`/verify?userID=${resp.userId}`);
}); });
} }

View File

@@ -44,7 +44,10 @@ export default function UsernameForm() {
function submitUsernameAndContinue(value: Inputs): Promise<boolean | void> { function submitUsernameAndContinue(value: Inputs): Promise<boolean | void> {
return submitUsername(value).then(({ factors }) => { return submitUsername(value).then(({ factors }) => {
console.log(factors); console.log(factors);
return router.push(`/password?loginName=${factors.user.loginName}`); return router.push(
`/password?` +
new URLSearchParams({ loginName: `${factors.user.loginName}` })
);
}); });
} }

View File

@@ -28,7 +28,7 @@ export default function VerifyEmailForm({ userId }: Props) {
async function submitCode(values: Inputs) { async function submitCode(values: Inputs) {
setLoading(true); setLoading(true);
const res = await fetch("/email/verify", { const res = await fetch("/verifyemail", {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",

View File

@@ -146,16 +146,27 @@ export async function getMostRecentCookieWithLoginname(
if (stringifiedCookie?.value) { if (stringifiedCookie?.value) {
const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value); const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value);
const latest = sessions console.log("sess", sessions);
.filter((cookie) => (loginName ? cookie.loginName === loginName : true)) const filtered = sessions.filter((cookie) => {
.reduce((prev, current) => { console.log("filtered", `${cookie.loginName}`, loginName?.toString());
return new Date(prev.changeDate).getTime() > return !!loginName ? cookie.loginName === loginName : true;
new Date(current.changeDate).getTime() });
? prev
: current;
});
return latest; const latest =
filtered && filtered.length
? filtered.reduce((prev, current) => {
return new Date(prev.changeDate).getTime() >
new Date(current.changeDate).getTime()
? prev
: current;
})
: undefined;
if (latest) {
return latest;
} else {
return Promise.reject();
}
} else { } else {
return Promise.reject(); return Promise.reject();
} }