Authentication System
Quickly implement an authentication system in Easy SaaS Next using better-auth
Easy SaaS Next uses better-auth as its authentication system.
Using better-auth requires database support. Therefore, you need to configure the database before using it.
Configuration
All configuration files are located in the src/lib folder.
For rapid development, Easy SaaS Next uses Google and Github as default authentication methods.
The advantage of this approach is that users don't need to verify their email addresses (email login typically requires sending verification emails). You only need to apply for Google and Github OAuth to quickly integrate the authentication system.
Configure Google OAuth
- Log in to Google Cloud Console
- Go to OAuth Client ID to create an OAuth client ID
- Configure
JavaScript Originsashttp://localhost:3000orhttps://your-domain - Configure
Redirect URIashttp://localhost:3000/api/auth/callback/googleorhttps://your-domain/api/auth/callback/google - Configure the
clientIdandclientSecretin your environment variables
# google client id
GOOGLE_CLIENT_ID=
# google client secret
GOOGLE_CLIENT_SECRET=Configure Github OAuth
- Log in to Github
- Go to OAuth Apps to create an OAuth application
- Configure
Homepage URLashttp://localhost:3000orhttps://your-domain - Configure
Authorization callback URLashttp://localhost:3000/api/auth/callback/githuborhttps://your-domain/api/auth/callback/github - Configure the
clientIdandclientSecretin your environment variables
# github client id
GITHUB_CLIENT_ID=
# github client secret
GITHUB_CLIENT_SECRET=Plugins
Easy SaaS Next uses plugins to enhance better-auth functionality.
Admin Plugin
The admin plugin is one of the better-auth plugins used for user management by administrators or route protection.
You only need to find the user table in your database and set the role field of your account to admin.
For more details, please check the better-auth admin plugin documentation.
Usage
Using in Client-side
import { useUser } from '@/components/user-provider'
const { data: session, isPending, error } = useUser()Using in Server-side
import { headers } from 'next/headers'
import { auth } from '@/lib/auth'
const session = await auth.api.getSession({
headers: await headers(),
})