| title | description |
|---|---|
Authentication Setup Guide |
Description of your new file. |
This guide explains how to set up GitHub OAuth and email-based authentication for KubeBrowse.
-
GitHub OAuth: Sign in with GitHub account
-
Email/Password Authentication: Traditional email and password sign-in
-
Server-side Sessions: Secure session management with database persistence
-
Frontend Integration: React components with authentication context
-
Session Management: Automatic session validation and cleanup
-
Database: PostgreSQL database for user and session storage
-
GitHub OAuth App: GitHub OAuth application for OAuth authentication
-
Environment Variables: Properly configured environment variables
```bash
psql -U postgres
CREATE DATABASE kubebrowse;
\c kubebrowse;
```
```bash
migrate -path db/migrations -database "postgres://user:password@localhost:5432/kubebrowse?sslmode=disable" up
```
```bash
sqlc generate
```
-
Go to GitHub Settings → Developer settings → OAuth Apps
-
Click "New OAuth App"
-
Fill in the details:
-
Application name: KubeBrowse
-
Homepage URL:
http://localhost:3000(or your domain) -
Authorization callback URL:
http://localhost:4567/auth/oauth/github/callback
-
-
Click "Register application"
-
Note down the Client ID and Client Secret
Create or update your .env file:
```bash
DATABASE_URL=postgres://postgresuser:postgrespassword@localhost:5432/kubebrowse?sslmode=disable
GITHUB_CLIENT_ID=your_github_client_id_here
GITHUB_CLIENT_SECRET=your_github_client_secret_here
GITHUB_CALLBACK_URL=http://localhost:4567/auth/oauth/github/callback
FRONTEND_URL=http://localhost:3000
SESSION_SECRET=your_long_random_session_secret_here_make_it_very_secure
GUACD_ADDRESS=localhost:4822
REDIS_HOST=localhost
```
Create a .env.local file in the frontend directory:
```bash
VITE_API_URL=http://localhost:4567
```
```bash
go build ./cmd/guac
./guac
```
```bash
cd frontend
npm install
pnpm install
```
```bash
npm run dev
pnpm dev
```
| Method | Endpoint | Description |
|--------|----------|-------------|
| POST | /auth/register | Register with email/password |
| POST | /auth/login | Login with email/password |
| POST | /auth/logout | Logout current user |
| GET | /auth/oauth/github | Start GitHub OAuth flow |
| GET | /auth/oauth/github/callback | GitHub OAuth callback |
| GET | /auth/me | Get current user info |
```bash
curl -X POST http://localhost:4567/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "[user@example.com](mailto:user@example.com)",
"password": "securepassword123"
}'
```
```bash
curl -X POST http://localhost:4567/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "[user@example.com](mailto:user@example.com)",
"password": "securepassword123"
}'
```
```bash
curl -X GET http://localhost:4567/auth/me \
-H "Cookie: kubebrowse_session=your_session_token"
```
The authentication state is managed by AuthContext and can be accessed using the useAuth hook:
```jsx
import { useAuth } from '../context/AuthContext';
function MyComponent() {
const {
user,
isAuthenticated,
isLoading,
login,
register,
logout,
loginWithGitHub
} = useAuth();
if (isLoading) return <div>Loading...</div>;
if (!isAuthenticated) {
return \<LoginForm /\>;
}
return (
\<div\>
\<h1\>Welcome, {[user.name](http://user.name) || [user.email](http://user.email)}\!\</h1\>
\<button onClick={logout}\>Logout\</button\>
\</div\>
);
}
```
-
*
AuthModal**: Modal dialog for login/register -
*
LoginForm**: Email/password login form -
*
RegisterForm**: Email/password registration form -
*
UserMenu**: Dropdown menu for authenticated users
```sql
CREATE TABLE user_sessions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
session_token VARCHAR(255) NOT NULL UNIQUE,
expires_at TIMESTAMP NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);
```
-
Session Tokens: Use cryptographically secure random tokens
-
Password Hashing: Passwords are hashed using bcrypt with default cost
-
HTTPS: Use HTTPS in production for secure cookie transmission
-
Session Expiration: Sessions expire after 7 days by default
-
CORS: Configure CORS appropriately for your domain
-
Environment Variables: Never commit secrets to version control
-
Database Connection Error
-
Verify PostgreSQL is running
-
Check DATABASE_URL format
-
Ensure database exists and user has permissions
-
-
GitHub OAuth Not Working
-
Verify GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET
-
Check callback URL matches GitHub app settings
-
Ensure FRONTEND_URL is correct
-
-
Session Not Persisting
-
Check cookie settings (secure, httpOnly, domain)
-
Verify session secret is set
-
Check session expiration times
-
-
CORS Issues
-
Verify CORS configuration allows your frontend domain
-
Check credentials: 'include' in frontend requests
-
Enable debug logging:
```bash
export LOG_LEVEL=debug
./guac
```
```bash
DATABASE_URL=postgres://user:password@prod-db:5432/kubebrowse?sslmode=require
GITHUB_CLIENT_ID=prod_github_client_id
GITHUB_CLIENT_SECRET=prod_github_client_secret
GITHUB_CALLBACK_URL=https://yourdomain.com/auth/oauth/github/callback
FRONTEND_URL=https://yourdomain.com
SESSION_SECRET=very_long_random_string_for_production
```
-
[ ] Use HTTPS for all communications
-
[ ] Set secure session cookie flags
-
[ ] Use strong, unique session secrets
-
[ ] Configure proper CORS settings
-
[ ] Set up database connection pooling
-
[ ] Enable database SSL/TLS
-
[ ] Set up proper logging and monitoring
-
[ ] Configure rate limiting for auth endpoints
For issues and questions:
-
Check the troubleshooting section
-
Review the logs for error messages
-
Verify environment configuration
-
Check database connectivity and migrations