Laravel environment variable in vue component - javascript

I'm having a problem getting the correct value for an environment variable I have set. I am working on a localhost with valet. This specific domain is configure for ssl (valet secure), and the URL I am on also shows the certificate and correct URL.
I have the following in my .env file:
APP_URL=https://pad.eppo
MIX_APP_URL=${APP_URL}
I then call on this in my Vue component:
console.log("Environment URL: " + process.env.MIX_APP_URL);
Expected result:
https://pad.eppo
Result:
http://pad.eppo
There is no cache or cookies involved. Both have been cleared to double check. I have recompiled using npm run watch, since this is necessary when changing the variables. I am out of ideas as to why this problem exists. It is a big problem since my axios requests give errors on this.
Any help is much appreciated.

Force HTTPS in your Laravel application:
URL::forceScheme('https');
Add this line at the begin of your web.php file. In that way ${APP_URL} will be returned with HTTPS protocol.
Also ensure your config is not cached - run:
php artisan config:clear

Related

MERN: 'Invalid API Key' Heroku Deployment?

How do I get Firebase Auth to work on Heroku Deployment?
Auth works in MERN app's local development without any issues. All private information is stored in an .env file and then called in my react app with process.env.VARIABLE_NAME.
However, when I deploy to Heroku production and add the env files as shown here, I get the error: "Your API key is invalid, please check you have copied it correctly"
This is my deployed Heroku app: https://evening-fortress-01391.herokuapp.com/login
REACT_APP_FIREBASE_API_KEY=XXXXXXXX
REACT_APP_FIREBASE_AUTH_DOMAIN=XXXXXXXX
REACT_APP_FIREBASE_PROJECT_ID=XXXXXXXX
REACT_APP_FIREBASE_STORAGE_BUCKET=XXXXXXXX
REACT_APP_FIREBASE_MESSAGING_SENDER_ID=XXXXXXXX
REACT_APP_FIREBASE_APP_ID=XXXXXXXX
Also, running the command heroku config displays the inputted env variables in the terminal.
Any idea why Auth works locally but not in deployment? Thank you!
Answer because too long for a comment.
Sounds super hard to debug - also your app just shows a login and not the API key error =) To me it sounds like either just like it says, an incorrect API key or a formatting issue.
I would make a test config variable using the same characters as your API key like REACT_APP_FIREBASE_POTATO=p07470-test-example-foooobar and then in your frontend app just console.log("TEST" + process.env.REACT_APP_FIREBASE_POTATO);. Then publish that and check it in production.
This tests are the environment variables working at all and are there some character encoding issues (do you see the exact characters in the console as you should)

Is there spa mode for Esbuild's serve?

I'm using esbuild to develop a SPA and there's a serve function that spins up a dev server, however, if I go to any routes eg "/about", this results in a 404 as because this is a SPA, there is no actual file at that route.
If I could resolve all requests to "/" then that'll fix this issue.
Any ideas?
I just added --single flag to my package, which is a simple wrapper of esbuild --serve. You can check my source code to see how it works.
If the proxy request returns 404, then try again with req.path set to /, that's it.

For the ajax url shows 404 on the AWS tomcat log

I was using MVN install deploy my code on the AWS tomcat. I did this by directly open the AWSname:8080/manager/html and upload the WAR file(I am not sure if it is the correct way). The server can load the index.html successfully as my expectation and shows page on the front-end, but error went to call AJAX, which is in Javascript file, it always return 404. The servlet's url pattern will be like this
AJAX
Login Servlet
I tested everything fine on my localhost:8080, but in the AWS tomcat, the log showed like this.
Tomcat Log
I've never met such a case before, could somebody give me some useful suggestions on the possible solutions?
from your description it is not clear if you have multiple apps/war/containers/servers.
From your message you have a UI, which seems not to be bundled into your war file. To check in this case use these steps to check your setups
Add a index.html to your root in the war file and test if you can access it locally.
if this works, deploy on AWS and try to access the same path on AWS.
if the page is showing, your context is defined well on AWS. So you have an issue with your URL mappings
But if it does not show, you have a URL problem (wrong URL on AWS)

4 FOSJSRouting callback=fos.Router.setData & Route not found in production only

I am developing a page within Symfony 4 that requires the FOSJSrouting bundle. Within my DEV environment - using docker - I got it working fine using the steps below.
However, in my prod environment I keep getting the errors:
- http://url/js/routing?callback=fos.Router.setData 500 (Internal Server Error)
- router.min.js:1 Uncaught Error: The route "get_coinTicker_from_platform" does not exist.
My steps to get it working in DEV:
$ composer require friendsofsymfony/jsrouting-bundle
Adding the following to routes.yaml:
fos_js_routing:
resource: "#FOSJsRoutingBundle/Resources/config/routing/routing.xml"
Adding the following to my base.html.twig
<script src="{{ asset('bundles/fosjsrouting/js/router.min.js') }}"></script>
<script src="{{ path('fos_js_routing_js', { callback: 'fos.Router.setData' }) }}"></script>
This was sufficient to get my exposed routes to work:
/**
* #Route("/ticker/{coin}/{plat}", name="get_coinTicker_from_platform", options={"expose"=true})
*/
Then in my JavaScript I did:
$.ajax({
method: 'POST',
url: Routing.generate('get_coinTicker_from_platform', {coin: coin.val(), plat: exch.val()})
}).done(function(data) {
$('.loader').hide();
}
});
I installed the routing bundle using composer on my Linux server and even tried the steps included in the docs to publish assets as well as dump routes like so:
bin/console fos:js-routing:dump --format=json --target=public/js/fos_js_routes.json
I have checked the symfony and Apache logs. Nothing is hinting about this issue there. Everything else is running fine, just FOSrouting is causing trouble.
Also, I tried:
npm install fos-routing --save
This actually temporarily solved the issue, but the next day, after I did another rsync from my local repository it was broken again.
I had the same problem and it has been solved by giving the right rwxrwxrwx on the folder var/cache/prod.
I am a fair bit late to the party but anyway let's get into it.
I think your issue - as hinted by #Samiul Amin Shanto comes from cached content that is not wiped out before dumping the routes.
In prod environment, symfony caches the Router. So if you do not wipe the cache
and update your code with new actions in your controller for example, they won't
be available at all because the route is not referencing them yet.
Hopefully you somehow managed to find this out because at some point you did run cache:clear --env=prod
So I'm just putting out this response here for any other internet user who might come across this question.
Take care.
Just in case someone has the same issue, I also spent about 2 hours on this, the actual issue is the right permission on the var folder inside your symfony project. Just refer to symfony official docs for giving the right permission here!

I am getting error in console "You need to enable JavaScript to run this app." reactjs

I am new to reactjs, I am working on a app. It was running fine, but when I've run npm run build command, I am getting error "You need to enable JavaScript to run this app.". I have made changes in server.js file even I've given "homepage": "./", but it did not solved my issue.
And I've checked by running laravel project, javascript is enabled in browser, also tried different browsers.
Someone please help me to overcome this error.
I received this message when no proxy to the server was specified inside client package.json file.
"proxy": "http://localhost:5000"
(where 5000 should be changed to whatever port number the server was setup to listen to. In my case it also required server restart once added)
I had same problem. My workable solution:
package.json:
"homepage": "."
index.js:
app.use(express.static(__dirname)); //here is important thing - no static directory, because all static :)
app.get("/*", function(req, res) {
res.sendFile(path.join(__dirname, "index.html"));
});
I received this error because an API call that I was making was being blocked because of an invalid API key.
Try to run in other browser and see if it is working.
If it works there then please try below things and refresh your website.
Right click and select Inspect.
Go to Application Tab.
Clear everything from local storage, session storage and cookies.
Refresh your website.
This resolved similar issue in my case and hope it will work for others.
Just make sure that this route must be appeared after at all other routes
app.get("/*", function (req, res) {
res.sendFile(path.resolve(__dirname, '../client/build', 'index.html'));
})
I had the same problem. In my case, the class name wasn't following the patterns (must starts with uppercase). As soon as I corrected it everything started to work well.
Go to your SignIn component or Register component, change the form tag to a div tag OR prevent the form default i.e (e.preventDefault). Also make sure that Javascript is enabled in your browser.
after adding proxy in react package.json restart the reacr server. This works for me.
I had some cookies set in the same url (localhost:8080), probably from a previous development, and it was somehow messing with my React project.
I only found out because (out of despair) I tried with another browser and it worked.
I deleted the cookies and it worked perfectly.
Also a react newbie, I hit this issue. Remembering to add ReactDOM.render(<MyClassName />, document.getElementById("root")); fixed it for me.
In your react project folder install serve if you haven't installed it before;
npm install -g serve
Then serve your project with a static server;
serve -s build
The build is a folder generated by 'npm run build'.
That's it! Visit
http://localhost:5000
by default.
If you are facing the error "You need to enable JavaScript to run this app." in reactjs then 9/10 times there is an issue with your API call. Please cross check API url string, parameters, action type, endpoints, action names, controller names and API call status. You'll be able to find the issue.
verify endpoint url again and again. You'll definitely find a solutions. Also check ur VPN access if you're able to hit the end point.
Go to console --> network tab and see ur API status
Had the same issue, when I was trying serve react static files in node js server. Was missing the following in index.js of server side code.
app.use(express.static(path.resolve(__dirname, '../client/build')));
I had been struggling with this issue the whole morning and after trying all the different hacks of setting up proxy and homepage, it still did not resolve.
And then I checked my js files, and because there was an error which was not being caught while npm start, it was not enabling javascript internally.
How I got to know that this did resolve the issue: Stopping the current run and correcting the issue that was there and then rerunning the server.
I had same problem right now. I forgot to bring ${REACT_APP_API}
before error
const res = await axios.get(`/api/products`)
console.log(res.data)
final solution
const res = await axios.get(`${process.env.REACT_APP_API}/api/products`)
console.log(res.data)
It is case sensitive. Needs to start with upper case:
const router = createBrowserRouter([
{
path: "/",
element: <**I**ndex />,
errorElement: < ErrorPage />,
},{
path: "/login",
element: < **L**ogin />,
},{
path: "/register",
element: < **R**egister />,
}
]);

Categories