Authentication
Learn how to set up and use authentication with Better Auth in MkSaaS
MkSaaS uses Better Auth for authentication, providing a flexible and secure system with multiple authentication methods, session management, and role-based access control.
Setup
To set up authentication in MkSaaS, follow these steps to configure the necessary environment variables:
1. Generate Better Auth Secret Key
The BETTER_AUTH_SECRET
is a random string used for encryption and generating hashes. You can generate a secret key using the OpenSSL command:
After generating your secret key, add it to your .env
file:
2. Configure GitHub OAuth
To enable GitHub authentication, you need to register your application:
- Go to GitHub Developer Settings
- Click on "OAuth Apps" and then "New OAuth App"
- Fill in the registration form:
- Application name: Your application name (e.g., "MkSaaS")
- Homepage URL: Your site's URL (e.g.,
https://your-domain.com
orhttp://localhost:3000
for local development) - Application description: A brief description of your application
- Authorization callback URL: Set this to
https://your-domain.com/api/auth/callback/github
(orhttp://localhost:3000/api/auth/callback/github
for local development)
- Click "Register application"
- After registration, you'll see your Client ID
- Click "Generate a new client secret" to create your Client Secret
- Copy both values and add them to your
.env
file:
Create two different OAuth applications in GitHub - one for production and one for development. Never use the same OAuth application for both environments as they require different callback URLs.
3. Configure Google OAuth
To enable Google authentication, follow these steps to obtain client credentials:
- Go to the Google Cloud Console
- Create a new project or select an existing one
- Go to "Credentials" in the left sidebar
- Click "Create Credentials" and select "OAuth client ID"
- If this is your first time, you may need to configure the OAuth consent screen:
- User Type: Choose "External"
- Fill in the required information (app name, user support email, developer contact)
- Add authorized domains including your application domain
- Click "Save and Continue" through each section
- Return to "Credentials" and create the OAuth client ID:
- Application type: Web application
- Name: Your application name
- Authorized JavaScript origins: Add your domain (e.g.,
https://your-domain.com
orhttp://localhost:3000
) - Authorized redirect URIs: Add
https://your-domain.com/api/auth/callback/google
(orhttp://localhost:3000/api/auth/callback/google
for local development)
- Click "Create", copy the provided Client ID and Client Secret, add them to the
.env
file:
Create two different OAuth applications in Google Cloud Console - one for production and one for development. Never use the same OAuth application for both environments as they require different callback URLs.
If you are setting up the environment, now you can go back to the Environment Setup guide and continue. The rest of this guide can be read later.
Environment Setup
Set up environment variables
Authentication System Structure
The authentication system in MkSaaS is designed with the following components:
This structure follows Next.js best practices with the App Router and handles internationalization through the locale parameter.
Core Features
- Email and password authentication with email verification
- Password reset functionality
- Social login providers (Google, GitHub)
- Account linking between different authentication methods
- User management (including admin functionality)
- Session management with cookie-based caching
- Automatic newsletter subscription on registration (optional)
Client-Side Usage
Accessing Session Data
In client components, you can access the user's session data using the useSession
hook:
Authentication Operations
The auth client provides methods for common authentication operations:
Protecting Routes
Server-Side Route Protection
In server components, you can protect routes by checking the session:
Admin Route Protection
For admin-only routes:
Error Handling
Better Auth supports error handling and redirection:
Best Practices
- Require Email Verification: Always require email verification for new users
- Provide Multiple Authentication Methods: Offer both email/password and social logins
- Use Secure Session Settings: Configure appropriate session timeouts and cookie security
- Handle Errors Gracefully: Provide user-friendly error messages and fallbacks
- Protect Sensitive Routes: Implement proper authorization checks on both client and server
- Keep Auth Libraries Updated: Regularly update dependencies
- Consider Different User Roles: Implement proper role-based access controls
Next Steps
Now that you understand how authentication works in MkSaaS, explore these related integrations: