Skip to content

Instantly share code, notes, and snippets.

@andreas-it-dev
Last active October 24, 2016 08:00
Show Gist options
  • Save andreas-it-dev/3968e85e10cfee01db2a44a1bb43ed01 to your computer and use it in GitHub Desktop.
Save andreas-it-dev/3968e85e10cfee01db2a44a1bb43ed01 to your computer and use it in GitHub Desktop.
module ApplicationHelper
def current_user
User.find(session[:user_id]) if session[:user_id]
end
...
<snip>
...
end
class SessionsController < ApplicationController
def new
end
def create
if user = User.authenticate(params[:username], params[:password])
session[:user_id] = user.id
flash[:notice] = t('sessions.success_html')
redirect_to messages_path
else
flash[:error] = t('sessions.failure')
render :new
end
end
def destroy
session[:user_id] = nil
redirect_to root_path, notice: t('sessions.signedout')
end
end
class User < ApplicationRecord
has_secure_password
def self.authenticate(username, password)
user = User.find_by(username: username)
user && user.authenticate(password)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment