How to change the `frontend_domain` port in `GRAPHQL_AUTH` in the verification mail? - reactjs

I'm working on a Docker-Django-Graphene-React stack where Django is the backend which receive GraphQL queries through Graphene from React which acts as the frontend. Everything runs in docker containers. Django on port 8000 and React on 3000.
I'm currently working on the authentication with the python's django-graqhql-auth package.
When a user register itself on the frontend form, the register mutation is correctly made to the backend and the account is created.
A mail is sent at the same time to the user's registered mail address containing a verification link with a token that must be used with the veryifyToken mutation in order to flag the account as verified.
Here is an example of the link:
http://localhost:8000/activate/eyJ1c2VybmFtZSI6IkpvaG5zb2ZuZiIsImFjdGlvbiI6ImFjdGl2YXRpb24ifQ:1mQr0R:Wh25LJ6A1PRVCQT730kXXIk4i2QJgz1a4aNDe7RoZM0
The problem is that the port on the link is 8000, which redirects to the backend (Django). I would like to redirect the user on port 3000 which is the frontend (React).
According to the documentation, I should be able to change the frontend_domain which I did. I also changed the path and the protocol values to see if it works:
Here is what I put in my backend's settings.py file:
GRAPHQL_AUTH = {
"EMAIL_TEMPLATE_VARIABLES": {
"protocol": "https",
"frontend_domain": "localhost:3000",
"path": "verify",
}
}
And I end up with this link:
https://localhost:8000/verify/eyJ1c2VybmFtZSI6IkpvaG5zZmdvZmdzbmRmIiwiYWN0aW9uIjoiYWN0aXZhdGlvbiJ9:1mQrIr:2o818drqPP8oVTE4E6fg2F6vMu2zITOjkF96z5K1whY
The protocol and path variables were correctly changed but not the frontend_domain. The problem is that I cannot redirect the user directly to the frontend.
Is there a way to fix this? Or do I have to create a route on the backend which will redirect the user to the frontend with the token so that I can use the verifyToken mutation?

I might be answering this late, but replace frontend_domain with domain and it will work.

you just have to update to latest version.
pip install --upgrade django-graphql-auth

I noticed that you used in protocol: "https" , but should probably be protocol: "http".
I'm in the same project using Django and Graphql Auth and for me worked to send the email to the frontend to Next.js by using protocol: "http".
GRAPHQL_AUTH = {
"EMAIL_TEMPLATE_VARIABLES": {
"protocol": "http",
"frontend_domain": "localhost:3000",
"path": "verify",
}
}

Related

How to configure a react capacitor app running on mobile by usb to make http request to sendbird as localhost instead of its IP address?

I have a React webapp that I have converted it to native app using Capacitor. For live reloading, I had to edit capacitor.config.json as follows:
const config: CapacitorConfig = {
//
webDir: 'build',
bundledWebRuntime: false,
server: {
url: 'http://192.XXX.XXX:3000',
cleartext: true
},
};
This works just fine until the app makes a request to sendbird for initialization. I get the error message: 'SendBirdCall needs https connection, except for 'localhost' or '127.0.0.1'. I tried to setup an express https server but failed. Later I created two channels using ngrok - one for the server and one for the app. The sendbird request was successful but the graphql websocket was failing as ngrok does not support websocket links, also tried a tcp link but it failed as well.
I have another app that I created using the Sendbird React Native Quickstart project and I did not need to do any configuration for that. What could be done in order to make sendbird request using localhost from mobile connected via usb while also being able to have a ws connection?
Try to change the url:
const config: CapacitorConfig = {
...
server: {
url: 'http://localhost:3000',
cleartext: true
},
};
And after connecting a physical device execute the following:
cd $ANDROID_HOME/platform-tools
./adb reverse tcp:3000 tcp:3000
Where $ANDROID_HOME is the Android SDK directory.

Necessary adjustments when running react app on amplify server instead of local machine

I developed a reactjs web app which I want to make accessible via federated login (i.e. logging in with Google credentials). Therefor I created an Amplify project and followed the instructions of the aws tutorial for Social sign-in (OAuth).
It all works fine when I run the app locally. In my amplify project I tried to include the frontend code by linking it to my github repo. I was able to deploy the app successfully before implementing federated login. Once I did that - after logging in to my google account - I get a redirection error. It says "Localhost denied the connection". How can I change the settings such that I get redirected to the correct url? Below you can see the aws-export.js. I tried changing redirectSignin/Signout (comments) I still get same error though. How can I make it work?
const awsmobile = {
"aws_project_region": "us-east-2",
"aws_cognito_identity_pool_id": "us-east-2:203de857-b72c-41e1-a349-e8689e1d7b88",
"aws_cognito_region": "us-east-2",
"aws_user_pools_id": "us-east-2_7Hqae0xMa",
"aws_user_pools_web_client_id": "fmb3jgl1ojmgs9655i2cona66",
"oauth": {
"domain": "cvhfederated23782f2e-23782f2e-dev.auth.us-east-2.amazoncognito.com",
// "domain": "cvhfederated23782f2e-23782f2e-final.auth.us-east-2.amazoncognito.com",
"scope": [
"phone",
"email",
"openid",
"profile",
"aws.cognito.signin.user.admin"
],
"redirectSignIn": "http://localhost:3000/",
"redirectSignOut": "http://localhost:3000/",
// "redirectSignIn": "https://final.dw4tcn2vagyv6.amplifyapp.com/",
// "redirectSignOut": "https://final.dw4tcn2vagyv6.amplifyapp.com/",
"responseType": "code"
},
"federationTarget": "COGNITO_USER_POOLS"
};
export default awsmobile;
I've run into this issue in the past and opted to update the configuration values at runtime.
import awsconfig from '../aws-exports';
awsconfig.oauth.redirectSignIn = `${window.location.origin}/`;
awsconfig.oauth.redirectSignOut = `${window.location.origin}/`;
Amplify.configure(awsconfig);
Refer to https://docs.amplify.aws/lib/auth/social/q/platform/js#amazon-cognito-user-pool-setup
Basically, this kind of configuration needs to be handled in code. Put in both the localhost and production signin/signout redirects separated by a comma (,).
Then, after importing your aws-exports, split the redirect values e.g., awsConfig.oauth.redirectSignIn.split(",")
Finally, use window.location.hostname to conditionally choose the correct redirect urls e.g.,
const isLocalhost = Boolean(
window.location.hostname === "localhost" ||
// [::1] is the IPv6 localhost address.
window.location.hostname === "[::1]" ||
// 127.0.0.1/8 is considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
);

Heroku: Django backend is not working, getting error GET http://localhost:8000/api/todos/ net::ERR_CONNECTION_REFUSED

I actually created a fullstack todo app with django as backend and react as frontend. The frontend is working perfectly fine, you can that here -> https://ym-todo-application.herokuapp.com. But somehow my application cannot connect to the django backend, also on inspecting on browser i saw this error -> GET http://localhost:8000/api/todos/ net::ERR_CONNECTION_REFUSED.
Containing lot of files and code so i pushed them on bitbucket to make it easier to debug. here's the link https://bitbucket.org/Yash-Marmat/todo-app-fullstack/src/master/.
Thanks in advance.
You need to change the REST API server address. The localhost:8000 is the server address used in the development.
What is more, I see in your code that each time you write the request you have hard-coded server URL. You don't need to do this. You can set the server address by setting baseURL:
import axios from "axios";
if (window.location.origin === "http://localhost:3000") {
axios.defaults.baseURL = "http://127.0.0.1:8000"; // development address
} else {
axios.defaults.baseURL = window.location.origin; // production address
}
Then in the request, you only write the endpoint address, example:
axios.put(`/api/todos/${item.id}/`, item)
Please see my article Docker-Compose for Django and React with Nginx reverse-proxy and Let's Encrypt certificate for more details about Django and React deployment.

Different IP addresses in production and test environment how to handle it automaticaly

I have an app with django backend and react frontend. When testing and coding i run my django app on local server 127.0.0.1:8000 and connecting all my react requests to those endpoints. However in production my django app run on a different ip address and everytime i changed my code and push my reactjs app to production server i have to change all the endpoints ip's.
What is the best way to handle it? (as a source control i use git i do not know if it makes any differences)
I set the axios.defaults.baseURL depending on the window.location.origin. Here is my setting:
if (window.location.origin === "http://localhost:3000") {
axios.defaults.baseURL = "http://127.0.0.1:8000";
} else {
axios.defaults.baseURL = window.location.origin;
}
The above config is from my article Docker-Compose for Django and React with Nginx reverse-proxy and Let's Encrypt certificate. I'm serving React with nginx, and have reverse-proxy to Django (also in nginx), that's why for production setting I'm just using the same address. In the case of development, I have REST API at 127.0.0.1:8000.
I prefer this dynamic setting than settin env files because I don't need to set any environment variables.

Can't solve strict-origin-when-cross-origin. React + AdonisJS

I have a server, in which I'm running two different applications. The frontend (express + React) is running on 443 port, and the AdonisJS api is running on 3333 port. They share the same domain (something.com, for example), but I need to add the port when calling the api. The problem is, when I try to hit an endpoint from my api from React, I get this error: strict-origin-when-cross-origin. Actually, I'm not sure if this is an error, but I can't make any request at all. From another client, such as Insomnia, the request works like magic.
I fixed the issue by changing the AdonisJS cors config file. I switched the origin value from true to *.
Besides adding the proxy instruction in package.json
"proxy": "http://localhost:5000", I also had to remove the host from the api url request, so:
const apiData = await axios.get('http://127.0.0.1:5000/api/get-user-data');
became
const apiData = await axios.get('/api/get-user-data');
The link provided by #ShawnYap was really helpful https://create-react-app.dev/docs/proxying-api-requests-in-development/

Resources