Rails 4 5.0 Session Cookie AuthenticityToken
Tag: Ruby
Tag: Rails
Category: jekyll
Category: update
Category: git
Category: database
Category: PostgreSQL
Category: websocket
Category: ruby
Category: projects
Category: Redis
Category: mysql
Category: Mac
Category: Node
Category: NPM
Category: PM2
Category: nodejs
Category: Meteor
Category: Nginx
Category: gitLab
Category: Rails
- Why Ruby on Rails is better than Python Django?
- How to use Hotwire turbo in Rails 6 with Webpacker?
- Rails 6 Credentials (master.key and credentials.yml.enc)
- Rails Console
- JIRA-Atlassian-Connect-App-Django
- Rails 4 5.0 Session Cookie AuthenticityToken
- Rails Active Storage
- Rails 5 Source code Research
- 微信支付
- Rails零星笔记
Category: Homebrew
Category: CentOS
Category: FreeSwitch
Category: Ruby
- Ruby on Rails 8
- RESTful API
- Ruby on Rails 7
- Study from Ruby official website
- Ruby-Metaprogramming
- Ruby连数据库的问题
- rbenv使用
Category: Vim
Category: javascript
Category: React-Native
Category: Wechat
Category: homeland
Category: JavaScript
Category: Docker
Category: RubyMine
Category: Authorization
Category: RESTful-API
Category: Proxy
Category: Deploy
Category: Devise
Category: Bootstrap
Category: Active_Storage
Category: github
Category: Android
Category: cloud
Category: ssh
Category: python
Category: reactjs
Category: markdown
Category: ShadowSocks
Category: Code
Category: rails
Category: code
Category: Django
Category: Python
Category: DRF
Category: Fish
Category: Yarn
Category: Material-UI
Category: CSS
Category: aws
Category: uwsgi
Category: nginx
Category: docker
Category: React
Category: Enzyme
Category: Jira
Category: Interview
Category: JetBrain
Category: PyCharm
Category: ESLint
Category: Rails6
Category: NVM
Category: ssl
Category: tencent
Category: CI
Category: jenkins
Category: GitHub
Category: Credentials
Category: master.key
Category: Webpacker
Category: Turbo
Category: Hotwire
Category: Bootstrap5
Category: Flutter
Category: Clash
Category: Tor
Category: proxy
Category: Build
Category: SwitchyOmega
Category: Chrome-extension
Category: SQLAlchemy
Category: Algorithm
Category: Rails7
Category: Data
Category: Structure
Category: CPP
Category: Languages
Category: Golang
Category: Typescript
Category: Rails 8
session_store
config/initializers/session_store.rb
Rails.application.config.session_store :cookie_store, key: '_your_session', secure: false, domain: :all
secure is set to true when https enabled.
LoginController
class LoginController < ActionController::Base
# https://stackoverflow.com/questions/38331496/rails-5-actioncontrollerinvalidauthenticitytoken-error
protect_from_forgery prepend: true
def show
@student = Student.new
end
def login
@student = Student.find_by_username(params.require(:student)[:username])
if @student.nil?
flash[:error] = '用户不存在!'
redirect_to login_path
else
if @student.password == params.require(:student)[:password]
login_success_process
else
flash[:error] = '密码错误!'
flash[:username] = params.require(:student)[:username]
redirect_to login_path
end
end
end
def logout
clear_session
flash[:notice] = 'Logout successfully!'
redirect_to login_path
end
private
def login_success_process
set_login_session
flash[:notice] = 'Login successfully!'
redirect_to root_path
end
def set_login_session
session[:id] = @student.id
session[:username] = @student.username
session[:name] = @student.name
end
def clear_session
[:id, :username, :name].each do |key|
session[key] = nil
end
end
end
login.html.haml
!!! 5
%html{dir: locale_dir}
%head
%title= '请登录'
= csrf_meta_tags
%body
- if session[:username]
= session[:username]
= '您已经登录!'
- else
%h1= t('signin_form.title')
.flex-container
#signin
= form_for @student, url: do_login_path do |f|
= error_msg @student
.form-group
= content_tag(:label, '用户名')
.col-sm-9
= f.text_field :username, required: true
.form-group
= content_tag(:label, '密码')
.col-sm-9
= f.password_field :password, required: true
%br
= content_tag(:button, raw("登录"), { type: 'submit' })