password error, unique loginName

This commit is contained in:
Max Peintner
2023-05-23 09:22:15 +02:00
parent b27c8af65f
commit 5f3696400c
4 changed files with 59 additions and 59 deletions

View File

@@ -2,25 +2,18 @@ import { listSessions, server } from "#/lib/zitadel";
import Alert from "#/ui/Alert";
import { Avatar } from "#/ui/Avatar";
import { getAllSessionIds } from "#/utils/cookies";
import {
ExclamationTriangleIcon,
XCircleIcon,
} from "@heroicons/react/24/outline";
import { UserPlusIcon, XCircleIcon } from "@heroicons/react/24/outline";
import moment from "moment";
import Link from "next/link";
async function loadSessions() {
const ids = await getAllSessionIds().catch((error) => {
console.log("err", error);
});
const ids = await getAllSessionIds();
if (ids && ids.length) {
return listSessions(
server,
ids.filter((id: string | undefined) => !!id)
).then((sessions) => {
return sessions;
});
);
} else {
return [];
}
@@ -38,7 +31,6 @@ export default async function Page() {
{sessions ? (
sessions.map((session: any, index: number) => {
const validPassword = session.factors.password?.verifiedAt;
console.log(session);
return (
<Link
key={"session-" + index}
@@ -91,6 +83,14 @@ export default async function Page() {
) : (
<Alert>No Sessions available!</Alert>
)}
<Link href="/username">
<div className="flex flex-row items-center py-3 px-4 hover:bg-black/10 dark:hover:bg-white/10 rounded-md">
<div className="w-8 h-8 mr-4 flex flex-row justify-center items-center rounded-full bg-black/5 dark:bg-white/5">
<UserPlusIcon className="h-5 w-5" />
</div>
<span className="text-sm">Add another account</span>
</div>
</Link>
</div>
</div>
);

View File

@@ -45,35 +45,42 @@ export async function PUT(request: NextRequest) {
const { password } = body;
const recent = await getMostRecentSessionCookie();
const session = await setSession(server, recent.id, recent.token, password);
const sessionCookie: SessionCookie = {
id: recent.id,
token: session.sessionToken,
changeDate: session.details.changeDate,
loginName: recent.loginName,
};
return getSession(server, sessionCookie.id, sessionCookie.token).then(
({ session }) => {
const newCookie: SessionCookie = {
id: sessionCookie.id,
token: sessionCookie.token,
changeDate: session.changeDate,
loginName: session.factors.user.loginName,
return setSession(server, recent.id, recent.token, password)
.then((session) => {
const sessionCookie: SessionCookie = {
id: recent.id,
token: session.sessionToken,
changeDate: session.details.changeDate,
loginName: recent.loginName,
};
return updateSessionCookie(sessionCookie.id, sessionCookie)
.then(() => {
console.log("updatedRecent:", sessionCookie);
return NextResponse.json({ factors: session.factors });
})
.catch((error) => {
console.error("errr", error);
return NextResponse.json(error, { status: 500 });
});
}
);
return getSession(server, sessionCookie.id, sessionCookie.token).then(
({ session }) => {
const newCookie: SessionCookie = {
id: sessionCookie.id,
token: sessionCookie.token,
changeDate: session.changeDate,
loginName: session.factors.user.loginName,
};
return updateSessionCookie(sessionCookie.id, newCookie)
.then(() => {
return NextResponse.json({ factors: session.factors });
})
.catch((error) => {
return NextResponse.json(
{ details: "could not set cookie" },
{ status: 500 }
);
});
}
);
})
.catch((error) => {
console.error("erasd", error);
return NextResponse.json(error, { status: 500 });
});
} else {
return NextResponse.error();
}

View File

@@ -22,24 +22,29 @@ async function set(sessions: SessionCookie[]) {
export async function addSessionToCookie(session: SessionCookie): Promise<any> {
const cookiesList = cookies();
// const hasSessions = cookiesList.has("sessions");
// if (hasSessions) {
const stringifiedCookie = cookiesList.get("sessions");
const currentSessions: SessionCookie[] = stringifiedCookie?.value
let currentSessions: SessionCookie[] = stringifiedCookie?.value
? JSON.parse(stringifiedCookie?.value)
: [];
const index = currentSessions.findIndex(
(s) => s.loginName === session.loginName
);
if (index > -1) {
currentSessions[index] = session;
} else {
currentSessions = [...currentSessions, session];
}
// @ts-ignore
return cookiesList.set({
name: "sessions",
value: JSON.stringify([...currentSessions, session]),
value: JSON.stringify(currentSessions),
httpOnly: true,
path: "/",
});
// } else {
// return set([session]);
// }
}
export async function updateSessionCookie(
@@ -47,8 +52,7 @@ export async function updateSessionCookie(
session: SessionCookie
): Promise<any> {
const cookiesList = cookies();
// const hasSessions = cookiesList.has("sessions");
// if (hasSessions) {
const stringifiedCookie = cookiesList.get("sessions");
const sessions: SessionCookie[] = stringifiedCookie?.value
@@ -65,17 +69,12 @@ export async function updateSessionCookie(
httpOnly: true,
path: "/",
});
// } else {
// return Promise.reject();
// }
}
export async function removeSessionFromCookie(
session: SessionCookie
): Promise<any> {
const cookiesList = cookies();
// const hasSessions = cookiesList.has("sessions");
// if (hasSessions) {
const stringifiedCookie = cookiesList.get("sessions");
const sessions: SessionCookie[] = stringifiedCookie?.value
@@ -88,14 +87,11 @@ export async function removeSessionFromCookie(
// @ts-ignore
return cookiesList.set({
name: "__Secure-sessions",
name: "sessions",
value: JSON.stringify(filteredSessions),
httpOnly: true,
path: "/",
});
// } else {
// return Promise.reject();
// }
}
export async function getMostRecentSessionCookie(): Promise<any> {
@@ -105,7 +101,6 @@ export async function getMostRecentSessionCookie(): Promise<any> {
if (stringifiedCookie?.value) {
const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value);
console.log(sessions);
const latest = sessions.reduce((prev, current) => {
return new Date(prev.changeDate).getTime() >
new Date(current.changeDate).getTime()
@@ -147,12 +142,9 @@ export async function getMostRecentCookieWithLoginname(
const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value);
const filtered = sessions.filter((cookie) => {
console.log(!!loginName);
return !!loginName ? cookie.loginName === loginName : true;
});
console.log(filtered);
const latest =
filtered && filtered.length
? filtered.reduce((prev, current) => {