refactor(auth): unify messenger bot link flow across login/profile/notifications
This commit is contained in:
10
app/composables/useMessengerBotLink.ts
Normal file
10
app/composables/useMessengerBotLink.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export function buildMessengerBotStartUrl(baseUrl: string, email: string) {
|
||||
const normalizedEmail = email.trim().toLowerCase();
|
||||
if (!baseUrl || !normalizedEmail) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const payload = encodeURIComponent(`login:${normalizedEmail}`);
|
||||
const separator = baseUrl.includes('?') ? '&' : '?';
|
||||
return `${baseUrl}${separator}start=${payload}`;
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
RequestLoginCodeDocument,
|
||||
VerifyLoginCodeDocument,
|
||||
} from '~/composables/graphql/generated';
|
||||
import { buildMessengerBotStartUrl } from '~/composables/useMessengerBotLink';
|
||||
|
||||
const config = useRuntimeConfig();
|
||||
const route = useRoute();
|
||||
@@ -35,18 +36,12 @@ const maxBotUrl = computed(() => config.public.maxBotUrl || '');
|
||||
const normalizedEmail = computed(() => email.value.trim().toLowerCase());
|
||||
const isEmailReady = computed(() => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(normalizedEmail.value));
|
||||
|
||||
function buildBotLoginUrl(baseUrl: string) {
|
||||
if (!isEmailReady.value || !baseUrl) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const payload = encodeURIComponent(`login:${normalizedEmail.value}`);
|
||||
const separator = baseUrl.includes('?') ? '&' : '?';
|
||||
return `${baseUrl}${separator}start=${payload}`;
|
||||
}
|
||||
|
||||
const telegramLoginUrl = computed(() => buildBotLoginUrl(telegramBotUrl.value));
|
||||
const maxLoginUrl = computed(() => buildBotLoginUrl(maxBotUrl.value));
|
||||
const telegramLoginUrl = computed(() =>
|
||||
isEmailReady.value ? buildMessengerBotStartUrl(telegramBotUrl.value, normalizedEmail.value) : '',
|
||||
);
|
||||
const maxLoginUrl = computed(() =>
|
||||
isEmailReady.value ? buildMessengerBotStartUrl(maxBotUrl.value, normalizedEmail.value) : '',
|
||||
);
|
||||
|
||||
async function finalizeSession(accessToken: string) {
|
||||
authCookie.value = accessToken;
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
MyNotificationHistoryDocument,
|
||||
SendTestMessengerMessageDocument,
|
||||
} from '~/composables/graphql/generated';
|
||||
import { buildMessengerBotStartUrl } from '~/composables/useMessengerBotLink';
|
||||
|
||||
const selectedChannel = ref<'TELEGRAM' | 'MAX'>('TELEGRAM');
|
||||
const customMessage = ref('Тест канала уведомлений Fregat');
|
||||
@@ -55,9 +56,7 @@ function buildBotConnectUrl(baseUrl: string) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const payload = encodeURIComponent(`login:${email}`);
|
||||
const separator = baseUrl.includes('?') ? '&' : '?';
|
||||
return `${baseUrl}${separator}start=${payload}`;
|
||||
return buildMessengerBotStartUrl(baseUrl, email);
|
||||
}
|
||||
|
||||
const telegramConnectUrl = computed(() => buildBotConnectUrl(config.public.telegramBotUrl || ''));
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { useMutation, useQuery } from '@vue/apollo-composable';
|
||||
import { MeDocument, MyMessengerConnectionsDocument, RegisterSelfDocument } from '~/composables/graphql/generated';
|
||||
import { buildMessengerBotStartUrl } from '~/composables/useMessengerBotLink';
|
||||
|
||||
const companyName = ref('');
|
||||
const inn = ref('');
|
||||
@@ -45,9 +46,7 @@ function buildBotConnectUrl(baseUrl: string) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const payload = encodeURIComponent(`login:${accountEmail}`);
|
||||
const separator = baseUrl.includes('?') ? '&' : '?';
|
||||
return `${baseUrl}${separator}start=${payload}`;
|
||||
return buildMessengerBotStartUrl(baseUrl, accountEmail);
|
||||
}
|
||||
|
||||
const telegramConnectUrl = computed(() => buildBotConnectUrl(config.public.telegramBotUrl || ''));
|
||||
|
||||
Reference in New Issue
Block a user