fix(task7): invalidate tokens on password change or reset
This commit is contained in:
@@ -26,8 +26,14 @@ export function fetchMeApi() {
|
||||
return api.get<UserOut>("/auth/me");
|
||||
}
|
||||
|
||||
export interface ChangePasswordResponse {
|
||||
message: string;
|
||||
access_token?: string;
|
||||
token_type?: string;
|
||||
}
|
||||
|
||||
export function changePasswordApi(current_password: string, new_password: string) {
|
||||
return api.post<{ message: string }>("/auth/change-password", {
|
||||
return api.post<ChangePasswordResponse>("/auth/change-password", {
|
||||
current_password,
|
||||
new_password,
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@ import { ApiError } from "@/api/client";
|
||||
import { Modal } from "@/components/ui/dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { toast } from "@/lib/toast";
|
||||
import { useAuthStore } from "@/hooks/useAuth";
|
||||
|
||||
interface ChangePasswordModalProps {
|
||||
open: boolean;
|
||||
@@ -55,6 +56,9 @@ export function ChangePasswordModal({ open, onClose }: ChangePasswordModalProps)
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await changePasswordApi(currentPassword, newPassword);
|
||||
if (res.access_token) {
|
||||
useAuthStore.getState().setToken(res.access_token);
|
||||
}
|
||||
toast.success(res.message || "Password changed successfully.");
|
||||
handleClose();
|
||||
} catch (err) {
|
||||
|
||||
@@ -8,6 +8,7 @@ interface AuthState {
|
||||
demoAsOfDate: string | null;
|
||||
hydrating: boolean;
|
||||
setAuth: (user: UserOut, token: string) => void;
|
||||
setToken: (token: string) => void;
|
||||
setDemoAsOfDate: (date: string | null) => void;
|
||||
hydrate: () => Promise<void>;
|
||||
logout: () => void;
|
||||
@@ -31,6 +32,11 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
});
|
||||
},
|
||||
|
||||
setToken: (token) => {
|
||||
sessionStorage.setItem("access_token", token);
|
||||
set({ token });
|
||||
},
|
||||
|
||||
setDemoAsOfDate: (date) => {
|
||||
set((state) => ({
|
||||
demoAsOfDate: date || null,
|
||||
|
||||
Reference in New Issue
Block a user