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

authMiddleware is used? #5

Closed
kandros opened this issue Jan 27, 2025 · 2 comments
Closed

authMiddleware is used? #5

kandros opened this issue Jan 27, 2025 · 2 comments

Comments

@kandros
Copy link

kandros commented Jan 27, 2025

I'm seeing authMiddleware not being registered anywhere and can't find references of auto imports in the TanStack Router's docs, does it work?

@dotnize
Copy link
Owner

dotnize commented Jan 27, 2025

It's not currently used in the template but it's basically a sample middleware for you to use in your own server functions. Or you can just delete it and use your own :)

https://tanstack.com/router/latest/docs/framework/react/start/middleware

So when you create your own server functions for ur own use cases, you can use that middleware to force server-side authentication for the server fn

e.g.

import { createServerFn } from '@tanstack/start'
import { authMiddleware } from '~/lib/middleware/auth-guard'

const yourOwnServerFn = createServerFn()
  .middleware([authMiddleware])
  .handler(async (context) => {
    // at this point you can be sure that the user is authenticated
    // and you can use context.user to access the user data
    console.log(context.user);
  })

I should probably work on documenting this or providing a sample server fn that uses this. Thanks for bringing this up.

@kandros
Copy link
Author

kandros commented Jan 27, 2025

Reading the TanStack's doc I was a bit confused too to see middleware acting on serverFn only and not routes, need a bit of mental shift form what I've been always used to

@dotnize dotnize closed this as completed in b41f1a7 Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants