Rails 8 Native Authentication Generator
Commandby davila7 · Added 5mo ago
Claude
Install
npx claude-code-templates@latest --command=cli-tool/templates/ruby/examples/rails-app/.claude/commands --yesAdd to Claude
claude mcp add rails-8-native-authentication-generatorAbout
Rails 8 Native Authentication Generator
Generate Rails 8's built-in authentication system with modern security practices.
Purpose
This command helps you implement Rails 8's new native authentication system, eliminating the need for external gems like Devise for basic authentication needs.
Usage
/authentication
What this command does
- Generates authentication models with secure password handling
- Creates authentication controllers for login/logout/signup
- Adds authentication views with modern styling
- Implements session management with security best practices
- Sets up authentication helpers for controllers and views
Rails 8 Authentication Generator
# Generate authentication for User model
bin/rails generate authentication User
# Or specify custom model name
bin/rails generate authentication Account
# Generate with custom attributes
bin/rails generate authentication User first_name:string last_name:string
Generated User Model
# app/models/user.rb
class User < ApplicationRecord
has_secure_password
validates :email, presence: true, uniqueness: true
validates :password, length: { minimum: 8 }, if: -> { new_record? || !password.blank? }
normalizes :email, with: ->(email) { email.strip.downcase }
before_save :normalize_email
private
def normalize_email
self.email = email.downcase.strip
end
end
Authentication Controller
# app/controllers/authentication_controller.rb
class AuthenticationController < ApplicationController
skip_before_action :authenticate_user!, only: [:new, :create]
def new
# Login page
end
def create
user = User.find_by(email: params[:email])
if user&.authenticate(params[:password])
login(user)
redirect_to root_path, notice: 'Logged in successfully'
else
flash.now[:alert] = 'Invalid email or password'
render :new, status: :unprocessable_entity
end
end
Tags
CodeAnalysisaitmplclaude-code-templates