Server functions with state
is there a way to do something like this but have it work and have the client side call these as server functions?
export function createAuthServerFns(client: Client, auth: Promise<Auth>, options: TanStackAuthOptions) {
const authMiddleware = createAuthMiddleware(client, auth, options);
const withSession = createMiddleware({ type: "function" })
.middleware([authMiddleware])
.server(({ next, context }) => {
const { __gel_auth: auth } = context;
const authCookie = getCookie(auth.options.authCookieName) || getCookie(DEFAULT_AUTH_COOKIE);
return next({ context: { ...context, session: { client: auth.client, authCookie } } });
});
const startOAuth = createServerFn()
.middleware([authMiddleware])
.inputValidator(({ providerName }: { providerName: BuiltinOAuthProviderNames }) => {
...
})
.handler(async ({ data: { providerName }, context: { __gel_auth: auth } }) => {
const pkceSession = await auth.core.then((core: Auth) => core.createPKCESession());
createVerifierCookie(auth, pkceSession.verifier);
const url = getUrl(auth);
return Response.redirect(pkceSession.getOAuthUrl(providerName, url, `${url}?isSignUp=true`));
});
const handleCallback = createFileRoute('/_auth/login/complete')({
params: {parse: (data: Record<"error" | "code" | "error_description" | "provider" | "isSignUp", string | undefined>) => {
...
}
},
server: {
middleware: [authMiddleware],
handler: {
ANY: (async ({ data: { provider, code, isSignUp }, context: { __gel_auth: auth } }) => {
...
}
}
)
const handleSignout = createServerFn()
.middleware([authMiddleware])
.handler(async ({ context: { __gel_auth: auth } }) => {
clearAuthCookie(auth);
return { success: true };
});
return {
authMiddleware,
withSession,
getProvidersInfo,
startOAuth,
handleCallback,
handleSignout,
};
}export function createAuthServerFns(client: Client, auth: Promise<Auth>, options: TanStackAuthOptions) {
const authMiddleware = createAuthMiddleware(client, auth, options);
const withSession = createMiddleware({ type: "function" })
.middleware([authMiddleware])
.server(({ next, context }) => {
const { __gel_auth: auth } = context;
const authCookie = getCookie(auth.options.authCookieName) || getCookie(DEFAULT_AUTH_COOKIE);
return next({ context: { ...context, session: { client: auth.client, authCookie } } });
});
const startOAuth = createServerFn()
.middleware([authMiddleware])
.inputValidator(({ providerName }: { providerName: BuiltinOAuthProviderNames }) => {
...
})
.handler(async ({ data: { providerName }, context: { __gel_auth: auth } }) => {
const pkceSession = await auth.core.then((core: Auth) => core.createPKCESession());
createVerifierCookie(auth, pkceSession.verifier);
const url = getUrl(auth);
return Response.redirect(pkceSession.getOAuthUrl(providerName, url, `${url}?isSignUp=true`));
});
const handleCallback = createFileRoute('/_auth/login/complete')({
params: {parse: (data: Record<"error" | "code" | "error_description" | "provider" | "isSignUp", string | undefined>) => {
...
}
},
server: {
middleware: [authMiddleware],
handler: {
ANY: (async ({ data: { provider, code, isSignUp }, context: { __gel_auth: auth } }) => {
...
}
}
)
const handleSignout = createServerFn()
.middleware([authMiddleware])
.handler(async ({ context: { __gel_auth: auth } }) => {
clearAuthCookie(auth);
return { success: true };
});
return {
authMiddleware,
withSession,
getProvidersInfo,
startOAuth,
handleCallback,
handleSignout,
};
}