This file contains hidden or 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 login | |
#Login Form | |
end | |
def login_attempt | |
authorized_user = User.authenticate(params[:username_or_email],params[:login_password]) | |
if authorized_user | |
flash[:notice] = "Wow Welcome again, you logged in as #{authorized_user.username}" | |
redirect_to(:action => 'home') | |
else |
This file contains hidden or 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
<% @page_title = "UserAuth | Login" -%> | |
<div class= "Sign_Form"> | |
<h1>Log in</h1> | |
<%= form_tag(:action => 'login_attempt') do %> | |
<p>Username or Email:</br> <%= text_field_tag(:username_or_email) %></p> | |
<p>Password:</br> <%= password_field_tag :login_password %></p> | |
<%= submit_tag("Log In") %> | |
<% end %> | |
</div> |
This file contains hidden or 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
$ rails g controller sessions login, home, profile, setting |
This file contains hidden or 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
def self.authenticate(username_or_email="", login_password="") | |
if EMAIL_REGEX.match(username_or_email) | |
user = User.find_by_email(username_or_email) | |
else | |
user = User.find_by_username(username_or_email) | |
end | |
if user && user.match_password(login_password) | |
return user | |
else | |
return false |
This file contains hidden or 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
match ':controller(/:action(/:id))(.:format)' |
This file contains hidden or 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
attr_accessible :username, :email, :password, :password_confirmation |
This file contains hidden or 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
before_save :encrypt_password | |
after_save :clear_password | |
def encrypt_password | |
if password.present? | |
self.salt = BCrypt::Engine.generate_salt | |
self.encrypted_password= BCrypt::Engine.hash_secret(password, salt) | |
end | |
end | |
def clear_password | |
self.password = nil |
This file contains hidden or 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
salt = BCrypt::Engine.generate_salt | |
encrypted_password = BCrypt::Engine.hash_secret(password, salt) |
This file contains hidden or 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
$ bundle install |
This file contains hidden or 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 UsersController < ApplicationController | |
def new | |
@user = User.new | |
end | |
def create | |
@user = User.new(params[:user]) | |
if @user.save | |
flash[:notice] = "You signed up successfully" | |
flash[:color]= "valid" | |
else |