Skip to content

Instantly share code, notes, and snippets.

View kelhusseiny's full-sized avatar
🤡
How about another joke, Murray?

Karim El-Husseiny kelhusseiny

🤡
How about another joke, Murray?
View GitHub Profile
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
<% @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>
$ rails g controller sessions login, home, profile, setting
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
match ':controller(/:action(/:id))(.:format)'
attr_accessible :username, :email, :password, :password_confirmation
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
salt = BCrypt::Engine.generate_salt
encrypted_password = BCrypt::Engine.hash_secret(password, salt)
$ bundle install
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