Trying to get authlogic running

This commit is contained in:
Christoph Haas 2015-07-13 18:42:36 +02:00
parent 06f09c0486
commit 737ff62cfc
23 changed files with 306 additions and 4 deletions

View file

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

View file

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

View file

@ -0,0 +1,3 @@
// Place all the styles related to the user_sessions controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View file

@ -0,0 +1,3 @@
// Place all the styles related to the users controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View file

@ -5,6 +5,12 @@ class ApplicationController < ActionController::Base
before_filter :create_user_token
# Do not mention passwords in the log file
# filter_parameter_logging :password
# AuthLogic
helper_method :current_user_session, :current_user
# Assign the visitor a unique hash so that we can track their uploads
# and show them even before moderation.
def create_user_token
@ -12,4 +18,46 @@ class ApplicationController < ActionController::Base
session[:ip] ||= request.remote_ip
end
# -----------------
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
def require_user
logger.debug "ApplicationController::require_user"
unless current_user
store_location
flash[:notice] = "You must be logged in to access this page"
redirect_to new_user_session_url
return false
end
end
def require_no_user
logger.debug "ApplicationController::require_no_user"
if current_user
store_location
flash[:notice] = "You must be logged out to access this page"
# redirect_to home_index_path
return false
end
end
def store_location
#session[:return_to] = request.request_uri
end
def redirect_back_or_default(default)
redirect_to(session[:return_to] || default)
session[:return_to] = nil
end
end

View file

@ -0,0 +1,22 @@
class UserSessionsController < ApplicationController
def new
@user_session = UserSession.new
end
def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:notice] = "Login successful!"
redirect_back_or_default account_url(@current_user)
else
render :action => :new
end
end
def destroy
current_user_session.destroy
flash[:notice] = "Logout successful!"
redirect_back_or_default new_user_session_url
end
end

View file

@ -0,0 +1,43 @@
class UsersController < ApplicationController
before_filter :require_user, :only => [:show, :edit, :update]
def new
@user = User.new
end
def create
@user = User.new(params[:user])
# Saving without session maintenance to skip
# auto-login which can't happen here because
# the User has not yet been activated
if @user.save
flash[:notice] = "Your account has been created."
redirect_to signup_url
else
flash[:notice] = "There was a problem creating you."
render :action => :new
end
end
def show
@user = current_user
end
def edit
@user = current_user
end
def update
@user = current_user # makes our views "cleaner" and more consistent
if @user.update_attributes(params[:user])
flash[:notice] = "Account updated!"
redirect_to account_url
else
render :action => :edit
end
end
end

View file

@ -0,0 +1,2 @@
module UserSessionsHelper
end

View file

@ -0,0 +1,2 @@
module UsersHelper
end

View file

@ -1,2 +1,5 @@
class User < ActiveRecord::Base
acts_as_authentic do |c|
c.login_field = 'email'
end
end

View file

@ -0,0 +1,7 @@
# User sessions. Refers to the "User" class automatically (by its name).
# See also: https://github.com/binarylogic/authlogic
class UserSession < Authlogic::Session::Base
# specify configuration here, such as:
# logout_on_timeout true
# ...many more options in the documentation
end

View file

@ -0,0 +1,6 @@
- if target.errors.any?
div id="errorExplanation"
h2 Errors
ul
- target.errors.full_messages.each do |msg|
li = msg

View file

@ -0,0 +1,15 @@
- raise @user_session
- form_for @user_session, :as => :user_session, :url => { :action => "create" } do |f|
= render "shared/error_messages", :target => @user_session
= f.label :email
br
= f.text_field :email
br
= f.label :password
br
= f.password_field :password
br
= f.check_box :remember_me
= f.label :remember_me
br
= f.submit "Login"

View file

@ -0,0 +1,20 @@
= render "shared/error_messages", :target => @user
p
= form.label :name %
br
= form.text_field :name
p
= form.label :email
br
= form.text_field :email
p
= form.label :password, form.object.new_record? ? nil : "Change password"
br
= form.password_field :password
p
= form.label :password_confirmation
br
= form.password_field :password_confirmation
= link_to 'Edit Account', edit_account_path

28
app/views/users/show.slim Normal file
View file

@ -0,0 +1,28 @@
p
b Email:
=h @user.email
p
b Login count:
=h @user.login_count
p
b Last request at:
=h @user.last_request_at
p
b Last login at:
=h @user.last_login_at
p
b Current login at:
=h @user.current_login_at
p
b Last login ip:
=h @user.last_login_ip
p
b Current login ip:
=h @user.current_login_ip