Authentication
Complete authentication system with Better Auth
Installation
Install the Package
Add Authentication to your project using the Tailar CLI:
Set Environment Variables
Create a `.env` file in the root of your project and add the following environment variables:
1# App Configuration2NEXT_PUBLIC_APP_URL="http://localhost:3000"3 4# Auth Configuration5BETTER_AUTH_URL="http://localhost:3000"6BETTER_AUTH_SECRET="your-secret-key-here-replace-with-secure-random-string"7# Generate a secure random string using: openssl rand -base64 328 9# Google OAuth (Optional)10# Uncomment and fill these if you want to enable Google authentication11# GOOGLE_CLIENT_ID="your_google_client_id"12# GOOGLE_CLIENT_SECRET="your_google_client_secret"Preview
Explore representative UI flows and snippets generated for this module (same previews as on the home page modules section).
Don't have an account? Sign Up
Usage
Once you've installed Authentication, you can start using the following actions:
signInWithEmail
Sign in a user with email and password.
1import { signInWithEmail } from '@/lib/auth/actions'2 3await signInWithEmail(email, password)signUpWithEmail
Create a new user account with email and password.
1import { signUpWithEmail } from '@/lib/auth/actions'2 3await signUpWithEmail(email, password, name)signInWithGoogle
Sign in a user with Google OAuth.
1import { signInWithGoogle } from '@/lib/auth/actions'2 3await signInWithGoogle()signOutAction
Sign out the current user.
1import { signOutAction } from '@/lib/auth/actions'2 3await signOutAction()getSession
Get the current user session. Returns null if not authenticated.
1import { getSession } from '@/lib/auth'2 3const session = await getSession()4if (session) {5 console.log('User:', session.user)6}updateUser
Update the current user's profile information.
1import { updateUser } from '@/lib/auth/actions'2 3await updateUser({ name: 'New Name', image: 'https://...' })forgotPassword
Send a password reset email to the user.
1import { forgotPassword } from '@/lib/auth/actions'2 3await forgotPassword(email)resetPassword
Reset the user's password using a reset token.
1import { resetPassword } from '@/lib/auth/actions'2 3await resetPassword(token, newPassword)useAuthActions
React hook for authentication operations in client components. Provides signIn, signUp, signOut, and other auth methods.
1import { useAuthActions } from '@/lib/auth/hooks'2 3function LoginForm() {4 const { signIn, isLoading, error } = useAuthActions()5 6 const handleSubmit = async () => {7 await signIn(email, password)8 }9}useAuth
React hook to access the current user session in client components.
1import { useAuth } from '@/lib/auth/context'2 3function Profile() {4 const { user, session } = useAuth()5 return <div>Welcome {user?.name}</div>6}