Back

How to forward User Agent

  • 0
  • Auth
  • Web
  • Cloud
Maldy
20 Aug, 2024, 20:19

I using Remix and Appwrite. I currently have a working Login Page that creates the SSR session, but when I try to forward the user agent to get more specific information on the Appwrite console (currently displays Windows 95), it fails. Since the problem is most likely due to my stupidity, I wonder how to forward the agent correctly? Any help is appreciated :)

My Code:

TypeScript
//signin.tsx
export async function action({ request }: ActionFunctionArgs) {
  let formData = await request.formData();
  let email = formData.get("email")?.toString();
  let password = formData.get("password")?.toString();
  if (!email || !password) {
    return new Response("Bad Request", { status: 400 });
  }
  let userAgent = request.headers.get("User-Agent")?.toString() ||"";
  let user = await createSession(email, password, userAgent);
  if (user) {
    return redirect("/", {
      headers: {
        "Set-Cookie": await authCookie.serialize(user?.secret),
      },
    });
  }
}

//createSession.ts 
export async function createSession(
  email: string,
  password: string,
  userAgent: string
) {
  const account = new Account(AdminClient);
  try {
    console.log("Creating Session");
    const session = await account.createEmailPasswordSession(email, password);
    console.log(userAgent);
    AdminClient.setForwardedUserAgent(userAgent);
    console.log(AdminClient.config);
    return session;
  } catch (error) {
    console.error(error);
    throw new Error("SignIn failed");
  }
}
TL;DR
Developers were seeking help on forwarding the user agent correctly in an Appwrite console. The setForwardUserAgent() method didn't seem effective, but manually setting the 'user-agent' header before creating a session worked as a workaround. By adding "account.client.addHeader("user-agent", userAgent)" before creating a session, the desired user-agent was successfully forwarded to the console.
Kenny
20 Aug, 2024, 20:51

Do you have a specific error message? How are you initializing your AdminClient?

Maldy
21 Aug, 2024, 08:10

No, I don't get any error message. It just doesn't get sent. This my My AdminClient:

TypeScript
const AdminClient = new Client()
  .setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint
  .setProject("My Project ID") // Your project ID
  .setKey("My Key"); // Your secret API key
Maldy
21 Aug, 2024, 08:11

When logging the User Agent on my server, I receive the following string: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"

Kenny
21 Aug, 2024, 13:24

Try setting it prior to making your create session call. It looks like to me all this method does it add a new header (X-Forwarded-User-Agent) to the client.

Maldy
21 Aug, 2024, 14:29

I will try that. Thank you for your help!

Maldy
23 Aug, 2024, 14:58

I am afraid that it doesn't seem to work. I tried to set it prior to the session call, but the result in the appwrite console is the same: Windows 95.

Kenny
23 Aug, 2024, 15:46

Hmm, I'm not quite sure then, sorry :(

Maldy
23 Aug, 2024, 15:59

All good. Thank you for your help. I will keep trying to find a solution.

Maldy
4 Sep, 2024, 20:57

I actually found the solution/ issue. @Kenny To my understanding "setForwardUserAgent()" is pretty useless, unless I am using it very wrong. As far as I understand the logic behind all this, the function adds the "X-Forwarded-User-Agent" Header but that has no effect, because it seems to me that the appwrite console uses the "user-agent" header, which depends on the server, not the actual user. However overwriting this very specific header seems to do the trick.

The workaround:

TypeScript
// ... 
 const account = new Account(AdminClient);
  account.client.addHeader("user-agent", userAgent);
// create Session after this 
TypeScript
account: {
 headers: {
     'x-sdk-name': 'Node.js',
...
// This needs to be overwritten 
     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',


     'X-Appwrite-Response-Format': '1.5.0',
     'X-Appwrite-Project': 'Id',
     'X-Appwrite-Key': 'Key',

// Seems to have no effect 
     'X-Forwarded-User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36' 
   }
} ```

Not sure if this is an actual issue or intended behavior though :/
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more