Last active
October 24, 2016 08:00
-
-
Save andreas-it-dev/3968e85e10cfee01db2a44a1bb43ed01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper | |
def current_user | |
User.find(session[:user_id]) if session[:user_id] | |
end | |
... | |
<snip> | |
... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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