api-client/components/firebase/Login.vue

282 lines
7.7 KiB
Vue
Raw Normal View History

2020-01-20 16:55:48 +00:00
<template>
<SmartModal
v-if="show"
:title="$t('auth.login_to_hoppscotch')"
dialog
@close="hideModal"
>
<template #body>
2021-08-15 09:48:04 +00:00
<div v-if="mode === 'sign-in'" class="flex flex-col space-y-2 px-2">
<SmartItem
:loading="signingInWithGitHub"
2021-08-28 00:17:33 +00:00
svg="auth/github"
2021-08-16 16:32:18 +00:00
:label="$t('auth.continue_with_github')"
@click.native="signInWithGithub"
/>
<SmartItem
:loading="signingInWithGoogle"
2021-08-28 00:17:33 +00:00
svg="auth/google"
:label="$t('auth.continue_with_google')"
@click.native="signInWithGoogle"
/>
<SmartItem
icon="mail"
2021-08-16 16:32:18 +00:00
:label="$t('auth.continue_with_email')"
@click.native="mode = 'email'"
/>
</div>
<div v-if="mode === 'email'" class="flex flex-col space-y-2">
2021-08-07 09:21:13 +00:00
<div class="flex items-center relative">
<input
id="email"
v-model="form.email"
v-focus
2021-08-07 09:21:13 +00:00
class="input floating-input"
placeholder=" "
type="email"
name="email"
autocomplete="off"
required
spellcheck="false"
autofocus
@keyup.enter="signInWithEmail"
/>
2021-08-07 09:21:13 +00:00
<label for="email">
{{ $t("auth.email") }}
</label>
</div>
2021-07-03 13:14:58 +00:00
<ButtonPrimary
:loading="signingInWithEmail"
:disabled="
form.email.length !== 0
? emailRegex.test(form.email)
? false
: true
: true
"
type="button"
2021-08-02 15:27:18 +00:00
:label="$t('auth.send_magic_link')"
2021-07-03 13:14:58 +00:00
@click.native="signInWithEmail"
/>
</div>
<div v-if="mode === 'email-sent'" class="flex flex-col px-4">
2021-07-17 17:40:28 +00:00
<div class="flex flex-col max-w-md justify-center items-center">
2021-08-28 00:17:33 +00:00
<SmartIcon class="h-6 text-accent w-6" name="inbox" />
<h3 class="my-2 text-center text-lg">
2021-08-02 15:27:18 +00:00
{{ $t("auth.we_sent_magic_link") }}
2021-07-03 13:14:58 +00:00
</h3>
<p class="text-center">
2021-08-02 15:27:18 +00:00
{{
$t("auth.we_sent_magic_link_description", { email: form.email })
}}
2021-07-03 13:14:58 +00:00
</p>
</div>
</div>
</template>
<template #footer>
<p v-if="mode === 'sign-in'" class="text-secondaryLight">
By signing in, you are agreeing to our
2021-08-06 16:10:26 +00:00
<SmartAnchor
class="link"
2021-08-19 17:08:50 +00:00
to="https://docs.hoppscotch.io/terms"
2021-08-06 16:10:26 +00:00
blank
label="Terms of Service"
/>
and
2021-08-06 16:10:26 +00:00
<SmartAnchor
class="link"
2021-08-19 17:08:50 +00:00
to="https://docs.hoppscotch.io/privacy"
2021-08-06 16:10:26 +00:00
blank
label="Privacy Policy"
/>.
</p>
<p v-if="mode === 'email'" class="text-secondaryLight">
<SmartAnchor
class="link"
2021-08-20 16:05:54 +00:00
:label="`← \xA0 ${$t('auth.all_sign_in_options')}`"
@click.native="mode = 'sign-in'"
/>
</p>
<p
v-if="mode === 'email-sent'"
class="flex flex-1 text-secondaryLight justify-between"
>
<SmartAnchor
class="link"
2021-08-20 16:05:54 +00:00
:label="`← \xA0 ${$t('auth.re_enter_email')}`"
@click.native="mode = 'email'"
/>
2021-08-02 15:27:18 +00:00
<SmartAnchor
class="link"
:label="$t('action.dismiss')"
@click.native="hideModal"
/>
</p>
</template>
</SmartModal>
2020-01-20 16:55:48 +00:00
</template>
<script lang="ts">
2021-08-24 03:44:46 +00:00
import { defineComponent } from "@nuxtjs/composition-api"
2021-06-14 04:07:30 +00:00
import {
signInUserWithGoogle,
2021-06-14 12:35:53 +00:00
signInUserWithGithub,
2021-06-14 04:07:30 +00:00
setProviderInfo,
2021-07-03 13:14:58 +00:00
currentUser$,
signInWithEmail,
linkWithFBCredential,
getGithubCredentialFromResult,
2021-06-14 04:07:30 +00:00
} from "~/helpers/fb/auth"
2021-07-03 13:14:58 +00:00
import { setLocalConfig } from "~/newstore/localpersistence"
2021-08-12 08:14:10 +00:00
import { useStreamSubscriber } from "~/helpers/utils/composables"
2020-01-20 16:55:48 +00:00
2021-08-24 03:44:46 +00:00
export default defineComponent({
props: {
show: Boolean,
},
2021-08-12 08:14:10 +00:00
setup() {
const { subscribeToStream } = useStreamSubscriber()
return {
subscribeToStream,
}
},
2021-07-03 13:14:58 +00:00
data() {
return {
form: {
email: "",
},
signingInWithGoogle: false,
signingInWithGitHub: false,
2021-07-03 13:14:58 +00:00
signingInWithEmail: false,
emailRegex:
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/,
mode: "sign-in",
}
},
mounted() {
2021-08-12 08:14:10 +00:00
this.subscribeToStream(currentUser$, (user) => {
2021-07-03 13:14:58 +00:00
if (user) this.hideModal()
})
},
2020-01-20 16:55:48 +00:00
methods: {
showLoginSuccess() {
this.$toast.success(this.$t("auth.login_success").toString(), {
icon: "vpn_key",
})
},
async signInWithGoogle() {
this.signingInWithGoogle = true
try {
await signInUserWithGoogle()
this.showLoginSuccess()
} catch (e) {
console.error(e)
// An error happened.
if (e.code === "auth/account-exists-with-different-credential") {
// Step 2.
// User's email already exists.
// The pending Google credential.
const pendingCred = e.credential
2021-08-02 15:27:18 +00:00
this.$toast.info(`${this.$t("auth.account_exists")}`, {
icon: "vpn_key",
duration: 0,
closeOnSwipe: false,
action: {
text: this.$t("action.yes").toString(),
2021-05-18 06:26:59 +00:00
onClick: async (_, toastObject) => {
const { user } = await signInUserWithGithub()
await linkWithFBCredential(user, pendingCred)
this.showLoginSuccess()
toastObject.goAway(0)
},
},
})
2021-08-18 05:04:20 +00:00
} else {
this.$toast.error(this.$t("error.something_went_wrong").toString(), {
2021-08-20 09:38:54 +00:00
icon: "error_outline",
2021-08-18 05:04:20 +00:00
})
}
}
this.signingInWithGoogle = false
2020-01-20 16:55:48 +00:00
},
async signInWithGithub() {
this.signingInWithGitHub = true
try {
const result = await signInUserWithGithub()
const credential = getGithubCredentialFromResult(result)!
const token = credential.accessToken
2020-12-07 08:44:02 +00:00
setProviderInfo(result.providerId!, token!)
this.showLoginSuccess()
} catch (e) {
console.error(e)
// An error happened.
if (e.code === "auth/account-exists-with-different-credential") {
// Step 2.
// User's email already exists.
// The pending Google credential.
const pendingCred = e.credential
2021-08-02 15:27:18 +00:00
this.$toast.info(`${this.$t("auth.account_exists")}`, {
icon: "vpn_key",
duration: 0,
closeOnSwipe: false,
action: {
text: this.$t("action.yes").toString(),
2021-05-18 06:26:59 +00:00
onClick: async (_, toastObject) => {
2021-06-14 04:07:30 +00:00
const { user } = await signInUserWithGoogle()
await linkWithFBCredential(user, pendingCred)
this.showLoginSuccess()
toastObject.goAway(0)
},
},
})
2021-08-18 05:04:20 +00:00
} else {
this.$toast.error(this.$t("error.something_went_wrong").toString(), {
2021-08-20 09:38:54 +00:00
icon: "error_outline",
2021-08-18 05:04:20 +00:00
})
}
}
this.signingInWithGitHub = false
2020-02-24 18:44:50 +00:00
},
2021-07-03 13:14:58 +00:00
async signInWithEmail() {
this.signingInWithEmail = true
2021-07-03 13:14:58 +00:00
const actionCodeSettings = {
url: `${process.env.BASE_URL}/enter`,
handleCodeInApp: true,
}
await signInWithEmail(this.form.email, actionCodeSettings)
.then(() => {
this.mode = "email-sent"
setLocalConfig("emailForSignIn", this.form.email)
})
.catch((e) => {
console.error(e)
this.$toast.error(e.message, {
2021-08-20 09:38:54 +00:00
icon: "error_outline",
2021-07-03 13:14:58 +00:00
})
this.signingInWithEmail = false
})
.finally(() => {
this.signingInWithEmail = false
})
},
hideModal() {
this.mode = "sign-in"
this.$toast.clear()
this.$emit("hide-modal")
},
2020-02-24 18:44:50 +00:00
},
2021-08-24 03:44:46 +00:00
})
2020-01-20 16:55:48 +00:00
</script>