2026-08-29 21:06:30 +03:30
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
|
import { computed, ref } from 'vue';
|
2026-09-05 00:47:33 +03:30
|
|
|
import type {
|
|
|
|
|
AuthUserDto,
|
|
|
|
|
LoginChallengeResponse,
|
|
|
|
|
LoginResponse,
|
|
|
|
|
RegisterChallengeResponse,
|
|
|
|
|
RegisterOrganizationRequest,
|
|
|
|
|
} from '@mento/shared';
|
2026-08-29 21:06:30 +03:30
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-05 00:47:33 +03:30
|
|
|
async function startLogin(identifier: string, password: string) {
|
|
|
|
|
const { data } = await api.post<LoginChallengeResponse>('/auth/login', {
|
|
|
|
|
identifier,
|
|
|
|
|
password,
|
|
|
|
|
});
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function verifyLogin(challengeId: string, code: string) {
|
|
|
|
|
const { data } = await api.post<LoginResponse>('/auth/login/verify', {
|
|
|
|
|
challengeId,
|
|
|
|
|
code,
|
|
|
|
|
});
|
2026-08-29 21:06:30 +03:30
|
|
|
persist(data);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-05 00:47:33 +03:30
|
|
|
async function startRegister(payload: RegisterOrganizationRequest) {
|
|
|
|
|
const { data } = await api.post<RegisterChallengeResponse>(
|
|
|
|
|
'/auth/register',
|
|
|
|
|
payload,
|
|
|
|
|
);
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function verifyRegister(
|
|
|
|
|
challengeId: string,
|
|
|
|
|
emailCode: string,
|
|
|
|
|
mobileCode: string,
|
|
|
|
|
) {
|
|
|
|
|
const { data } = await api.post<LoginResponse>('/auth/register/verify', {
|
|
|
|
|
challengeId,
|
|
|
|
|
emailCode,
|
|
|
|
|
mobileCode,
|
|
|
|
|
});
|
2026-08-29 21:06:30 +03:30
|
|
|
persist(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function logout() {
|
|
|
|
|
token.value = null;
|
|
|
|
|
user.value = null;
|
|
|
|
|
localStorage.removeItem(TOKEN_KEY);
|
|
|
|
|
localStorage.removeItem(USER_KEY);
|
|
|
|
|
clearLegacy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
token,
|
|
|
|
|
user,
|
|
|
|
|
isAuthenticated,
|
2026-09-05 00:47:33 +03:30
|
|
|
startLogin,
|
|
|
|
|
verifyLogin,
|
|
|
|
|
startRegister,
|
|
|
|
|
verifyRegister,
|
2026-08-29 21:06:30 +03:30
|
|
|
logout,
|
|
|
|
|
};
|
|
|
|
|
});
|