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
$ rails new User_Auth -d mysql |
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 CreateUsers < ActiveRecord::Migration | |
def change | |
create_table :users do |t| | |
t.string :username | |
t.string :email | |
t.string :encrypted_password | |
t.string :salt | |
t.timestamps | |
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
$ rake db:create | |
$ rake db:migrate |
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
gem 'bcrypt-ruby', :require => 'bcrypt' |
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
$ rails cd ./User_Auth | |
$ rails g model user | |
$ rails g controller users new |
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
<% @page_title = "UserAuth | Signup" %> | |
<div class="Sign_Form"> | |
<h1>Sign Up</h1> | |
<%= form_for(:user, :url => {:controller => 'users', :action => 'create'}) do |f| %> | |
<p> Username:</br> <%= f.text_field :username%> </p> | |
<p> Email:</br> <%= f.text_field :email%> </p> | |
<p> Password:</br> <%= f.password_field :password%></p> | |
<p> Password Confirmation:</br> <%= f.password_field :password_confirmation%> </p> | |
<%= f.submit :Signup %> | |
<% 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
require 'digest/sha1' | |
encrypted_password= Digest::SHA1.hexdigest(password) |
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
salt= Digest::SHA1.hexdigest("# We add {email} as unique value and #{Time.now} as random value") | |
encrypted_password= Digest::SHA1.hexdigest("Adding #{salt} to {password}") |
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 < ActiveRecord::Base | |
attr_accessor :password | |
EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i | |
validates :username, :presence => true, :uniqueness => true, :length => { :in => 3..20 } | |
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX | |
validates :password, :confirmation => true #password_confirmation attr | |
validates_length_of :password, :in => 6..20, :on => :create | |
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 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 |
OlderNewer