Tiesen LogoYuki UI

Authentication

Build your own authentication system from scratch with complete control over implementation

Build your own authentication system from scratch with complete control over implementation. This collection provides authentication components and utilities that you can customize to fit your exact requirements, supporting multiple database adapters and OAuth providers.

Installation

CLI

npx shadcn add https://yuki-ui.vercel.app/r/auth.json

Manual

Download and customize the source code directly from the registry: Source

Quick Start

  1. Configure Authentication

    Edit a configuration file to define your authentication settings, including secret keys, OAuth providers, and database adapters.

    server/auth/config.ts
    import type { AuthConfig } from '@/server/auth/types'
    import { adapter } from '@/server/auth/adapter'
    
    export const authConfig = {
      // ...other config options
    
      providers: [
        // Add OAuth providers here
      ],
    
      // This will be implemented after installing a database adapter
      adapter,
    } as const satisfies AuthConfig

    Set environment variables for your secret key and OAuth provider credentials.

    .env
    # You can generate a secure random key using `openssl rand -base64 32`
    AUTH_SECRET=your-secret-key
    
    # OAuth provider credentials
    AUTH_{PROVIDER}_ID=your-client-id
    AUTH_{PROVIDER}_SECRET=your-client-secret

    Supported OAuth Providers:

    • Discord
    • Facebook
    • Figma
    • Github
    • Google
    • Vercel

    You can add custom providers by implementing the BaseProvider abstract class.

  2. Create API Route

    To handle authentication requests, create an API route in your server framework of choice and export the handler function from the authentication package.

    app/api/auth/[...auth]/route.ts
    export {
      handlers as GET,
      handlers as POST,
      handlers as OPTIONS,
    } from '@/server/auth'
  3. Set Up Query Client and Providers

    Configure React Query and session providers for your application. This ensures efficient data fetching, caching, and session management across your app.

    lib/query-client.ts
    import {
      defaultShouldDehydrateQuery,
      QueryClient,
    } from '@tanstack/react-query'
    
    export const createQueryClient = () =>
      new QueryClient({
        defaultOptions: {
          queries: {
            // With SSR, we usually want to set some default staleTime
            // above 0 to avoid refetching immediately on the client
            staleTime: 60 * 1000,
          },
          dehydrate: {
            shouldDehydrateQuery: (query) =>
              defaultShouldDehydrateQuery(query) ||
              query.state.status === 'pending',
          },
          hydrate: {},
        },
      })
    components/providers.tsx
    'use client'
    
    import type { QueryClient } from '@tanstack/react-query'
    import { QueryClientProvider } from '@tanstack/react-query'
    
    import { SessionProvider } from '@/hooks/use-session'
    import { createQueryClient } from '@/lib/query-client'
    
    let clientQueryClientSingleton: QueryClient | undefined = undefined
    const getQueryClient = () => {
      if (typeof window === 'undefined') return createQueryClient()
      else return (clientQueryClientSingleton ??= createQueryClient())
    }
    
    export function Providers({
      children,
    }: Readonly<{ children: React.ReactNode }>) {
      const queryClient = getQueryClient()
    
      return (
        <QueryClientProvider client={queryClient}>
          <SessionProvider>{children}</SessionProvider>
        </QueryClientProvider>
      )
    }

Conclusion

With these steps, you have set up a customizable authentication system in your application. You can now extend and modify the components and utilities to fit your specific needs, ensuring a secure and efficient authentication process for your users.

On this page