Rails API application with Kakao OAuth2 authentication and JWT token-based authorization.
- Kakao OAuth2 Login
- JWT Token Authentication
- User Management
- Ruby 3.4.7
- Rails 8.1.1
- SQLite3
bundle installrails db:create
rails db:migrateEdit your credentials file:
EDITOR="vim" rails credentials:editAdd the following configuration:
development:
kakao:
client_id: your_kakao_client_id
client_secret: your_kakao_client_secret
production:
kakao:
client_id: your_kakao_client_id
client_secret: your_kakao_client_secretTo get Kakao OAuth credentials:
- Go to Kakao Developers
- Create an application
- Get your REST API Key (Client ID) and Client Secret
- Add redirect URI:
http://localhost:3000/auth/kakao/callback
Set the frontend URL environment variable (defaults to http://localhost:3001):
export FRONTEND_URL=http://localhost:3001rails serverWhen user wants to login:
- Frontend redirects browser to:
http://localhost:3000/auth/kakao - User completes Kakao login
- Backend redirects to:
http://localhost:3001/auth/callback?token=JWT_TOKEN
The frontend should handle the /auth/callback route to:
- Extract
tokenfrom URL query parameter - Store token in localStorage/sessionStorage
- Redirect user to main page
Example frontend code:
// On /auth/callback page
const urlParams = new URLSearchParams(window.location.search);
const token = urlParams.get('token');
if (token) {
localStorage.setItem('jwt_token', token);
window.location.href = '/dashboard';
}GET /auth/kakao
Redirects to Kakao login page.
GET /auth/kakao/callback
Handles OAuth callback and redirects to frontend with JWT token in URL.
Redirect example:
http://localhost:3001/auth/callback?token=eyJhbGc...
GET /auth/failure
Redirects to frontend with error message.
Include the JWT token in the Authorization header for protected endpoints:
Authorization: Bearer <your_jwt_token>
Example:
curl -H "Authorization: Bearer eyJhbGc..." http://localhost:3000/api/protected_resourcerails testrubocopbundle exec bundler-audit
bundle exec brakemanapp/
├── controllers/
│ ├── application_controller.rb # JWT authentication logic
│ └── auth/
│ └── callbacks_controller.rb # OAuth callback handling
├── models/
│ └── user.rb # User model with OAuth support
└── services/
└── json_web_token.rb # JWT encode/decode service
lib/
└── omniauth/
└── strategies/
└── kakao.rb # Custom Kakao OAuth2 strategy
config/
└── initializers/
└── omniauth.rb # OmniAuth configuration
This project is available as open source under the terms of the MIT License.