-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.vue
202 lines (184 loc) · 5.47 KB
/
app.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<template>
<ElConfigProvider :locale="ptBr">
<HemocioneHeader class="header" v-show="userStore.loggedIn" size="10" />
<NuxtLoadingIndicator color="#bb0a08" />
<NuxtLayout v-if="userStore.loggedIn">
<NuxtPage />
</NuxtLayout>
<div class="hemocione-login-loading-wrapper" v-else>
<img src="/logos/baseLogo.svg" class="logo" />
<ElButton
:disabled="!attemptedLogin || userStore.loggedIn || executingLogin"
@click="doLogin"
type="primary"
size="large"
:loading="!attemptedLogin || executingLogin"
>
{{ loginButtonText }}
</ElButton>
</div>
</ElConfigProvider>
</template>
<script lang="ts" setup>
import "dayjs/locale/pt-br";
import ptBr from "element-plus/dist/locale/pt-br.mjs";
import { useUserStore } from "@/stores/user";
const userStore = useUserStore();
const config = useRuntimeConfig();
const route = useRoute();
const { token: urlToken, noAuto } = route.query;
const attemptedLogin = ref(Boolean(noAuto));
const executingLogin = ref(false);
import { Browser } from "@capacitor/browser";
import { Capacitor } from "@capacitor/core";
import { Preferences } from "@capacitor/preferences";
import { App } from "@capacitor/app";
import type { URLOpenListenerEvent } from "@capacitor/app";
import OneSignal from "onesignal-cordova-plugin";
useHead({
title: "Hemocione",
meta: [
{
name: "viewport",
content:
"width=device-width, initial-scale=1.0, viewport-fit=cover, user-scalable=no",
},
{
name: "description",
content:
"Plataforma de doação de sangue do Hemocione. Acompanhe suas doações de sangue, seja notificado quando puder doar novamente e muito mais! 🩸",
},
],
});
if (Capacitor.isNativePlatform() && config.public.oneSignalAppId) {
OneSignal.initialize(config.public.oneSignalAppId);
}
const navigateAfterLogin = ref<string | null>(null);
const confirmLogin = async () => {
await Browser.removeAllListeners();
if (navigateAfterLogin.value) {
navigateTo(navigateAfterLogin.value);
}
};
App.addListener("appUrlOpen", async function (event: URLOpenListenerEvent) {
await Browser.close(); // ensure browser is closed. IOS doesn't close it automatically as android does
const url = new URL(event.url);
if (url.protocol.includes("http")) {
// deep link origin
const path = url.pathname;
const slug = path + url.search;
navigateAfterLogin.value = slug;
}
// TODO: handle own app deeplinks in the future maybe
const token = url.searchParams.get("token");
if (token) {
executingLogin.value = true;
attemptedLogin.value = false;
await evaluateCurrentLogin(token);
}
});
const evaluateCurrentLogin = async (enforcedToken?: string) => {
const currentUserCookie = useCookie(config.public.authLocalKey, {
domain: config.public.cookieDomain,
});
const currentUserLocalToken = (
await Preferences.get({
key: config.public.authLocalKey,
})
).value;
// prefer local storage over cookie
const currentUserToken =
enforcedToken || currentUserLocalToken || currentUserCookie.value;
if (currentUserToken && (enforcedToken || !noAuto)) {
try {
await $fetch(`${config.public.hemocioneIdApiUrl}/users/validate-token`, {
headers: {
Authorization: `Bearer ${currentUserToken}`,
},
});
await Preferences.set({
key: config.public.authLocalKey,
value: currentUserToken,
});
await userStore.setToken(currentUserToken);
await confirmLogin();
} catch (e) {
console.error(e);
} finally {
attemptedLogin.value = true;
executingLogin.value = false;
}
if (attemptedLogin.value && !userStore.loggedIn) {
userStore.$reset();
await Preferences.remove({ key: config.public.authLocalKey });
}
}
if (!userStore.loggedIn && !noAuto) {
doLogin();
}
};
const doLogin = async () => {
const baseUrl = `${config.public.hemocioneIdUrl}/?redirect=`;
if (!Capacitor.isNativePlatform()) {
const url = `${baseUrl}${encodeURIComponent(window.location.href)}`;
await Browser.open({ url, toolbarColor: "#bb0a08", windowName: "_self" });
return;
}
const url =
Capacitor.getPlatform() === "ios"
? `${baseUrl}${encodeURIComponent("apphemocione:auth")}`
: `${baseUrl}${encodeURIComponent(
"br.com.hemocione.app://app.hemocione.com.br/"
)}`;
Browser.addListener("browserFinished", async () => {
attemptedLogin.value = true;
});
await Browser.open({
url,
toolbarColor: "#bb0a08",
windowName: "_self",
presentationStyle: "popover",
});
};
const loginButtonText = computed(() => {
if (executingLogin.value || !attemptedLogin.value) {
return "Entrando...";
}
return "Entrar";
});
if (urlToken) {
executingLogin.value = true;
await Preferences.set({
key: config.public.authLocalKey,
value: String(urlToken),
});
await userStore.setToken(String(urlToken));
await confirmLogin();
executingLogin.value = false;
window.history.replaceState({}, document.title, window.location.pathname);
} else {
evaluateCurrentLogin();
}
</script>
<style scoped>
.hemocione-login-loading-wrapper {
display: flex;
justify-content: center;
flex-direction: column;
gap: 3em;
align-items: center;
height: 100vh;
width: 100vw;
}
.logo {
height: 300px;
animation: zoom 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.header {
position: fixed;
top: 0;
z-index: 2;
height: var(--navbar-size);
width: 100%;
}
</style>