Skip to content

Instantly share code, notes, and snippets.

@RJNY
Last active August 29, 2015 14:02
Show Gist options
  • Save RJNY/87f24d2a1f2d96d2fcef to your computer and use it in GitHub Desktop.
Save RJNY/87f24d2a1f2d96d2fcef to your computer and use it in GitHub Desktop.
BCrypt cheat sheet
# BCrypt Outline
# Config/Environment
require 'bcrypt'
# Gemfile
gem "bcrypt"
# Migration
t.string :password_hash, null: false
#############################################
################# Model #####################
#############################################
include BCrypt
def password
@password ||= Password.new(password_hash)
end
def password=(pass)
@entered_password = pass
@password = Password.create(pass)
self.password_hash = @password
end
def self.authenticate(email, password)
user = User.find_by_email(email)
return user if user && (user.password == password)
nil
end
#############################################
###############Controller####################
#############################################
get '/' do
display_errors
@user = User.find_by_id(session[:user_id])
erb :index
end
post '/sign_in' do
@email = params[:email]
user = User.authenticate(@email, params[:password])
if user
session[:user_id] = user.id
redirect '/'
# change redirect as needed
else
session[:error] = "Invalid email or password."
redirect '/'
end
end
get '/sign_out' do
session.clear
redirect '/'
end
post '/sign_up' do
@user = User.new params[:user]
if @user.save
session[:user_id] = @user.id
redirect '/'
# change redirect as needed
else
session[:error] = "Oops, something went wrong! Please try again"
redirect '/'
end
end
# not_found do
# status 404
# erb :oops
# end
# get '/user/home' do
# @current_user = User.find(session[:user_id])
# @user_proficiencies = @current_user.proficiencies
# erb :index
# end
#############################################
################# View ######################
#############################################
#Sign In
<div class="container"><br><br>
<% if @error %>
<%= @error %>
<% end %>
<% if @user %>
<%= @user.username %>
<a href="/sign_out">Sign out</a>
<% end %>
<form id="sign-in" method="post" action="/sign_in">
<input type="email" name="email" placeholder="email" value="<%= %>"><br>
<input type="password" name="password" placeholder="password">
<br>
<div class="submit"><input type="submit" value="Sign In"></div>
</form><br><br>
<form id="sign-up" method="post" action="/sign_up">
<input type="text" name="user[first_name]" placeholder="First Name" value="<%= %>">
<span id="name-errors" class="errors"></span>
<br>
<input type="text" name="user[last_name]" placeholder="Last Name" value="<%= %>">
<span id="name-errors" class="errors"></span>
<br>
<input type="text" name="user[username]" placeholder="Username" value="<%= %>">
<span id="name-errors" class="errors"></span>
<br>
<input type="email" name="user[email]" placeholder="email" value="<%= %>">
<span id="email-errors" class="errors"></span>
<br>
<input type="password" name="user[password]" placeholder="password">
<span id="entered_password-errors" class="errors"></span>
<br>
<div class="submit"><input type="submit" value="Sign Up"></div>
</form>
</div>
# Seed
my_password = BCrypt::Password.create("password")
User.create(password_hash: "#{my_password}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment