Guest User

Untitled

a guest
Apr 12th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. It is best to separate the main controller from the others. All calls should be to the login controller.
  2.  
  3. ## login_controller.rb [ruby]
  4. before_filter :enable_login # you can write a before_filter to load the @user object that will be needed after login
  5. def show_login_index
  6. # the rjs here is small enough that you can write it directly into the controller if you wish
  7. # (or wrap the contained code into an rjs template named 'show_login_index')
  8. # note that the purpose of this specific action is the extra effect
  9. render :update do |page|
  10. page.replace_html 'ajax_login', :partial => 'login/index'
  11. page.visual_effect :shake, 'login_status', :duration => 1.5
  12. end
  13. end
  14.  
  15. def show_login
  16. # ajax action
  17. render :update do |page|
  18. page.replace_html 'ajax_login', :partial => 'login/login'
  19. page.hide 'ajax_login' # I believe you can switch the page.hide first if you wish
  20. page.visual_effect :blind_down, 'ajax_login', :duration => 1.5
  21. end
  22. end
  23.  
  24. def login
  25. # the UserStore object is just something that I use to keep track of login attempts... it's probably not the safest as it shows the real password in the db... replace it with your login method
  26. if request.post?
  27. @user_store = UserStore.new(params[:user_store])
  28. if @user_store.save
  29. user = User.authenticate(@user_store.name, @user_store.password)
  30. if user
  31. session[:user_id] = user.id
  32. @user = user
  33. # this is just some extra code that you might want to have
  34. if user.last_login
  35. flash[:notice] = "Welcome back, #{ user.name }. You last logged in #{ user.last_login.strftime('%whatever')}."
  36. else
  37. flash[:notice] = "Welcome to <the app>, #{ user.name }."
  38. end
  39. render :action => 'show_login_index'
  40. else
  41. flash[:notice] = 'Sorry, invalid username/password combination. Please try again.'
  42. render :action => 'show_login_index'
  43. end
  44. end
  45. elsif request.xhr?
  46. render :action => 'show_login_index'
  47. end
  48. # if it's a GET request, it should show a fancy login page
  49. end
  50.  
  51. def show_logout
  52. # ajax action
  53. render :update do |page|
  54. page.replace_html 'ajax_login', :partial => 'login/logout'
  55. page.hide 'ajax_login'
  56. page.visual_effect :blind_down, 'ajax_login', :duration => 1.5
  57. end
  58. end
  59.  
  60. def logout
  61. if request.post?
  62. session[:user_id] = nil
  63. @user = nil
  64. flash[:notice] = 'You have been successfully logged out.'
  65. render :action => 'show_login_index'
  66. elsif request.xhr?
  67. render :partial => 'logout'
  68. end
  69. end
  70.  
  71. ## _index.rhtml [ruby]
  72. <!-- use a before_filter on the main controller to provide an @user object, the login controller provided it already -->
  73. <div class="flash" id="flash_inline">
  74. <%= flash[:notice] %>
  75. <% flash[:notice] = nil -%>
  76. </div>
  77. <div class="text" id="login_status">
  78. <% if @user -%>
  79. Logged in as <%= @user.name %>. &nbsp; | &nbsp; <%= link_to_remote 'Logout', :url => { :controller => 'login', :action => 'show_logout' } -%>
  80. <% else -%>
  81. Not logged in.
  82. <%= link_to_remote 'Login', :url => { :controller => 'login', :action => 'show_login' } -%>
  83. <% end %>
  84. </div>
  85.  
  86. ## _login.rhtml [ruby]
  87. <div class="flash" id="inline_flash">
  88. <%= flash[:notice] %>
  89. </div>
  90. <% form_remote_tag :url => { :controller => 'login', :action => 'login' } do %>
  91. <div class="container" id="login_button">
  92. <%= submit_tag 'Login' %>
  93. </div>
  94.  
  95. <div class="container" id="login_password">
  96. <label for="password">Password:</label>
  97. <%= password_field 'user_store', 'password', :size => 10, :tabindex => 2 %>
  98. </div>
  99.  
  100. <div class="container" id="login_user_name">
  101. <label for="name">Username:</label>
  102. <%= text_field 'user_store', 'name', :size => 10, :tabindex => 1 %>
  103. </div>
  104. <% end %>
  105.  
  106. ## _logout.rhtml [ruby]
  107. <div class="text" id="logout_inline">
  108. <% link_to_remote 'Cancel', :url => { :controller => 'login', :action => 'show_login_index' } %>
  109. </div>
  110. <div class="form" id="inline_logout">
  111. <% form_remote_tag :url => { :controller => 'login', :action => 'logout' } do %>
  112. <%= submit_tag 'Logout' %>
  113. <% end %>
  114. </div>
Add Comment
Please, Sign In to add comment