/
Login.vue
376 lines (329 loc) · 10.1 KB
/
Login.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<template>
<HoppSmartModal
dialog
:title="`${t('auth.login_to_hoppscotch')}`"
styles="sm:max-w-md"
@close="hideModal"
>
<template #body>
<template v-if="isLoadingAllowedAuthProviders">
<div class="flex justify-center">
<HoppSmartSpinner />
</div>
</template>
<template v-else>
<div v-if="mode === 'sign-in'" class="flex flex-col space-y-2">
<HoppSmartItem
v-for="provider in allowedAuthProviders"
:key="provider.id"
:loading="provider.isLoading.value"
:icon="provider.icon"
:label="provider.label"
@click="provider.action"
/>
<hr v-if="additionalLoginItems.length > 0" />
<HoppSmartItem
v-for="loginItem in additionalLoginItems"
:key="loginItem.id"
:icon="loginItem.icon"
:label="loginItem.text(t)"
@click="doAdditionalLoginItemClickAction(loginItem)"
/>
</div>
<form
v-if="mode === 'email'"
class="flex flex-col space-y-2"
@submit.prevent="signInWithEmail"
>
<HoppSmartInput
v-model="form.email"
type="email"
placeholder=" "
:label="t('auth.email')"
input-styles="floating-input"
/>
<HoppButtonPrimary
:loading="signingInWithEmail"
type="submit"
:label="`${t('auth.send_magic_link')}`"
/>
</form>
<div v-if="mode === 'email-sent'" class="flex flex-col px-4">
<div class="flex max-w-md flex-col items-center justify-center">
<icon-lucide-inbox class="h-6 w-6 text-accent" />
<h3 class="my-2 text-center text-lg">
{{ t("auth.we_sent_magic_link") }}
</h3>
<p class="text-center">
{{
t("auth.we_sent_magic_link_description", { email: form.email })
}}
</p>
</div>
</div>
</template>
</template>
<template #footer>
<div
v-if="mode === 'sign-in' && tosLink && privacyPolicyLink"
class="text-tiny text-secondaryLight"
>
By signing in, you are agreeing to our
<HoppSmartAnchor
class="link"
:to="tosLink"
blank
label="Terms of Service"
/>
and
<HoppSmartAnchor
class="link"
:to="privacyPolicyLink"
blank
label="Privacy Policy"
/>
</div>
<div v-if="mode === 'email'">
<HoppButtonSecondary
:label="t('auth.all_sign_in_options')"
:icon="IconArrowLeft"
class="!p-0"
@click="mode = 'sign-in'"
/>
</div>
<div
v-if="mode === 'email-sent'"
class="flex flex-1 justify-between text-secondaryLight"
>
<HoppSmartAnchor
class="link"
:label="t('auth.re_enter_email')"
:icon="IconArrowLeft"
@click="mode = 'email'"
/>
<HoppSmartAnchor
class="link"
:label="`${t('action.dismiss')}`"
@click="hideModal"
/>
</div>
</template>
</HoppSmartModal>
</template>
<script setup lang="ts">
import { Ref, onMounted, ref } from "vue"
import { useI18n } from "@composables/i18n"
import { useStreamSubscriber } from "@composables/stream"
import { useToast } from "@composables/toast"
import { platform } from "~/platform"
import IconEmail from "~icons/auth/email"
import IconGithub from "~icons/auth/github"
import IconGoogle from "~icons/auth/google"
import IconMicrosoft from "~icons/auth/microsoft"
import IconArrowLeft from "~icons/lucide/arrow-left"
import { useService } from "dioc/vue"
import { LoginItemDef } from "~/platform/auth"
import { PersistenceService } from "~/services/persistence"
import * as E from "fp-ts/Either"
const emit = defineEmits<{
(e: "hide-modal"): void
}>()
const { subscribeToStream } = useStreamSubscriber()
const t = useI18n()
const toast = useToast()
const persistenceService = useService(PersistenceService)
const form = {
email: "",
}
const isLoadingAllowedAuthProviders = ref(true)
const signingInWithGoogle = ref(false)
const signingInWithGitHub = ref(false)
const signingInWithMicrosoft = ref(false)
const signingInWithEmail = ref(false)
const mode = ref("sign-in")
const tosLink = import.meta.env.VITE_APP_TOS_LINK
const privacyPolicyLink = import.meta.env.VITE_APP_PRIVACY_POLICY_LINK
type AuthProviderItem = {
id: string
icon: typeof IconGithub
label: string
action: (...args: any[]) => any
isLoading: Ref<boolean>
}
let allowedAuthProviders: AuthProviderItem[] = []
const additionalLoginItems: LoginItemDef[] = []
const doAdditionalLoginItemClickAction = async (item: LoginItemDef) => {
await item.onClick()
emit("hide-modal")
}
onMounted(async () => {
const currentUser$ = platform.auth.getCurrentUserStream()
subscribeToStream(currentUser$, (user) => {
if (user) hideModal()
})
const res = await platform.auth.getAllowedAuthProviders()
if (E.isLeft(res)) {
toast.error(`${t("error.authproviders_load_error")}`)
isLoadingAllowedAuthProviders.value = false
return
}
// setup the normal auth providers
const enabledAuthProviders = authProvidersAvailable.filter((provider) =>
res.right.includes(provider.id)
)
allowedAuthProviders = enabledAuthProviders
// setup the additional login items
platform.auth.additionalLoginItems?.forEach((item) => {
if (res.right.includes(item.id)) {
additionalLoginItems.push(item)
}
// since the BE send the OIDC auth providers as OIDC:providerName,
// we need to split the string and use the providerName as the text
if (item.id === "OIDC") {
res.right
.filter((provider) => provider.startsWith("OIDC"))
.forEach((provider) => {
const OIDCName = provider.split(":")[1]
const loginItemText = OIDCName
? () =>
t("auth.continue_with_auth_provider", {
provider: OIDCName,
})
: item.text
const OIDCLoginItem = {
...item,
text: loginItemText,
}
additionalLoginItems.push(OIDCLoginItem)
})
}
})
isLoadingAllowedAuthProviders.value = false
})
const showLoginSuccess = () => {
toast.success(`${t("auth.login_success")}`)
}
const signInWithGoogle = async () => {
signingInWithGoogle.value = true
try {
await platform.auth.signInUserWithGoogle()
} catch (e) {
console.error(e)
/*
A auth/account-exists-with-different-credential Firebase error wont happen between Google and any other providers
Seems Google account overwrites accounts of other providers https://github.com/firebase/firebase-android-sdk/issues/25
*/
toast.error(`${t("error.something_went_wrong")}`)
}
signingInWithGoogle.value = false
}
const signInWithGithub = async () => {
signingInWithGitHub.value = true
const result = await platform.auth.signInUserWithGithub()
if (!result) {
signingInWithGitHub.value = false
return
}
if (result.type === "success") {
// this.showLoginSuccess()
} else if (result.type === "account-exists-with-different-cred") {
toast.info(`${t("auth.account_exists")}`, {
duration: 0,
closeOnSwipe: false,
action: {
text: `${t("action.yes")}`,
onClick: async (_, toastObject) => {
await result.link()
showLoginSuccess()
toastObject.goAway(0)
},
},
})
} else {
console.log("error logging into github", result.err)
toast.error(`${t("error.something_went_wrong")}`)
}
signingInWithGitHub.value = false
}
const signInWithMicrosoft = async () => {
signingInWithMicrosoft.value = true
try {
await platform.auth.signInUserWithMicrosoft()
// this.showLoginSuccess()
} catch (e) {
console.error(e)
/*
A auth/account-exists-with-different-credential Firebase error wont happen between MS with Google or Github
If a Github account exists and user then logs in with MS email we get a "Something went wrong toast" and console errors and MS replaces GH as only provider.
The error messages are as follows:
FirebaseError: Firebase: Error (auth/popup-closed-by-user).
@firebase/auth: Auth (9.6.11): INTERNAL ASSERTION FAILED: Pending promise was never set
They may be related to https://github.com/firebase/firebaseui-web/issues/947
*/
toast.error(`${t("error.something_went_wrong")}`)
}
signingInWithMicrosoft.value = false
}
const signInWithEmail = async () => {
signingInWithEmail.value = true
await platform.auth
.signInWithEmail(form.email)
.then(() => {
mode.value = "email-sent"
persistenceService.setLocalConfig("emailForSignIn", form.email)
})
.catch((e) => {
console.error(e)
toast.error(e.message)
signingInWithEmail.value = false
})
.finally(() => {
signingInWithEmail.value = false
})
}
const authProvidersAvailable: AuthProviderItem[] = [
{
id: "GITHUB",
icon: IconGithub,
label: t("auth.continue_with_github"),
action: signInWithGithub,
isLoading: signingInWithGitHub,
},
// the authprovider either send github or github:enterprise and both are handled by the same route
{
id: "GITHUB:ENTERPRISE",
icon: IconGithub,
label: t("auth.continue_with_github_enterprise"),
action: signInWithGithub,
isLoading: signingInWithGitHub,
},
{
id: "GOOGLE",
icon: IconGoogle,
label: t("auth.continue_with_google"),
action: signInWithGoogle,
isLoading: signingInWithGoogle,
},
{
id: "MICROSOFT",
icon: IconMicrosoft,
label: t("auth.continue_with_microsoft"),
action: signInWithMicrosoft,
isLoading: signingInWithMicrosoft,
},
{
id: "EMAIL",
icon: IconEmail,
label: t("auth.continue_with_email"),
action: () => {
mode.value = "email"
},
isLoading: signingInWithEmail,
},
]
const hideModal = () => {
mode.value = "sign-in"
toast.clear()
emit("hide-modal")
}
</script>