'current_user is nil through axios
My environment is following the below.
- Rails
- React.js
- axios
- devise
- devise-token-auth
- js-cookie
I achieved to login using devise_token_auth/sessions#create.
Then I got access-token, client and uid.
The next step is Obtaining user data from devise.
So I tried this code:
react
export function AdminPageBase(params: adminUserData & UserListProps) {
const classes = useStyles();
const uid = Cookies.get("_uid");
const access_token = Cookies.get("_access_token");
const client = Cookies.get("_client");
useEffect(() => {
axios
.post(`${process.env.REACT_APP_ENDPOINT}/admin/sessions`, {
headers: {
accsess_token: access_token ?? "",
uid: uid!,
client: client!,
},
})
.then((response) => {
Cookies.set("_access_token", response.headers["access-token"]);
Cookies.set("_client", response.headers["client"]);
Cookies.set("_uid", response.headers["uid"]);
})
.catch(() => {});
}, []);
...
But when I type current_admin_admin_user and current_user just in case, it was nil.
However, when I type params, I got #<ActionController::Parameters {"headers"=>{"accsess_token"=>"2hUxLpyMD9Dux8_6WP3BbA", "uid"=>"[email protected]", "client"=>"CCqv2ZsyOQlaPfsAmCVmLA"}, "controller"=>"admin/sessions", "action"=>"index", "session"=>{"headers"=>{"accsess_token"=>"2hUxLpyMD9Dux8_6WP3BbA", "uid"=>"[email protected]", "client"=>"CCqv2ZsyOQlaPfsAmCVmLA"}}} permitted: false>.
I don't know why current_admin_admin_user doesn't work...
Please help me...
sessions_controller.rb
class Admin::SessionsController < ApplicationController
def index
binding.pry
end
helper_method :current_user, :user_signed_in?
end
routes.rb
namespace :admin do
mount_devise_token_auth_for 'AdminUser',
at: 'auth',
controllers: {
registrations: 'admin/auth/registrations',
}
post '/sessions', to: 'sessions#index'
end
admin_user
devise :database_authenticatable,
:registerable,
:recoverable,
:rememberable,
:trackable,
:validatable
include DeviseTokenAuth::Concerns::User
devise_token_auth
config.headers_names = {
'access-token': 'access-token',
'client': 'client',
'expiry': 'expiry',
'uid': 'uid',
'token-type': 'token-type',
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
