Make Persona the only login option (with the ability to access old accounts)
parent
bc2b91f5a2
commit
d4cbc3e504
@ -1,8 +1,13 @@
|
||||
p.notice, p.alert, p.error
|
||||
background: #396
|
||||
p.notice, p.alert
|
||||
color: white
|
||||
font-size: 15px
|
||||
height: 38px
|
||||
line-height: 36px
|
||||
margin: 0
|
||||
text-align: center
|
||||
|
||||
p.notice
|
||||
background: #396
|
||||
|
||||
p.alert
|
||||
background: #d52
|
||||
|
@ -0,0 +1,26 @@
|
||||
class AccountMergesController < ApplicationController
|
||||
|
||||
def create
|
||||
user = find_user
|
||||
|
||||
if user
|
||||
user.update_attribute(:email, store.delete(:new_user_email))
|
||||
self.current_user = user
|
||||
redirect_back_or_to root_url, notice: 'Welcome!'
|
||||
else
|
||||
redirect_to new_user_path,
|
||||
alert: 'Sorry, no account found. Try a different provider.'
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def store
|
||||
session
|
||||
end
|
||||
|
||||
def find_user
|
||||
User.for_credentials(omniauth_credentials)
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,21 @@
|
||||
class OmniAuthCredentials
|
||||
|
||||
attr_reader :omniauth_hash
|
||||
|
||||
def initialize(omniauth_hash)
|
||||
@omniauth_hash = omniauth_hash
|
||||
end
|
||||
|
||||
def provider
|
||||
omniauth_hash['provider']
|
||||
end
|
||||
|
||||
def uid
|
||||
omniauth_hash['uid']
|
||||
end
|
||||
|
||||
def email
|
||||
omniauth_hash['info'] && omniauth_hash['info']['email']
|
||||
end
|
||||
|
||||
end
|
@ -1,35 +0,0 @@
|
||||
<section class="supplimental">
|
||||
<div class="wrapper">
|
||||
<%= form_for @user, :url => '/user', :html => { :class => 'form-horizontal' } do |f| %>
|
||||
<fieldset>
|
||||
<legend>Your new account</legend>
|
||||
|
||||
<div class="control-group <%= 'error' if @user.errors[:nickname].present? %>">
|
||||
<%= f.label :nickname, 'Username', :class => 'control-label' %>
|
||||
<div class="controls">
|
||||
<%= f.text_field :nickname, :class => 'text_field' %>
|
||||
<span class="help-inline"><%= @user.errors[:nickname].first %></span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="control-group">
|
||||
<%= f.label :name, 'Real name', :class => 'control-label' %>
|
||||
<div class="controls">
|
||||
<%= f.text_field :name, :class => 'text_field' %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="control-group">
|
||||
<%= f.label :email, :class => 'control-label' %>
|
||||
<div class="controls">
|
||||
<%= f.text_field :email, :class => 'text_field' %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-actions">
|
||||
<%= f.submit 'Create', :class => 'btn btn-primary' %>
|
||||
</div>
|
||||
</fieldset>
|
||||
<% end %>
|
||||
</div>
|
||||
</section>
|
@ -0,0 +1,32 @@
|
||||
section.supplimental
|
||||
.wrapper
|
||||
p There is no account with email #{@user.email}.
|
||||
|
||||
h2 That's because I am here for the first time
|
||||
|
||||
h3 Pick your username:
|
||||
|
||||
= simple_form_for @user, :url => user_path do |f|
|
||||
= f.input :nickname, label: 'Username', required: true
|
||||
= f.button :submit
|
||||
|
||||
hr
|
||||
|
||||
/* p */
|
||||
/* | We recommend logging in with your email using */
|
||||
/* = link_to "Mozilla Persona", "https://login.persona.org/" */
|
||||
/* | . Look */
|
||||
/* = link_to "here", "https://developer.mozilla.org/en-US/docs/Persona" */
|
||||
/* | for more details. */
|
||||
|
||||
h2 That's because I've been logging in via Github/Twitter
|
||||
|
||||
p
|
||||
| If you have been previously logging in via Github or Twitter
|
||||
| pls do it again. One time!
|
||||
|
||||
ul.login
|
||||
li = link_to "Log in via Github", github_auth_path, :class => 'btn-auth btn-github'
|
||||
li = link_to "Log in via Twitter", twitter_auth_path, :class => 'btn-auth btn-twitter'
|
||||
|
||||
.vertical-expander
|
@ -0,0 +1,6 @@
|
||||
class AllowNullForUserProviderAndUid < ActiveRecord::Migration
|
||||
def change
|
||||
change_column :users, :provider, :string, null: true
|
||||
change_column :users, :uid, :string, null: true
|
||||
end
|
||||
end
|
@ -1,16 +0,0 @@
|
||||
class OmniAuthHelper
|
||||
|
||||
def self.get_avatar_url(auth)
|
||||
case auth["provider"]
|
||||
when "twitter"
|
||||
auth["info"]["image"]
|
||||
when "github"
|
||||
auth["extra"]["raw_info"]["avatar_url"]
|
||||
when "browser_id"
|
||||
email = auth["uid"]
|
||||
hash = Digest::MD5.hexdigest(email.to_s.downcase)
|
||||
"http://gravatar.com/avatar/#{hash}?s=64"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,65 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe AccountMergesController do
|
||||
|
||||
describe '#create' do
|
||||
subject { get :create, provider: 'twitter' }
|
||||
|
||||
let(:credentials) { double('credentials') }
|
||||
let(:store) { {} }
|
||||
|
||||
before do
|
||||
allow(controller).to receive(:omniauth_credentials) { credentials }
|
||||
allow(controller).to receive(:store) { store }
|
||||
allow(User).to receive(:for_credentials).with(credentials) { user }
|
||||
store[:new_user_email] = 'foo@bar.com'
|
||||
end
|
||||
|
||||
context "when user can be found for given credentials" do
|
||||
let(:user) { stub_model(User) }
|
||||
|
||||
before do
|
||||
allow(user).to receive(:update_attribute)
|
||||
allow(controller).to receive(:current_user=)
|
||||
|
||||
subject
|
||||
end
|
||||
|
||||
it 'updates the email on the user' do
|
||||
expect(user).to have_received(:update_attribute).
|
||||
with(:email, 'foo@bar.com')
|
||||
end
|
||||
|
||||
it 'removes the email from the store' do
|
||||
expect(store.key?(:new_user_email)).to be(false)
|
||||
end
|
||||
|
||||
it 'sets the current_user' do
|
||||
expect(controller).to have_received(:current_user=).with(user)
|
||||
end
|
||||
|
||||
it 'redirects to the root_path with a notice' do
|
||||
expect(flash[:notice]).to_not be_blank
|
||||
should redirect_to(root_path)
|
||||
end
|
||||
end
|
||||
|
||||
context "when user can't be found for given credentials" do
|
||||
let(:user) { nil }
|
||||
|
||||
before do
|
||||
subject
|
||||
end
|
||||
|
||||
it "doesn't remove the email from the store" do
|
||||
expect(store.key?(:new_user_email)).to be(true)
|
||||
end
|
||||
|
||||
it 'redirects to new user page with an alert' do
|
||||
expect(flash[:alert]).to_not be_blank
|
||||
should redirect_to(new_user_path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,40 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe OmniAuthCredentials do
|
||||
|
||||
let(:credentials) { described_class.new(omniauth_hash) }
|
||||
|
||||
let(:omniauth_hash) { {
|
||||
'provider' => 'twitter',
|
||||
'uid' => '1234567',
|
||||
'info' => { 'email' => 'foo@bar.com' }
|
||||
} }
|
||||
|
||||
describe '#provider' do
|
||||
subject { credentials.provider }
|
||||
|
||||
it { should eq('twitter') }
|
||||
end
|
||||
|
||||
describe '#uid' do
|
||||
subject { credentials.uid }
|
||||
|
||||
it { should eq('1234567') }
|
||||
end
|
||||
|
||||
describe '#email' do
|
||||
subject { credentials.email }
|
||||
|
||||
it { should eq('foo@bar.com') }
|
||||
|
||||
context "when no info section in hash" do
|
||||
let(:omniauth_hash) { {
|
||||
'provider' => 'twitter',
|
||||
'uid' => '1234567'
|
||||
} }
|
||||
|
||||
it { should be(nil) }
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -1,44 +0,0 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe OmniAuthHelper do
|
||||
describe ".avatar_url" do
|
||||
let(:avatar_url) { "http://foo.bar/foo.png" }
|
||||
|
||||
context "when github auth" do
|
||||
let(:auth) do
|
||||
{
|
||||
"provider" => "github",
|
||||
"extra" => {
|
||||
"raw_info" => {
|
||||
"avatar_url" => avatar_url
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
it { OmniAuthHelper.get_avatar_url(auth).should == avatar_url }
|
||||
|
||||
end
|
||||
|
||||
context "when twitter auth" do
|
||||
let(:auth) do
|
||||
{
|
||||
"provider" => "twitter",
|
||||
"info" => {
|
||||
"image" => avatar_url
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
it { OmniAuthHelper.get_avatar_url(auth).should == avatar_url }
|
||||
end
|
||||
|
||||
context "when other provider" do
|
||||
let(:auth) do
|
||||
{ "provider" => "other" }
|
||||
end
|
||||
|
||||
it { OmniAuthHelper.get_avatar_url(auth).should be_nil }
|
||||
end
|
||||
end
|
||||
end
|
@ -1,16 +0,0 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe "users/new" do
|
||||
let(:user) { FactoryGirl.build(:user) }
|
||||
|
||||
before do
|
||||
assign(:user, user)
|
||||
end
|
||||
|
||||
it "renders form with attr" do
|
||||
render
|
||||
rendered.should =~ /user\[nickname\]/
|
||||
rendered.should =~ /user\[name\]/
|
||||
rendered.should =~ /user\[email\]/
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue