added persisting tokenID after password changes on changing session

This commit is contained in:
z1glr
2025-01-11 17:31:18 +00:00
parent b8ede7ef36
commit 4f86dce569
5 changed files with 48 additions and 35 deletions

View File

@@ -70,10 +70,10 @@ type UserChangePassword struct {
Password string `json:"password" validate:"required,min=12"` Password string `json:"password" validate:"required,min=12"`
} }
func ChangePassword(user UserChangePassword) error { func ChangePassword(user UserChangePassword) (string, error) {
// try to hash teh password // try to hash teh password
if hash, err := hashPassword(user.Password); err != nil { if hash, err := hashPassword(user.Password); err != nil {
return err return "", err
} else { } else {
execStruct := struct { execStruct := struct {
UserName string `db:"userName"` UserName string `db:"userName"`
@@ -86,11 +86,11 @@ func ChangePassword(user UserChangePassword) error {
} }
if _, err := db.DB.NamedExec("UPDATE USERS SET tokenID = :tokenID, password = :password WHERE name = :userName", execStruct); err != nil { if _, err := db.DB.NamedExec("UPDATE USERS SET tokenID = :tokenID, password = :password WHERE name = :userName", execStruct); err != nil {
return err return "", err
} else { } else {
refresh() refresh()
return nil return execStruct.TokenID, nil
} }
} }
} }

View File

@@ -51,10 +51,24 @@ func patchPassword(args HandlerArgs) responseMessage {
response.Status = fiber.StatusBadRequest response.Status = fiber.StatusBadRequest
logger.Info().Msgf("invalid body: %v", err) logger.Info().Msgf("invalid body: %v", err)
} else if err := users.ChangePassword(body); err != nil { } else if tokenID, err := users.ChangePassword(body); err != nil {
response.Status = fiber.StatusInternalServerError response.Status = fiber.StatusInternalServerError
logger.Error().Msgf("can't update password: %v", err) logger.Error().Msgf("can't update password: %v", err)
// sign a new JWT with the new tokenID
} else if jwt, err := config.SignJWT(JWTPayload{
UserName: body.UserName,
TokenID: tokenID,
// if something failed, remove the current session-cookie
}); err != nil {
removeSessionCookie(args.C)
// set the new session-cookie
} else {
// update the token in the session-cookie
setSessionCookie(args.C, &jwt)
} }
} }

View File

@@ -4,16 +4,10 @@ import { create } from "zustand";
import { persist } from "zustand/middleware"; import { persist } from "zustand/middleware";
import { apiCall } from "./lib"; import { apiCall } from "./lib";
export type Task = string;
export type Availability = string;
export const Availabilities: Availability[] = ["yes", "maybe", "no"];
export interface EventData { export interface EventData {
id: number; id: number;
date: string; date: string;
tasks: Partial<Record<Task, string | null>>; tasks: Partial<Record<string, string | null>>;
description: string; description: string;
} }
@@ -70,21 +64,4 @@ const zustand = create<Zustand>()(
), ),
); );
export async function getTasks(): Promise<
Record<number, { text: string; disabled: boolean }>
> {
const result = await apiCall<{ text: string; disabled: boolean }[]>(
"GET",
"tasks",
);
if (result.ok) {
const tasks = await result.json();
return tasks;
} else {
return [];
}
}
export default zustand; export default zustand;

View File

@@ -1,6 +1,6 @@
import { useEffect, useReducer } from "react"; import { useEffect, useReducer, useState } from "react";
import { Add } from "@carbon/icons-react"; import { Add } from "@carbon/icons-react";
import zustand, { getTasks, Task } from "../../Zustand"; import zustand from "../../Zustand";
import { getLocalTimeZone, now, ZonedDateTime } from "@internationalized/date"; import { getLocalTimeZone, now, ZonedDateTime } from "@internationalized/date";
import { import {
Button, Button,
@@ -16,12 +16,12 @@ import {
Spinner, Spinner,
Textarea, Textarea,
} from "@nextui-org/react"; } from "@nextui-org/react";
import { apiCall } from "@/lib"; import { apiCall, getTasks, Task } from "@/lib";
interface state { interface state {
date: ZonedDateTime; date: ZonedDateTime;
description: string; description: string;
tasks: Task[]; tasks: string[];
} }
interface dispatchAction { interface dispatchAction {
@@ -50,11 +50,13 @@ export default function AddEvent(props: {
} }
} }
const [state, dispatchState] = useReducer(reducer, initialState); const [state, dispatchState] = useReducer(reducer, initialState);
const tasks = zustand((state) => state.tasks); const [tasks, setTasks] = useState<Record<number, Task>>({});
// get the available tasks // get the available tasks
useEffect(() => { useEffect(() => {
void getTasks(); (async () => {
setTasks(await getTasks());
})();
}, []); }, []);
// sends the addEvent request to the backend // sends the addEvent request to the backend

View File

@@ -90,3 +90,23 @@ export function vaidatePassword(password: string): string[] {
return errors; return errors;
} }
export interface Task {
text: string;
disabled: boolean;
}
export async function getTasks(): Promise<Record<number, Task>> {
const result = await apiCall<{ text: string; disabled: boolean }[]>(
"GET",
"tasks",
);
if (result.ok) {
const tasks = await result.json();
return tasks;
} else {
return [];
}
}