Skip to content

Commit

Permalink
add signin function to example app
Browse files Browse the repository at this point in the history
  • Loading branch information
i2bskn committed Nov 22, 2014
1 parent 7a86ceb commit fdd4ccb
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 58 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class SessionsController < ApplicationController
def destroy
# Clear session (Only user_id)
signout!
redirect_to some_url
end
end
```
Expand Down
4 changes: 4 additions & 0 deletions example/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ gem 'jbuilder', '~> 2.0'

gem 'passwd', path: File.expand_path("../..", __FILE__)

group :development do
gem 'pry-rails'
end

2 changes: 1 addition & 1 deletion example/app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

1 change: 1 addition & 0 deletions example/app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
*= require_tree .
*= require_self
*/

5 changes: 5 additions & 0 deletions example/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception

# Require authentication all access!
# Specified `skil_before_action` if not authenticate
before_action :require_signin
end

5 changes: 5 additions & 0 deletions example/app/controllers/root_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RootController < ApplicationController
# GET /
def index; end
end

29 changes: 29 additions & 0 deletions example/app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class SessionsController < ApplicationController
# If you has been enabled `require_signin` in ApplicationController
skip_before_action :require_signin

# GET /signin
def new; end

# POST /signin
def create
# Returns nil or user
@user = User.authenticate(params[:email], params[:password])

if @user
# Save user_id to session
signin!(@user)
redirect_to root_url
else # Authentication fails
render action: :new
end
end

# DELETE /signout
def destroy
# Clear session (Only user_id)
signout!
redirect_to signin_url
end
end

5 changes: 3 additions & 2 deletions example/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<html>
<head>
<title>Example</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
</head>
<body>
Expand All @@ -12,3 +12,4 @@

</body>
</html>

4 changes: 4 additions & 0 deletions example/app/views/root/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>Hello <%= current_user.name %>!!</h1>

<%= link_to "Signout!", signout_path, method: :delete, data: { confirm: "Are you sure?" } %>

6 changes: 6 additions & 0 deletions example/app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= form_tag create_session_path do %>
<%= email_field_tag :email, @email, placeholder: "E-Mail", required: true %>
<%= password_field_tag :password, nil, placeholder: "Password", required: true %>
<%= submit_tag "Signin" %>
<% end %>

10 changes: 10 additions & 0 deletions example/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,19 @@ class Application < Rails::Application
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
config.time_zone = "Tokyo"
config.active_record.default_timezone = :local

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
I18n.enforce_available_locales = false
config.i18n.default_locale = :ja

config.generators do |g|
g.stylesheets false
g.javascripts false
g.helper false
end
end
end
2 changes: 1 addition & 1 deletion example/config/initializers/passwd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@

# Redirect path when not signin
# E.G. :signin_path # Do not specify ***_url
Rails.application.config.passwd.redirect_to = nil
Rails.application.config.passwd.redirect_to = :signin_path

64 changes: 10 additions & 54 deletions example/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,56 +1,12 @@
Rails.application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

# You can have the root of your site routed with "root"
# root 'welcome#index'

# Example of regular route:
# get 'products/:id' => 'catalog#view'

# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products

# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end

# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end

# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end

# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable

# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
controller :sessions do
get :signin, to: :new, as: :signin
post :signin, to: :create, as: :create_session
delete :signout, to: :destroy, as: :signout
end

controller :root do
root to: :index
end
end

12 changes: 12 additions & 0 deletions example/lib/tasks/user.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace :user do
desc "User create for development"
task :create => :environment do
User.find_or_create_by(email: "develop@example.com") do |u|
u.name = "developer"
u.salt = "fdb4bb5a745232d43874088c8edfcbaf5975d0d1e254016015991f1f3c4fc7061fd2f379b7d1f40504ddd1ff2f5feeb2caa0df895af5cb036b39f53f5ca79b04"
# password => "secret"
u.password = "636641d912523d7868614deed094797bfa3e9d504e0e01ab76ee2c7246555b41ff657169ac1ddbba9972fc324bc48c7fe479c0d2858105300ee203df6aeb53cc"
end
end
end

0 comments on commit fdd4ccb

Please sign in to comment.