如何在rails api中使用devise-jwt和devise进行登录、注册和注销

2024-02-18

我正在使用 Rails 作为后端设计-jwt https://github.com/waiting-for-dev/devise-jwt并对前端部分做出反应。

我正在关注这个https://github.com/waiting-for-dev/devise-jwt/blob/master/README.md https://github.com/waiting-for-dev/devise-jwt/blob/master/README.md

我的routes.rb 文件包含:

 Rails.application.routes.draw do
  # remove this in production
  require 'sidekiq/web'
  mount Sidekiq::Web => '/sidekiq'

  namespace :api, defaults: { format: 'json' } do
    namespace :v1 do
      devise_for :users, :controllers => {sessions: 'api/v1/sessions', registrations: 'api/v1/registrations'}
    end
  end
end

我的registrations_controller.rb(应用程序/控制器/api/registrations_controller.rb)

class Api::V1::RegistrationsController < Devise::RegistrationsController
  respond_to :json, :controllers => {sessions: 'sessions', registrations: 'registrations'}

  before_action :sign_up_params, if: :devise_controller?, on: [:create]

  def create
    build_resource(sign_up_params)

    if resource.save
      render :json => resource, serializer: Api::V1::UserSerializer, meta: { message: 'Sign up success', token: request.headers["Authorization"] }, :status => :created
    else
      render :json => resource, adapter: :json_api, serializer: ActiveModel::Serializer::ErrorSerializer, meta: { message: 'Sign up success' }, :status => :created
    end
  end


  protected

  def sign_up_params
    params.require(:sign_up).permit(:first_name, :last_name, :mobile, :email, :password, :password_confirmation)
  end
end

我的sessions_controller.rb(应用程序/控制器/api/sessions_controller.rb)

class Api::SessionsController < Devise::SessionsController  
  respond_to :json
end

我的 application_controller.rb (应用程序/控制器/application_controller.rb)

class ApplicationController < ActionController::Base
end

基本上访问令牌的下一步是什么。我很困惑。我将如何获取访问令牌并使用它在前端反应部分进行身份验证。


假设您已进行服务器端设置,响应将包括Authorization Header.

在前端,您将发出登录请求并进行回调以捕获响应:

 window.fetch(LOGIN_URL, dataWithLoginInfo).then(response => {
    const jwt = response.headers.get('Authorization').split('Bearer ')[1];
    window.sessionStorage.setItem('jwt', jwt);
  }).catch(handleError)

接下来使用以下命令发出请求Authorization标头包括:

const token =  window.sessionStorage.getItem('jwt')
const headers = { Authorization: `Bearer ${token}` }

或者在解码后在您的应用程序中使用它:

import jwt from 'jsonwebtoken';
const decodedToken = jwt.decode(window.sessionStorage.getItem('jwt'));

if (decodedToken.isAdmin) {
  return <AdminPage />;
} else {
  return <NotAdminPage />;
}

你会使用类似的东西https://www.npmjs.com/package/jwt-decode https://www.npmjs.com/package/jwt-decode or https://www.npmjs.com/package/jsonwebtoken https://www.npmjs.com/package/jsonwebtoken解码令牌并从中读取信息,如 id、角色、权限等。

您确实需要遵循以下教程:https://auth0.com/blog/secure-your-react-and-redux-app-with-jwt-authentication/ https://auth0.com/blog/secure-your-react-and-redux-app-with-jwt-authentication/ or http://jasonwatmore.com/post/2017/12/07/react-redux-jwt-authentication-tutorial-example http://jasonwatmore.com/post/2017/12/07/react-redux-jwt-authentication-tutorial-example。然后请当地专家查看您的所有代码。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在rails api中使用devise-jwt和devise进行登录、注册和注销 的相关文章

随机推荐