1.修改配置文件config/initializers/devise.rb
config.token_authentication_key=:auth_token2.修改controlleraction
classUsers::SessionsController<Devise::SessionsControllerdefcreateresource=warden.authenticate!(:scope=>resource_name,:recall=>"#{controller_path}#new")set_flash_message(:notice,:signed_in)ifis_navigational_format?sign_in(resource_name,resource)respond_todo|format|format.htmldorespond_withresource,:location=>redirect_location(resource_name,resource)endformat.jsondorender:json=>{:response=>'ok',:auth_token=>current_user.authentication_token}.to_json,:status=>:okendendendend3,可以使用如下的命令测试
curl-
XPOST'http://localhost:3000/users/sign_in.json'-d
'user[email]=example@example.com&user[password]=password'
->{
"response":
"ok",
"auth_token":
"ABCDE0123456789"}
curl-
L'http://localhost:3000/profile?auth_token=ABCDE0123456789'
->gotpagethat
Iwantedthatneedsauthentication
devise 提供的auth token 方式不是很合适,需要修改devise.rb配置文件,与web认证冲突
还是RubyChina实现的比较好一点
Ruby-China的代码中如何实现tokenauthentication登录
1,登录的时候生成private_token代码
#用户密钥,用于客户端验证
field
:private_token#重新生成PrivateTokendefupdate_private_token
random_key=
"#{SecureRandom.hex(10)}:#{self.id}"self.update_attribute(
:private_token,random_key)
end2,在客户端登录的时候,返回private_token
defcreate
resource=warden.authenticate!(
:scope=>resource_name,
:recall=>
"#{controller_path}#new")
set_flash_message(
:notice,
:signed_in)
ifis_navigational_format?
sign_in(resource_name,resource)
respond_to
do|format|
format.html{redirect_toafter_sign_in_path_for(resource)}
format.json{render
:status=>
'201',
:json=>resource.as_json(
:only=>[
:login,
:email,
:private_token])}
endend3,可以使用如下命令测试
curl-
XPOST'http://ruby-china.org/account/sign_in.json'-d
"user[login]=xxxx&user[password]=xxxxxx"4,如何在下次接口定义中使用?
#filepath:lib/api.rb##Postanewtopic#requireauthentication#params:#title#body#node_id
post
do
authenticate!
@topic=current_user.topics.new(
:title=>params[
:title],
:body=>params[
:body])
@topic.node_id=params[
:node_id]
@topic.save!
#TODOerrorhandlingend5,authenticate!的源代码【在lib/api/helper.rb文件中】
#userhelpersdefcurrent_user
@current_user||=
User.where(
:private_token=>params[
:token]||
'').first
enddefauthenticate!
error!({
"error"=>
"401Unauthorized"},
401)
unlesscurrent_user
end
RubyChina没有使用Devise默认的认证码,定义了一套新的认证机制,结合
Grape做接口,与web层
分享到:
相关推荐
RubyonRails6课程作为API,通过devise_token_auth显示身
Rails(devise_token_auth) + AngularJS(ng-token-auth) 演示这是一个完全配置为与和一起使用的 rails 应用程序。入门导轨 $bundle install$rake db:create db:migrateAngularJS $rake bower:install启动服务器 $...
cd controlled_health_api # installation of dependencies bundle install # creation of database and tables rails db:create rails db:migrate # run the project rails s 生产配置 # delete the config/...
这是使用devise_token_auth gem进行身份验证的React on Rails的示例应用程序。 发展历程 $ git clone https://github.com/saitoxu/react-devise-token-auth-sample.git $ cd react-devise-token-auth-sample $ ...
Graphql Devise生成器将执行Devise和Devise Token Auth生成器以在您的项目中设置gem。 您可以像往常一样使用其初始化程序文件(每个gem一个)自定义它们。 $ bundle exec rails generate graphql_devise:install ...
博客使用基于令牌的身份验证实现直接在 heroku 实例上访问网址: : 用户名/密码: /P@ssword 或 /P@ssw0rd 屏幕 1 屏幕 2 屏幕 3技术栈AngularJS 导轨ng-token-auth devise_token_auth设置申请分为两部分客户端:...
这是一个示例存储库,演示了如何设置集成了以下内容的rails应用程序:我主要用它来测试我的gem 。
自述文件该自述文件通常会记录启动和运行应用程序所需的所有步骤。 您可能要讲的内容: Ruby版本系统依赖配置数据库创建数据库初始化如何运行测试套件服务(作业队列,缓存服务器,搜索引擎等) 部署说明...
gem 'devise_token_auth' 然后使用捆绑软件安装gem: bundle install 需要帮忙? 请使用获取帮助请求和操作方法问题。 请仅在Bug和增强方面打开GitHub问题,而不是常规帮助请求。 在创建新期刊之前,请搜索以前...
简单的设计令牌验证演示 这是使用设计 gem 构建 JSON API(Web 服务)的 Rails 令牌身份验证的简单演示应用程序 随意 fork 并贡献您的更改。
devise-i18n, 设计 gem的翻译 devise-i18n 设计"是一种基于warden的Rails 灵活认证方案"。 国际化( aka i18n ) 是一个"计算机软件适应不同语言。区域差异和目标市场技术要求的方法"。在控制器。模型和其他领域中支持...
这是使用 React 和 Redux 以及使用 Devise Token Auth 的 Rails 后端进行用户身份验证的最 raddest 客户端解决方案。 TL; 博士 给定一个使用 Devise Token Auth 的 Rails 后端,这个模块提供了几个异步 Redux Thunk...
苹果认证安装将此行添加到您的Gemfile中: gem 'apple_auth' 然后执行: $ bundle install或自己安装: $ gem install apple_auth安装gem之后,您需要运行此生成器。 $ rails g apple_auth:config这将生成一个新的...
Devise_token_auth 工厂机器人 RSPec 测验 rspec 作者 :bust_in_silhouette: 伊丽莎白·比利亚莱霍斯(Elizabeth Villalejos) :handshake: 贡献 欢迎提供文稿,问题和功能要求! 随时检查。 表示支持 给一个 :...
使用以下命令推送到heroku: git push angular-front-end `git subtree split --prefix angular-front-end`:master 用git push heroku `git subtree split --prefix devise-auth-token`:master -auth-token文件夹:...
黑皮书服务器Black Book的服务器端组件位于此处。... 要登录并接收JWT,请向端点/auth/sign_in email POST email和password TODO:弄清楚如何使用devise_token_auth进行身份验证用户数用户是有权访
眼镜Ruby版本 2.7.0 Rails 版本 5.2.4 React版本 16.12.0 devise_token_auth 版本 1.1.3 Axios 0.19.2 版本Postgres 版本 1.2.2 使用 Yarn 而不是 NPM配置后端$ rvm install 2.7.0 或 rbenv install 2.7.0 $ rvm ...
导入AngularTokenModule并将其添加到您的主模块中,然后使用配置调用'forRoot'函数。 确保您也导入了HttpClientModule 。 import { AngularTokenModule } from 'angular-token' ; @ NgModule ( { imports : [ .....
沙托木德Ruby3.0.0 rvm 1.29.11 捆绑器2.2.3 MySQL服务器5.7.21要做的事情: 生产default_confirm_url(devise-token-auth) 生产SMTP设置(动作邮件程序) Player.destroy_all ActiveRecord :: ...
Devise Token Auth进行身份验证 用于渲染的Jbuilder 如何使用它? 基本上,我们有一些具有特定权限的用户可以使用的端点。 我们在应用程序上有两个配置文件: admin和client 。 管理员是负责管理一切的人。他可以...