Hello, in this example I am using Node.js to send from my backend the information from the user and I am also printing in the console the token I am getting from the front end
Now the issue I have I that I am trying to replicate the same thing but using rails,
here is the configuration inside my App.js
import React from 'react';
import './App.css';
import { useAuth0 } from '@auth0/auth0-react';
import axios from 'axios';
function App() {
const {
loginWithPopup,
loginWithRedirect,
logout,
user,
isAuthenticated,
getAccessTokenSilently,
} = useAuth0();
const callApi = async () => {
try {
const response = await axios.get('http://localhost:5000');
console.log(response.data);
} catch (error) {
console.error(error);
}
};
const callProtectedApi = async () => {
try {
const token = await getAccessTokenSilently();
const response = await axios.get('http://localhost:5000/private', {
headers: {
authorization: `Bearer ${token}`,
},
});
console.log(response.data);
} catch (error) {
console.log(error.messsage);
}
};
return (
<div className="App">
<h1>Auth0 authentication</h1>
<ul>
<li>
<button onClick={loginWithPopup}>Login with Popup</button>
<button onClick={loginWithRedirect}>Login with Redicrect</button>
<button onClick={logout}>Logout</button>
</li>
</ul>
<h3>User is {isAuthenticated ? 'Logged in' : 'Not logged in'}</h3>
<ul>
<li>
<button onClick={callApi}>Call API</button>
</li>
<li>
<button onClick={callProtectedApi}>Call Proteced API route </button>
</li>
</ul>
<pre style={{ textAlign: 'start' }}>{JSON.stringify(user, null, 2)}</pre>
</div>
);
}
export default App;
now inside my rails app, I have added the examples from the tutorial to configure API and validate token, matter a fact I get the following results
now what i am trying to do is show replicate the same thing I did with Node.js I want to send back to the front end the credentials of the user that logged in.
I was trying to add the following code but
class DisplayController < ActionController::API
def index
require 'uri'
require 'net/http'
require 'openssl'
url = URI('https://dev-v88tfgqc.us.auth0.com/oauth/token')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(url)
request['content-type'] = 'application/x-www-form-urlencoded'
request.body = 'grant_type=client_credentials&client_id=%24%7Baccount.clientId%7D&client_secret=Rails.application.credentials.CLIENT_SECRET&audience=Rails.application.credentials.API_IDENTIFIER'
response = http.request(request)
puts response.read_body
end
end
but here is the result i get
I know my environmental variables work because i tested them inside the rails console