Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Telegram widget Login #102

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 53 additions & 9 deletions src/routes/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ function Login() {
}
}, [window.location.search])

useEffect(() => {
if (settingData?.config?.oauth2_providers?.includes("Telegram")) {
const initTelegramLogin = async () => {
try {
const redirectUrl = await getOauth2RedirectURL(
"Telegram",
Oauth2RequestType.LOGIN,
)
const [botName, authUrl] = redirectUrl.redirect!.split("---")
const container = document.getElementById("telegram-login-container")
if (container) {
container.innerHTML = ""
const widget = loadTelegramWidget(botName, authUrl)
container.appendChild(widget)
}
} catch (error: any) {
toast.error(error.message)
}
}

initTelegramLogin()
}
}, [settingData?.config?.oauth2_providers])

const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
Expand All @@ -64,6 +88,17 @@ function Login() {

const { t } = useTranslation()

const loadTelegramWidget = (botName: string, authUrl: string) => {
const script = document.createElement("script")
script.src = "https://telegram.org/js/telegram-widget.js?22"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里能不能拆一下或者编码下 怕被q

script.async = true
script.setAttribute("data-telegram-login", botName)
script.setAttribute("data-size", "large")
script.setAttribute("data-auth-url", authUrl)
script.setAttribute("data-request-access", "write")
return script
}

return (
<div className="mt-28 sm:max-w-sm m-auto max-w-xs">
<Form {...form}>
Expand Down Expand Up @@ -118,15 +153,24 @@ function Login() {
)}
</Form>
<div className="mt-3 flex flex-col gap-3">
{settingData?.config?.oauth2_providers?.map((p: string) => (
<Button
className="w-full rounded-lg shadow-[inset_0_1px_0_rgba(255,255,255,0.2)] bg-muted text-primary hover:bg-muted/80 hover:text-primary/80"
onClick={() => loginWith(p)}
>
{p === "GitHub" && <GitHubIcon className="size-4" />}
{p}
</Button>
))}
{settingData?.config?.oauth2_providers?.map((p: string) =>
p === "Telegram" ? (
<div
id="telegram-login-container"
key={p}
className="flex justify-center"
/>
) : (
<Button
key={p}
className="w-full rounded-lg shadow-[inset_0_1px_0_rgba(255,255,255,0.2)] bg-muted text-primary hover:bg-muted/80 hover:text-primary/80"
onClick={() => loginWith(p)}
>
{p === "GitHub" && <GitHubIcon className="size-4" />}
{p}
</Button>
),
)}
</div>
</div>
)
Expand Down
39 changes: 38 additions & 1 deletion src/routes/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,38 @@ export default function ProfilePage() {
}
}, [window.location.search])

const initTelegramWidget = async () => {
try {
const redirectUrl = await getOauth2RedirectURL("Telegram", Oauth2RequestType.BIND)
const [botName, authUrl] = redirectUrl.redirect!.split("---")
const container = document.getElementById("telegram-bind-container")
if (container) {
container.innerHTML = ""
const widget = loadTelegramWidget(botName, authUrl)
container.appendChild(widget)
}
} catch (error: any) {
toast.error(error.message)
}
}

useEffect(() => {
if (settingData?.config?.oauth2_providers?.includes("Telegram")) {
initTelegramWidget()
}
}, [settingData?.config?.oauth2_providers])

const loadTelegramWidget = (botName: string, authUrl: string) => {
const script = document.createElement("script")
script.src = "https://telegram.org/js/telegram-widget.js?22"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里也是

script.async = true
script.setAttribute("data-telegram-login", botName)
script.setAttribute("data-size", "medium")
script.setAttribute("data-auth-url", authUrl)
script.setAttribute("data-request-access", "write")
return script
}

const bindO2 = async (provider: string) => {
try {
const redirectUrl = await getOauth2RedirectURL(provider, Oauth2RequestType.BIND)
Expand All @@ -42,6 +74,9 @@ export default function ProfilePage() {
await unbindOauth2(provider)
const profile = await getProfile()
setProfile(profile)
if (provider === "Telegram") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

名字也是

initTelegramWidget()
}
} catch (error: any) {
toast.error(error.message)
}
Expand Down Expand Up @@ -111,7 +146,7 @@ export default function ProfilePage() {
<section className="flex gap-2 items-center">
<p>{provider}: </p>
{profile.oauth2_bind?.[provider.toLowerCase()] && (
<p className=" bg-muted px-1.5 py-0.5 text-sm rounded-full">
<p className="bg-muted px-1.5 py-0.5 text-sm rounded-full">
{profile.oauth2_bind?.[provider.toLowerCase()]}
</p>
)}
Expand All @@ -124,6 +159,8 @@ export default function ProfilePage() {
>
Unbind
</Button>
) : provider === "Telegram" ? (
<div id="telegram-bind-container" />
) : (
<Button
className="my-1"
Expand Down