Initial Mento CRM: portal OTP, reports, SMS, and deploy for crm.montzar.ir.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { computed, ref } from 'vue';
|
||||
import type { AuthUserDto, LoginResponse } from '@mento/shared';
|
||||
import { api } from '@/lib/api';
|
||||
|
||||
const TOKEN_KEY = 'mento_token';
|
||||
const USER_KEY = 'mento_user';
|
||||
const LEGACY_KEYS = ['ment_token', 'ment_user'] as const;
|
||||
|
||||
function readStoredToken(): string | null {
|
||||
return localStorage.getItem(TOKEN_KEY) ?? localStorage.getItem('ment_token');
|
||||
}
|
||||
|
||||
function readStoredUser(): AuthUserDto | null {
|
||||
const raw = localStorage.getItem(USER_KEY) ?? localStorage.getItem('ment_user');
|
||||
if (!raw) return null;
|
||||
try {
|
||||
return JSON.parse(raw) as AuthUserDto;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function clearLegacy() {
|
||||
for (const key of LEGACY_KEYS) localStorage.removeItem(key);
|
||||
}
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const token = ref<string | null>(readStoredToken());
|
||||
const user = ref<AuthUserDto | null>(readStoredUser());
|
||||
|
||||
const isAuthenticated = computed(() => Boolean(token.value));
|
||||
|
||||
function persist(payload: LoginResponse) {
|
||||
token.value = payload.accessToken;
|
||||
user.value = payload.user;
|
||||
localStorage.setItem(TOKEN_KEY, payload.accessToken);
|
||||
localStorage.setItem(USER_KEY, JSON.stringify(payload.user));
|
||||
clearLegacy();
|
||||
}
|
||||
|
||||
async function login(email: string, password: string) {
|
||||
const { data } = await api.post<LoginResponse>('/auth/login', { email, password });
|
||||
persist(data);
|
||||
}
|
||||
|
||||
async function register(payload: {
|
||||
organizationName: string;
|
||||
fullName: string;
|
||||
email: string;
|
||||
password: string;
|
||||
}) {
|
||||
const { data } = await api.post<LoginResponse>('/auth/register', payload);
|
||||
persist(data);
|
||||
}
|
||||
|
||||
function logout() {
|
||||
token.value = null;
|
||||
user.value = null;
|
||||
localStorage.removeItem(TOKEN_KEY);
|
||||
localStorage.removeItem(USER_KEY);
|
||||
clearLegacy();
|
||||
}
|
||||
|
||||
return {
|
||||
token,
|
||||
user,
|
||||
isAuthenticated,
|
||||
login,
|
||||
register,
|
||||
logout,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user