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: migrate to better auth #1951

Merged
merged 12 commits into from
Dec 3, 2024
Prev Previous commit
Next Next commit
feat: server auth create session plugin
  • Loading branch information
DIYgod committed Dec 2, 2024
commit fa8512c36d20f42d443d0bf73fc18afadce0ab8d
3 changes: 2 additions & 1 deletion apps/renderer/src/queries/users.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getProviders } from "@follow/shared/auth"
import { useQuery } from "@tanstack/react-query"

import { apiClient } from "~/lib/api-fetch"
Expand All @@ -16,7 +17,7 @@ export const users = {
export const useAuthProviders = () => {
return useQuery({
queryKey: ["providers"],
queryFn: () => apiClient["auth-app"].providers.$get(),
queryFn: async () => (await getProviders()).data,
placeholderData: {
google: {
id: "google",
Expand Down
13 changes: 7 additions & 6 deletions apps/server/client/pages/(login)/login.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { UserAvatar } from "@client/components/ui/user-avatar"
import { apiClient } from "@client/lib/api-fetch"
import { useSession } from "@client/query/auth"
import { useAuthProviders } from "@client/query/users"
import { Logo } from "@follow/components/icons/logo.jsx"
import { Button } from "@follow/components/ui/button/index.js"
import { authProvidersConfig } from "@follow/constants"
import { loginHandler, signOut } from "@follow/shared/auth"
import { createSession, loginHandler, signOut } from "@follow/shared/auth"
import { DEEPLINK_SCHEME } from "@follow/shared/constants"
import { cn } from "@follow/utils/utils"
import { useCallback, useEffect, useRef, useState } from "react"
Expand Down Expand Up @@ -38,16 +37,18 @@ function Login() {
}, [status])

const getCallbackUrl = useCallback(async () => {
const { data } = await apiClient["auth-app"]["new-session"].$post({})
const { data } = await createSession()
if (!data) return null
return {
url: `${DEEPLINK_SCHEME}auth?token=${data.sessionToken}&userId=${data.userId}`,
url: `${DEEPLINK_SCHEME}auth?token=${data.token}&userId=${data.userId}`,
userId: data.userId,
}
}, [])

const handleOpenApp = useCallback(async () => {
const { url } = await getCallbackUrl()
window.open(url, "_top")
const callbackUrl = await getCallbackUrl()
if (!callbackUrl) return
window.open(callbackUrl.url, "_top")
}, [getCallbackUrl])

const onceRef = useRef(false)
Expand Down
6 changes: 5 additions & 1 deletion packages/shared/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ const serverPlugins = [
id: "getProviders",
$InferServerPlugin: {} as (typeof authPlugins)[0],
},
{
id: "createSession",
$InferServerPlugin: {} as (typeof authPlugins)[1],
},
] satisfies BetterAuthClientPlugin[]

const authClient = createAuthClient({
baseURL: `${env.VITE_API_URL}/better-auth`,
plugins: serverPlugins,
})

export const { signIn, signOut, getSession, getProviders } = authClient
export const { signIn, signOut, getSession, getProviders, createSession } = authClient

export const LOGIN_CALLBACK_URL = `${WEB_URL}/login`
export type LoginRuntime = "browser" | "app"
Expand Down
122 changes: 98 additions & 24 deletions packages/shared/src/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Env = {
Bindings: HttpBindings;
};

declare const authPlugins: {
declare const authPlugins: ({
id: "getProviders";
endpoints: {
getProviders: {
Expand All @@ -38,7 +38,33 @@ declare const authPlugins: {
headers: Headers;
};
};
}[];
} | {
id: "createSession";
endpoints: {
createSession: {
<C extends [(better_call.Context<"/create-session", {
method: "GET";
}> | undefined)?]>(...ctx: C): Promise<C extends [{
asResponse: true;
}] ? Response : {
id: string;
userId: string;
createdAt: Date;
updatedAt: Date;
expiresAt: Date;
token: string;
ipAddress?: string | null | undefined;
userAgent?: string | null | undefined;
} | null>;
path: "/create-session";
options: {
method: "GET";
};
method: better_call.Method | better_call.Method[];
headers: Headers;
};
};
})[];

declare const achievements: drizzle_orm_pg_core.PgTableWithColumns<{
name: "achievements";
Expand Down Expand Up @@ -6589,6 +6615,28 @@ declare const auth: {
method: better_call.Method | better_call.Method[];
headers: Headers;
};
createSession: {
<C extends [(better_call.Context<"/create-session", {
method: "GET";
}> | undefined)?]>(...ctx: C): Promise<C extends [{
asResponse: true;
}] ? Response : {
id: string;
userId: string;
createdAt: Date;
updatedAt: Date;
expiresAt: Date;
token: string;
ipAddress?: string | null | undefined;
userAgent?: string | null | undefined;
} | null>;
path: "/create-session";
options: {
method: "GET";
};
method: better_call.Method | better_call.Method[];
headers: Headers;
};
signInSocial: {
<C extends [better_call.Context<"/sign-in/social", {
method: "POST";
Expand Down Expand Up @@ -9108,28 +9156,6 @@ declare const auth: {
} | undefined;
};
plugins: ({
id: "getProviders";
endpoints: {
getProviders: {
<C extends [(better_call.Context<"/get-providers", {
method: "GET";
}> | undefined)?]>(...ctx: C): Promise<C extends [{
asResponse: true;
}] ? Response : {
[k: string]: {
id: string;
name: string;
};
}>;
path: "/get-providers";
options: {
method: "GET";
};
method: better_call.Method | better_call.Method[];
headers: Headers;
};
};
} | {
id: "custom-session";
endpoints: {
getSession: {
Expand Down Expand Up @@ -9179,6 +9205,54 @@ declare const auth: {
headers: Headers;
};
};
} | {
id: "getProviders";
endpoints: {
getProviders: {
<C extends [(better_call.Context<"/get-providers", {
method: "GET";
}> | undefined)?]>(...ctx: C): Promise<C extends [{
asResponse: true;
}] ? Response : {
[k: string]: {
id: string;
name: string;
};
}>;
path: "/get-providers";
options: {
method: "GET";
};
method: better_call.Method | better_call.Method[];
headers: Headers;
};
};
} | {
id: "createSession";
endpoints: {
createSession: {
<C extends [(better_call.Context<"/create-session", {
method: "GET";
}> | undefined)?]>(...ctx: C): Promise<C extends [{
asResponse: true;
}] ? Response : {
id: string;
userId: string;
createdAt: Date;
updatedAt: Date;
expiresAt: Date;
token: string;
ipAddress?: string | null | undefined;
userAgent?: string | null | undefined;
} | null>;
path: "/create-session";
options: {
method: "GET";
};
method: better_call.Method | better_call.Method[];
headers: Headers;
};
};
})[];
};
$context: Promise<better_auth.AuthContext>;
Expand Down