-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.vue
187 lines (170 loc) · 5.33 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
<script lang="ts" setup>
import { useAuth0 } from "@auth0/auth0-vue";
const { $appInsights } = useNuxtApp();
const { isLoading, loginWithRedirect, isAuthenticated, error, logout } =
useAuth0();
const runtimeConfig = useRuntimeConfig();
const isElectron = runtimeConfig.public.systemName === "Electron";
// Redirect to new domain for testers that still use the old host name.
const oldDomain = "bmm-web.brunstad.org";
if (window.location.origin.endsWith(oldDomain)) {
const newPath = window.location
.toString()
.replace(oldDomain, config.websiteDomain);
window.location.href = newPath;
}
const promptToOpenInDesktopApp = (url: string) =>
new Promise((resolve) => {
// create temp frame where url will be opened
const frame = document.createElement("iframe");
frame.name = `_invoker_${Math.random()}`;
// create temp link and set it's target to temp frame
const link = document.createElement("a");
link.href = url;
link.target = frame.name;
// frame must be appended to body otherwise link will open in new tab, and might trigger popup blocker
document.body.appendChild(frame);
setTimeout(() => {
frame?.parentNode?.removeChild(frame);
resolve(null);
}, 2000);
// a simple link.click() did not work in firefox hence we're using dispatchEvent
link.dispatchEvent(new MouseEvent("click"));
});
if (!isElectron && window.location.pathname.startsWith("/track/")) {
// Links to tracks account for 90% of the links shared in the app.
// Other links can be annoying when hitting refresh in the browser, since it will prompt the user again.
// But /track/* will redirect the user to the album and hitting refresh doesn't cause the prompt to happen again.
// As far as I can tell, Spotify also only opens the app for links to tracks.
const adjustedLink = window.location
.toString()
.replace("https://", `${config.appProtocol}://`)
.replace("http://", `${config.appProtocol}://`);
promptToOpenInDesktopApp(adjustedLink);
}
watch(
isLoading,
async (loading) => {
if (loading) return;
if (!isAuthenticated.value) {
$appInsights.event("auth0 - redirect to login", {});
await loginWithRedirect();
}
},
{ immediate: true },
);
watch(error, async (e) => {
if (e) {
const errorCode = e.error;
console.error(e.message, errorCode);
const tryLogoutAndRelogin =
errorCode === "missing_refresh_token" || errorCode === "invalid_grant";
$appInsights.event("auth0 - error", {
error: errorCode,
message: e.message,
stack: e.stack,
tryLogoutAndRelogin,
});
if (tryLogoutAndRelogin) {
// For these errors we've identified that a logout & relogin likely will fix it.
await logout({ openUrl: false });
await loginWithRedirect();
}
}
});
const logoutAndRedirect = async () => {
$appInsights.event("auth0 - user clicked logoutAndRedirect()", {
error: error.value.error,
message: error.value.message,
stack: error.value.stack,
});
await logout({ openUrl: false });
await loginWithRedirect();
};
const { locale } = useI18n();
locale.value = useProfileStore().uiLanguage;
useProfileStore().$subscribe((_, state) => {
locale.value = state.uiLanguage;
});
useHead({
titleTemplate: (chunk) => (chunk ? `${chunk} - BMM` : "BMM"),
});
</script>
<template>
<div class="h-full">
<NuxtLayout v-if="isAuthenticated && !error">
<div class="container mx-auto min-w-80 px-2 pb-20 lg:px-9">
<NuxtPage />
<div class="teleport"></div>
</div>
</NuxtLayout>
<div
v-else-if="!isAuthenticated"
class="flex h-screen text-center"
style="-webkit-app-region: drag"
>
<div class="m-auto">
<SiteLogo size="medium"></SiteLogo>
<div>{{ $t("login.redirect-message.redirect-info") }}</div>
<i18n-t tag="div" keypath="login.redirect-message.manual-redirect-info">
<span
class="cursor-pointer underline"
href="#"
@click="loginWithRedirect()"
>{{ $t("login.redirect-message.redirect-link") }}</span
>
</i18n-t>
</div>
</div>
<div
v-else
class="flex h-screen text-center"
style="-webkit-app-region: drag"
>
<div class="m-auto">
<SiteLogo size="medium"></SiteLogo>
<div>{{ $t("login.error-message.logout-and-redirect-info") }}</div>
<div class="mb-5">{{ error }}</div>
<i18n-t
tag="div"
class="mb-5"
keypath="login.error-message.manual-logout-and-redirect-info"
>
<span
class="cursor-pointer underline"
href="#"
@click="logoutAndRedirect()"
>{{ $t("login.error-message.logout-and-redirect-link") }}</span
>
</i18n-t>
<i18n-t tag="div" keypath="login.error-message.contact-info">
<a
class="cursor-pointer underline"
href="mailto:bmm-support@bcc.no"
target="_blank"
>{{ $t("login.error-message.contact-link") }}</a
>
</i18n-t>
</div>
</div>
</div>
</template>
<style>
html {
color-scheme: light dark;
}
html.dark {
color-scheme: dark;
}
html.light {
color-scheme: light;
}
.page-enter-active,
.page-leave-active {
transition: all 200ms ease-out;
}
.page-enter-from,
.page-leave-to {
opacity: 0;
}
</style>