|
@@ -30,10 +30,20 @@ function normalizeEmailOrNull(value) {
|
|
|
return s.toLowerCase();
|
|
return s.toLowerCase();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+function normalizeMustChangePassword(value) {
|
|
|
|
|
+ return value === true;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Create a signed session JWT and store it in a HTTP-only cookie.
|
|
* Create a signed session JWT and store it in a HTTP-only cookie.
|
|
|
*/
|
|
*/
|
|
|
-export async function createSession({ userId, role, branchId, email }) {
|
|
|
|
|
|
|
+export async function createSession({
|
|
|
|
|
+ userId,
|
|
|
|
|
+ role,
|
|
|
|
|
+ branchId,
|
|
|
|
|
+ email,
|
|
|
|
|
+ mustChangePassword,
|
|
|
|
|
+}) {
|
|
|
if (!userId || !role) {
|
|
if (!userId || !role) {
|
|
|
throw new Error("createSession requires userId and role");
|
|
throw new Error("createSession requires userId and role");
|
|
|
}
|
|
}
|
|
@@ -43,6 +53,7 @@ export async function createSession({ userId, role, branchId, email }) {
|
|
|
role,
|
|
role,
|
|
|
branchId: branchId ?? null,
|
|
branchId: branchId ?? null,
|
|
|
email: normalizeEmailOrNull(email),
|
|
email: normalizeEmailOrNull(email),
|
|
|
|
|
+ mustChangePassword: normalizeMustChangePassword(mustChangePassword),
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
const jwt = await new SignJWT(payload)
|
|
const jwt = await new SignJWT(payload)
|
|
@@ -80,7 +91,7 @@ export async function getSession() {
|
|
|
try {
|
|
try {
|
|
|
const { payload } = await jwtVerify(cookie.value, secretKey);
|
|
const { payload } = await jwtVerify(cookie.value, secretKey);
|
|
|
|
|
|
|
|
- const { userId, role, branchId, email } = payload;
|
|
|
|
|
|
|
+ const { userId, role, branchId, email, mustChangePassword } = payload;
|
|
|
|
|
|
|
|
if (typeof userId !== "string" || typeof role !== "string") {
|
|
if (typeof userId !== "string" || typeof role !== "string") {
|
|
|
return null;
|
|
return null;
|
|
@@ -91,6 +102,7 @@ export async function getSession() {
|
|
|
role,
|
|
role,
|
|
|
branchId: typeof branchId === "string" ? branchId : null,
|
|
branchId: typeof branchId === "string" ? branchId : null,
|
|
|
email: typeof email === "string" ? email : null,
|
|
email: typeof email === "string" ? email : null,
|
|
|
|
|
+ mustChangePassword: normalizeMustChangePassword(mustChangePassword),
|
|
|
};
|
|
};
|
|
|
} catch {
|
|
} catch {
|
|
|
const store = await cookies();
|
|
const store = await cookies();
|