feat: update login functionality to remove refresh token and adjust password requirements

This commit is contained in:
hamed
2026-06-20 13:22:09 +03:30
parent e2636ce743
commit 943784b64a
2 changed files with 13 additions and 13 deletions
+10 -6
View File
@@ -30,6 +30,7 @@ export default function LoginPage() {
const [forgotMobile, setForgotMobile] = useState(''); const [forgotMobile, setForgotMobile] = useState('');
const [forgotCode, setForgotCode] = useState(''); const [forgotCode, setForgotCode] = useState('');
const [forgotUuid, setForgotUuid] = useState(''); const [forgotUuid, setForgotUuid] = useState('');
const [forgotGrant, setForgotGrant] = useState('');
const [forgotPass, setForgotPass] = useState(''); const [forgotPass, setForgotPass] = useState('');
const [forgotPass2, setForgotPass2] = useState(''); const [forgotPass2, setForgotPass2] = useState('');
const [showNewPass, setShowNewPass] = useState(false); const [showNewPass, setShowNewPass] = useState(false);
@@ -71,7 +72,7 @@ export default function LoginPage() {
}); });
const json = await res.json(); const json = await res.json();
if (!res.ok) { toast.error(json?.errors?.[0]?.message ?? 'خطا در ورود'); return; } if (!res.ok) { toast.error(json?.errors?.[0]?.message ?? 'خطا در ورود'); return; }
login(json.access_token, json.refresh_token ?? ''); login(json.access_token);
toast.success('خوش آمدید'); toast.success('خوش آمدید');
} catch { toast.error('خطا در اتصال به سرور'); } } catch { toast.error('خطا در اتصال به سرور'); }
finally { setPwLoading(false); } finally { setPwLoading(false); }
@@ -111,14 +112,16 @@ export default function LoginPage() {
const vJson = await vRes.json(); const vJson = await vRes.json();
if (!vRes.ok) { toast.error(vJson?.errors?.[0]?.message ?? 'کد نادرست است'); return; } if (!vRes.ok) { toast.error(vJson?.errors?.[0]?.message ?? 'کد نادرست است'); return; }
const grant = vJson?.data?.grant;
const lRes = await fetch('/api/v1/user/otp-login', { const lRes = await fetch('/api/v1/user/otp-login', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ uuid: smsUuid }), body: JSON.stringify({ grant }),
}); });
const lJson = await lRes.json(); const lJson = await lRes.json();
if (!lRes.ok) { toast.error(lJson?.errors?.[0]?.message ?? 'کاربری با این شماره یافت نشد'); return; } if (!lRes.ok) { toast.error(lJson?.errors?.[0]?.message ?? 'کاربری با این شماره یافت نشد'); return; }
login(lJson.access_token, lJson.refresh_token ?? ''); login(lJson.access_token);
toast.success('خوش آمدید'); toast.success('خوش آمدید');
} catch { toast.error('خطا در اتصال به سرور'); } } catch { toast.error('خطا در اتصال به سرور'); }
finally { setSmsLoading(false); } finally { setSmsLoading(false); }
@@ -140,20 +143,21 @@ export default function LoginPage() {
}); });
const json = await res.json(); const json = await res.json();
if (!res.ok) { toast.error(json?.errors?.[0]?.message ?? 'کد نادرست است'); return; } if (!res.ok) { toast.error(json?.errors?.[0]?.message ?? 'کد نادرست است'); return; }
setForgotGrant(json?.data?.grant ?? '');
setForgotStep(3); setForgotStep(3);
} catch { toast.error('خطا در اتصال به سرور'); } } catch { toast.error('خطا در اتصال به سرور'); }
finally { setForgotLoading(false); } finally { setForgotLoading(false); }
}; };
const handleForgotReset = async () => { const handleForgotReset = async () => {
if (forgotPass.length < 6) { toast.error('رمز عبور باید حداقل ۶ کاراکتر باشد'); return; } if (forgotPass.length < 8) { toast.error('رمز عبور باید حداقل ۸ کاراکتر باشد'); return; }
if (forgotPass !== forgotPass2) { toast.error('رمزهای عبور یکسان نیستند'); return; } if (forgotPass !== forgotPass2) { toast.error('رمزهای عبور یکسان نیستند'); return; }
setForgotLoading(true); setForgotLoading(true);
try { try {
const res = await fetch('/api/v1/user/reset-password', { const res = await fetch('/api/v1/user/reset-password', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ uuid: forgotUuid, new_password: forgotPass }), body: JSON.stringify({ grant: forgotGrant, new_password: forgotPass }),
}); });
const json = await res.json(); const json = await res.json();
if (!res.ok) { toast.error(json?.errors?.[0]?.message ?? 'خطا در تغییر رمز'); return; } if (!res.ok) { toast.error(json?.errors?.[0]?.message ?? 'خطا در تغییر رمز'); return; }
@@ -343,7 +347,7 @@ export default function LoginPage() {
<label>رمز عبور جدید</label> <label>رمز عبور جدید</label>
<div style={{ position: 'relative' }}> <div style={{ position: 'relative' }}>
<input className="input" type={showNewPass ? 'text' : 'password'} <input className="input" type={showNewPass ? 'text' : 'password'}
placeholder="حداقل ۶ کاراکتر" style={{ paddingLeft: 44 }} placeholder="حداقل ۸ کاراکتر" style={{ paddingLeft: 44 }}
value={forgotPass} onChange={(e) => setForgotPass(e.target.value)} autoFocus /> value={forgotPass} onChange={(e) => setForgotPass(e.target.value)} autoFocus />
<button type="button" onClick={() => setShowNewPass((v) => !v)} style={{ <button type="button" onClick={() => setShowNewPass((v) => !v)} style={{
position: 'absolute', left: 12, top: '50%', transform: 'translateY(-50%)', position: 'absolute', left: 12, top: '50%', transform: 'translateY(-50%)',
+3 -7
View File
@@ -13,7 +13,6 @@ export interface ContextItem {
interface AuthState { interface AuthState {
token: string | null; token: string | null;
refreshToken: string | null;
isAuthenticated: boolean; isAuthenticated: boolean;
userUuid: string | null; userUuid: string | null;
userName: string | null; userName: string | null;
@@ -24,7 +23,7 @@ interface AuthState {
context: ContextItem | null; context: ContextItem | null;
availableContexts: ContextItem[]; availableContexts: ContextItem[];
login: (token: string, refreshToken: string) => void; login: (token: string) => void;
logout: () => void; logout: () => void;
fetchMe: () => Promise<void>; fetchMe: () => Promise<void>;
switchContext: (dbUuid: string) => Promise<void>; switchContext: (dbUuid: string) => Promise<void>;
@@ -57,7 +56,6 @@ export const useAuthStore = create<AuthState>()(
persist( persist(
(set, get) => ({ (set, get) => ({
token: null, token: null,
refreshToken: null,
isAuthenticated: false, isAuthenticated: false,
userUuid: null, userUuid: null,
userName: null, userName: null,
@@ -68,15 +66,14 @@ export const useAuthStore = create<AuthState>()(
context: null, context: null,
availableContexts: [], availableContexts: [],
login: (token, refreshToken) => { login: (token) => {
set({ token, refreshToken, isAuthenticated: true }); set({ token, isAuthenticated: true });
get().fetchMe(); get().fetchMe();
}, },
logout: () => logout: () =>
set({ set({
token: null, token: null,
refreshToken: null,
isAuthenticated: false, isAuthenticated: false,
userUuid: null, userUuid: null,
userName: null, userName: null,
@@ -128,7 +125,6 @@ export const useAuthStore = create<AuthState>()(
name: 'clinicpro-auth', name: 'clinicpro-auth',
partialize: (s) => ({ partialize: (s) => ({
token: s.token, token: s.token,
refreshToken: s.refreshToken,
isAuthenticated: s.isAuthenticated, isAuthenticated: s.isAuthenticated,
userUuid: s.userUuid, userUuid: s.userUuid,
userName: s.userName, userName: s.userName,