google-login-vue3
Version:
A lightweight and customizable Vue 3 component for Google OAuth 2.0 authentication, supporting both code-based and JWT-based authentication modes.
60 lines (59 loc) • 1.79 kB
JavaScript
import { defineAsyncComponent as a, onMounted as u, createElementBlock as c, openBlock as d, renderSlot as g, createVNode as m, unref as p } from "vue";
const f = {
key: 0,
class: "g_id_signin"
}, h = {
__name: "GoogleLogin",
props: {
clientId: {
type: String,
required: !0
},
mode: {
type: String,
default: "JWT",
validator: (i) => ["code", "JWT"].includes(i)
},
oneTapLogin: {
type: Boolean,
default: !0
}
},
emits: ["success", "error"],
setup(i, { emit: l }) {
const r = a(() => import("./CustomButton-Dh7NPMMw.js")), o = i, t = l, s = () => {
o.mode === "code" && window.google.accounts.oauth2.initCodeClient({
client_id: o.clientId,
scope: "profile email",
callback: (e) => {
e.error ? t("error", e) : t("success", e);
}
}).requestCode();
};
return u(() => {
const e = document.createElement("script");
e.src = "https://accounts.google.com/gsi/client", e.async = !0, e.defer = !0, e.onload = () => {
o.mode === "JWT" && (window.google.accounts.id.initialize({
client_id: o.clientId,
callback: (n) => {
n.credential ? t("success", { credential: n.credential }) : t("error", n);
},
auto_select: !1
}), window.google.accounts.id.renderButton(
document.querySelector(".g_id_signin"),
{ theme: "outline", size: "large" }
), o.oneTapLogin && window.google.accounts.id.prompt());
}, document.head.appendChild(e);
}), (e, n) => o.mode === "JWT" ? (d(), c("div", f)) : (d(), c("div", {
key: 1,
onClick: s
}, [
g(e.$slots, "default", {}, () => [
m(p(r))
])
]));
}
};
export {
h as default
};