Prompt for username after login

This commit is contained in:
Marcin Kulik 2014-10-17 17:28:22 +02:00
parent 5cb64f0fda
commit 427c19638c
11 changed files with 150 additions and 9 deletions

View File

@ -16,12 +16,10 @@ $(function() {
this.select(); this.select();
}); });
$('input[data-behavior=focus]:first').focus(); $('input[data-behavior=focus]:first').focus().select();
$('#embed-link').click(function(e) { $('#embed-link').click(function(e) {
e.preventDefault(); e.preventDefault();
$('.embed-box').slideDown('fast'); $('.embed-box').slideDown('fast');
}); });
$('.login-form input[type=email]').focus();
}); });

View File

@ -3,6 +3,10 @@
width: 250px width: 250px
margin-right: 10px margin-right: 10px
.username-form
input#user_username
width: 200px
.profile-page .profile-page
.cinema .cinema
.user-avatar .user-avatar

View File

@ -5,7 +5,7 @@ class SessionsController < ApplicationController
if user if user
self.current_user = user self.current_user = user
redirect_back_or_to profile_path(user), notice: login_notice(user) redirect_to_profile(user)
else else
render :error render :error
end end
@ -22,6 +22,14 @@ class SessionsController < ApplicationController
EmailLoginService.new EmailLoginService.new
end end
def redirect_to_profile(user)
if user.username
redirect_back_or_to profile_path(user), notice: login_notice(user)
else
redirect_to new_username_path, notice: login_notice(user)
end
end
def login_notice(user) def login_notice(user)
if user.first_login? if user.first_login?
"Welcome to Asciinema!" "Welcome to Asciinema!"

View File

@ -0,0 +1,34 @@
class UsernamesController < ApplicationController
before_filter :ensure_authenticated!
def new
@user = load_user
end
def create
@user = load_user
if @user.update(username: params[:user][:username].strip)
redirect_to_profile(@user)
else
@invalid_username = true
render :new
end
end
def skip
redirect_to_profile(current_user)
end
private
def load_user
User.find(current_user.id)
end
def redirect_to_profile(user)
redirect_back_or_to profile_path(user)
end
end

View File

@ -43,7 +43,7 @@ class UserPagePresenter
count = h.pluralize(user.asciicast_count, 'asciicast') count = h.pluralize(user.asciicast_count, 'asciicast')
"You have recorded #{count}" "You have recorded #{count}"
else else
"You haven't recorded anything yet" "Record your first asciicast"
end end
else else
if user.asciicast_count > 0 if user.asciicast_count > 0

View File

@ -9,7 +9,7 @@
= form_tag login_path, class: "form-inline login-form" do = form_tag login_path, class: "form-inline login-form" do
.form-group .form-group
input.form-control.email name="email" type="email" placeholder="Enter email" input.form-control.email name="email" type="email" placeholder="Enter email" data-behavior="focus"
button.btn.btn-primary type="submit" Log in button.btn.btn-primary type="submit" Log in
- if @invalid_email - if @invalid_email

View File

@ -0,0 +1,28 @@
.container
.row
.col-md-12
h1 Choose your username
br
p
' Every Asciinema user gets a profile page at
a href="#"
| #{root_url}~
strong username
' .
= form_for @user, url: username_path, method: :post, html: { class: "username-form" } do |f|
.form-group
= f.label :username, 'Your username:'
= f.text_field :username, class: 'form-control', 'data-behavior' => 'focus'
- if @invalid_username
br
p.text-danger
' Use only letters, digits and "-" character.
' Examples:
em johnny-knoxville, destroyer666
.form-group
= f.submit 'Continue', class: 'btn btn-primary'
a.btn href=skip_username_path I'll do it later

View File

@ -1,7 +1,7 @@
markdown: markdown:
Make sure you have asciinema recorder [installed](#{docs_path(:installation)}). Make sure you have asciinema recorder [installed](#{docs_path(:installation)}).
To start recording go to your terminal and run the following command: To start recording run the following command in your terminal:
$ asciinema rec $ asciinema rec

View File

@ -35,6 +35,10 @@ Rails.application.routes.draw do
resource :user resource :user
resource :username do
get :skip
end
root 'home#show' root 'home#show'
get '/about' => 'pages#show', page: :about, as: :about get '/about' => 'pages#show', page: :about, as: :about

View File

@ -25,9 +25,24 @@ describe SessionsController do
expect(controller).to have_received(:current_user=).with(user) expect(controller).to have_received(:current_user=).with(user)
end end
it "redirects to the user's profile with a notice" do it "sets a notice" do
expect(flash[:notice]).to_not be_blank expect(flash[:notice]).to_not be_blank
should redirect_to(unnamed_user_path(user)) end
context "when user has username" do
let(:user) { User.new(username: "foobar") }
it "redirects to user's profile" do
should redirect_to(public_profile_path(username: "foobar"))
end
end
context "when user has no username" do
let(:user) { User.new }
it "redirects to new username page" do
should redirect_to(new_username_path)
end
end end
end end

View File

@ -0,0 +1,50 @@
require 'rails_helper'
describe UsernamesController do
let(:user) { stub_model(User) }
before do
login_as user
allow(User).to receive(:find) { user }
end
describe "#new" do
subject { get :new }
it "renders 'new' template" do
should render_template('new')
end
end
describe "#create" do
subject { post :create, user: { username: 'doppelganger' } }
before do
allow(user).to receive(:update).with(username: 'doppelganger') { success }
subject
end
context "when username is updated" do
let(:success) { true }
it "redirects to user's profile" do
should redirect_to(unnamed_user_path(user))
end
end
context "when username is not updated" do
let(:success) { false }
it "displays error" do
should render_template('new')
end
end
end
describe "#skip" do
pending
end
end