Permalink
Browse files

Changed signups to use traditional form.

  • Loading branch information...
1 parent 0d74900 commit 84ad59779022a263663089a47c736eabefc3fdfb @barrym committed Feb 8, 2012
@@ -15,42 +15,50 @@ def authenticate
def process_twitter_callback
omniauth = request.env["omniauth.auth"]
- user = User.find_by_uid(omniauth['uid'])
- if user
- sign_in_and_redirect(:user, user)
- else
- if params[:invite] == 'blahblahblah'
- user = User.find_by_uid(omniauth['uid'])
- if user
- flash[:notice] = "This account has already signed up."
- sign_in_and_redirect(:user, user)
- else
- user = User.new(
- :uid => omniauth['uid'],
- :oauth_token => omniauth['credentials']['token'],
- :oauth_token_secret => omniauth['credentials']['secret'],
- :nickname => omniauth['info']['nickname'],
- :name => omniauth['info']['name'],
- :image => omniauth['info']['image']
- )
- user.save!
- sign_in_and_redirect(:user, user)
- end
+ case params[:type]
+ when 'link_account'
+ when 'signin'
+ user = User.find_by_twitter_uid(omniauth['uid'])
+
+ if user
+ flash[:success] = "Successfully signed in."
+ sign_in_and_redirect(:user, user)
else
- flash[:error] = "Sorry, the account #{omniauth['info']['nickname']} isn't registered."
+ flash[:error] = "Sorry, the Twitter user #{omniauth['info']['nickname']} isn't linked to any accounts."
redirect_to new_user_session_path
end
- end
- end
-
- def sign_up
- if params[:invite] == 'blahblahblah'
- redirect_to "/auth/twitter?invite=blahblahblah"
else
- flash[:error] = "Sorry, that invite code was not recognised."
- redirect_to new_user_registration_path
+ render :text => params
end
+
+ # user = User.find_by_uid(omniauth['uid'])
+
+ # if user
+ # sign_in_and_redirect(:user, user)
+ # else
+ # if params[:invite] == 'blahblahblah'
+ # user = User.find_by_uid(omniauth['uid'])
+ # if user
+ # flash[:notice] = "This account has already signed up."
+ # sign_in_and_redirect(:user, user)
+ # else
+ # user = User.new(
+ # :uid => omniauth['uid'],
+ # :oauth_token => omniauth['credentials']['token'],
+ # :oauth_token_secret => omniauth['credentials']['secret'],
+ # :nickname => omniauth['info']['nickname'],
+ # :name => omniauth['info']['name'],
+ # :image => omniauth['info']['image']
+ # )
+ # user.save!
+ # sign_in_and_redirect(:user, user)
+ # end
+ # else
+ # flash[:error] = "Sorry, the account #{omniauth['info']['nickname']} isn't registered."
+ # redirect_to new_user_session_path
+ # end
+ # end
end
def no_welcome
@@ -1,5 +1,25 @@
class UsersController < ApplicationController
+ def new
+ @user = User.new
+ end
+
+ def create
+ logger.info session
+ @user = User.new(
+ :email => params[:email],
+ :nickname => params[:nickname],
+ :password => params[:password],
+ :invite_code => params[:invite_code]
+ )
+ if @user.valid?
+ @user.save!
+ sign_in_and_redirect(:user, @user)
+ else
+ render :new
+ end
+ end
+
def show
@user = User.find(params[:id])
end
@@ -1,2 +1,11 @@
module ApplicationHelper
+
+ def error_on(model, attribute)
+ !model.errors[attribute].blank?
+ end
+
+ def errors_for(model, attribute)
+ model.errors[attribute].join(", ").capitalize
+ end
+
end
View
@@ -6,12 +6,22 @@ class User < ActiveRecord::Base
has_many :comments, :dependent => :destroy
has_many :commented_links, :through => :comments, :source => :comment # TODO: these aren't unique when returned
+ validates_presence_of :email, :nickname, :password
+ validates_uniqueness_of :email
+ validate :invite_code_is_valid
+
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable #, :validatable
# Setup accessible (or protected) attributes for your model
- attr_accessible :email, :password, :password_confirmation, :remember_me, :uid, :oauth_token, :oauth_token_secret, :nickname, :name, :image, :show_welcome
+ attr_accessible :email, :password, :password_confirmation, :remember_me, :twitter_uid, :twitter_oauth_token, :twitter_oauth_secret, :nickname, :name, :image, :show_welcome, :invite_code
+
+ def invite_code_is_valid
+ if self.invite_code != 'blahblahblah'
+ errors.add(:invite_code, 'is invalid')
+ end
+ end
end
@@ -1,25 +0,0 @@
-<h2>Edit <%= resource_name.to_s.humanize %></h2>
-
-<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
- <%= devise_error_messages! %>
-
- <div><%= f.label :email %><br />
- <%= f.email_field :email %></div>
-
- <div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
- <%= f.password_field :password %></div>
-
- <div><%= f.label :password_confirmation %><br />
- <%= f.password_field :password_confirmation %></div>
-
- <div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
- <%= f.password_field :current_password %></div>
-
- <div><%= f.submit "Update" %></div>
-<% end %>
-
-<h3>Cancel my account</h3>
-
-<p>Unhappy? <%= link_to "Cancel my account", registration_path(resource_name), :confirm => "Are you sure?", :method => :delete %>.</p>
-
-<%= link_to "Back", :back %>
@@ -1,7 +0,0 @@
-%h1 Enter your invite code to sign up
-%form{:method => :post, :action => '/site/sign_up'}
- %input{:type => :text, :id => :invite, :name => :invite}
- = submit_tag "Sign up", :class => "btn btn-primary"
-
-%p
- Signups require a Twitter account. If you don't have one then #{link_to('go get one', 'http://twitter.com')}.
@@ -1,4 +1,27 @@
-%p= link_to image_tag('sign-in-with-twitter-l.png'), '/auth/twitter'
+%h2 Sign in
+%p= link_to image_tag('sign-in-with-twitter-l.png'), '/auth/twitter?type=signin'
+
+= form_for(resource, :as => resource_name, :url => session_path(resource_name), :class => 'form-horizontal') do |f|
+ %fieldset
+ .control-group
+ = f.label :email
+ .controls
+ = f.email_field :email
+
+ .control-group
+ = f.label :password
+ .controls
+ = f.password_field :password
+
+ -if devise_mapping.rememberable?
+ .control-group
+ = f.check_box :remember_me
+ = f.label :remember_me
+
+ .control-group
+ .controls
+ = submit_tag "Sign in", :class => "btn btn-primary"
+
%p
or
= link_to 'sign up.', new_user_registration_path
@@ -36,11 +36,14 @@
.content
#alerts
- if flash[:notice]
- .alert-message.info
+ .alert.alert-info
= flash[:notice]
- if flash[:error]
- .alert-message.error
+ .alert.alert-error
= flash[:error]
+ - if flash[:success]
+ .alert.alert-success
+ = flash[:success]
#main
=yield
@@ -0,0 +1,40 @@
+%h1 Sign Up
+
+- unless @user.errors.blank?
+ .alert.alert-error
+ %strong Oops!
+ Some errors occured.
+
+%form.form-horizontal{:method => :post, :action => user_registration_path}
+ %fieldset
+ .control-group{:class => (error_on(@user, :email) ? 'error' :'')}
+ = label_tag :email, nil, :class => 'control-label'
+ .controls
+ = text_field_tag :email, @user.email, :autofocus => true
+ - if error_on(@user, :email)
+ %span.help-inline
+ = errors_for(@user, :email)
+ .control-group{:class => (error_on(@user, :nickname) ? 'error' :'')}
+ = label_tag :nickname, nil, :class => 'control-label'
+ .controls
+ = text_field_tag :nickname, @user.nickname
+ - if error_on(@user, :nickname)
+ %span.help-inline
+ = errors_for(@user, :nickname)
+ .control-group{:class => (error_on(@user, :password) ? 'error' :'')}
+ = label_tag :password, nil, :class => 'control-label'
+ .controls
+ = password_field_tag :password, @user.password
+ - if error_on(@user, :password)
+ %span.help-inline
+ = errors_for(@user, :password)
+ .control-group{:class => (error_on(@user, :invite_code) ? 'error' :'')}
+ = label_tag :invite_code, nil, :class => 'control-label'
+ .controls
+ = text_field_tag :invite_code, @user.invite_code
+ - if error_on(@user, :invite_code)
+ %span.help-inline
+ = errors_for(@user, :invite_code)
+ .control-group
+ .controls
+ = submit_tag "Sign up", :class => "btn btn-primary"
View
@@ -2,10 +2,9 @@
match '/auth/twitter/callback' => 'site#process_twitter_callback'
match '/add' => 'links#add', :as => 'links_add'
- match '/site/sign_up' => 'site#sign_up'
match '/site/no_welcome' => 'site#no_welcome', :as => 'no_welcome'
- devise_for :users
+ devise_for :users, :controllers => {:registrations => "users"}
resources :links do
resources :comments
@@ -0,0 +1,7 @@
+class RenameTwitterColumnsForUser < ActiveRecord::Migration
+ def change
+ rename_column :users, :uid, :twitter_uid
+ rename_column :users, :oauth_token, :twitter_oauth_token
+ rename_column :users, :oauth_token_secret, :twitter_oauth_secret
+ end
+end
@@ -0,0 +1,5 @@
+class AddInviteCodeToUser < ActiveRecord::Migration
+ def change
+ add_column :users, :invite_code, :string
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120202192138) do
+ActiveRecord::Schema.define(:version => 20120207220453) do
create_table "comments", :force => true do |t|
t.integer "link_id"
@@ -69,9 +69,9 @@
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "authentication_token"
- t.string "uid"
- t.string "oauth_token"
- t.string "oauth_token_secret"
+ t.string "twitter_uid"
+ t.string "twitter_oauth_token"
+ t.string "twitter_oauth_secret"
t.string "nickname"
t.string "name"
t.string "image"
@@ -81,6 +81,7 @@
t.integer "likes_count"
t.integer "comments_count"
t.boolean "show_welcome", :default => true
+ t.string "invite_code"
end
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true

0 comments on commit 84ad597

Please sign in to comment.