Skip to content

Commit

Permalink
fix: auth in electron
Browse files Browse the repository at this point in the history
  • Loading branch information
hyoban committed Dec 25, 2024
1 parent d42f52d commit 405ee42
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
32 changes: 20 additions & 12 deletions apps/renderer/src/modules/auth/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,23 @@ import {
FormMessage,
} from "@follow/components/ui/form/index.js"
import { Input } from "@follow/components/ui/input/Input.js"
import type { LoginRuntime } from "@follow/shared/auth"
import { loginHandler, signUp } from "@follow/shared/auth"
import { env } from "@follow/shared/env"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { useTranslation } from "react-i18next"
import { Link } from "react-router"
import { toast } from "sonner"
import { z } from "zod"

import { useCurrentModal, useModalStack } from "~/components/ui/modal/stacked/hooks"

async function onSubmit(values: z.infer<typeof formSchema>) {
const res = await loginHandler("credential", "browser", values)
if (res?.error) {
toast.error(res.error.message)
return
}
window.location.reload()
}
const formSchema = z.object({
email: z.string().email(),
password: z.string().max(128),
})
export function LoginWithPassword() {

export function LoginWithPassword({ runtime }: { runtime?: LoginRuntime }) {
const { t } = useTranslation("app")
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
Expand All @@ -44,6 +38,15 @@ export function LoginWithPassword() {
const { present } = useModalStack()
const { dismiss } = useCurrentModal()

async function onSubmit(values: z.infer<typeof formSchema>) {
const res = await loginHandler("credential", runtime ?? "browser", values)
if (res?.error) {
toast.error(res.error.message)
return
}
window.location.reload()
}

return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
Expand Down Expand Up @@ -73,9 +76,14 @@ export function LoginWithPassword() {
</FormItem>
)}
/>
<Link to="/forget-password" className="block py-1 text-xs text-accent hover:underline">
<a
href={`${env.VITE_WEB_URL}/forget-password`}
target="_blank"
rel="noreferrer"
className="block py-1 text-xs text-accent hover:underline"
>
{t("login.forget_password.note")}
</Link>
</a>
<Button
type="submit"
className="w-full"
Expand Down
2 changes: 1 addition & 1 deletion apps/renderer/src/modules/auth/LoginModalContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export const AuthProvidersRender: FC<{

if (provider.id === "credential") {
present({
content: LoginWithPassword,
content: () => <LoginWithPassword runtime={runtime} />,
title: t("login.with_email.title"),
})
return
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const loginHandler = async (
},
) => {
const { email, password } = args ?? {}
if (IN_ELECTRON) {
if (IN_ELECTRON && provider !== "credential") {
window.open(`${WEB_URL}/login?provider=${provider}`)
} else {
if (provider === "credential") {
Expand Down

0 comments on commit 405ee42

Please sign in to comment.