将用户从我的网站重定向到移动应用程序

发布于 2025-01-11 02:52:57 字数 1232 浏览 0 评论 0原文

我在 vercel 上运行了这个中间件函数,我试图用它来重定向用户。该链接是在 Expo 上生成的,它包含指向我的应用程序的深层链接。该链接会通过电子邮件发送给用户以用于身份验证。

该链接看起来像这样

https://www.mywebsite.com/redirect?redirectUrl=exp://192.168.1.0:9000&mode=signIn&lang=en&apiKey=xxxx&oobCode=xxxx

所以我设法获取我也想定向的世博会链接,但它永远不会进入我的应用程序,因为

https://www.mywebsite.com

总是被添加到链接中,所以我最终得到

https://www.mywebsite.com/exp://192.168.1.0:9000&mode=signIn&lang=en&apiKey=xxxx&oobCode=xxxx

如何修改我的函数以直接到世博会链接?


import { NextRequest, NextResponse } from "next/server";

export default async function redirects(req, res) {
  const { url } = req;
  const { href, origin, searchParams } = req.nextUrl;

  if (href.includes("redirectUrl")) {
    let extractedUrl = url.split("?")[1];
    let redirectUrl = extractedUrl.replace("redirectUrl=", "");

    return NextResponse.redirect(redirectUrl);
  }

  return NextResponse.next();
}

I have this middleware function running on vercel that I'm attempting to redirect users with. The link is generated on Expo and it contains a deeplink to my app. This link gets emailed to the user for auth purposes.

The link looks like this

https://www.mywebsite.com/redirect?redirectUrl=exp://192.168.1.0:9000&mode=signIn&lang=en&apiKey=xxxx&oobCode=xxxx

So I manage to get the expo link I want to direct too but it never goes to my app because

https://www.mywebsite.com

always gets added to the link so I end up with

https://www.mywebsite.com/exp://192.168.1.0:9000&mode=signIn&lang=en&apiKey=xxxx&oobCode=xxxx

How can I modify my function to direct to the Expo link?


import { NextRequest, NextResponse } from "next/server";

export default async function redirects(req, res) {
  const { url } = req;
  const { href, origin, searchParams } = req.nextUrl;

  if (href.includes("redirectUrl")) {
    let extractedUrl = url.split("?")[1];
    let redirectUrl = extractedUrl.replace("redirectUrl=", "");

    return NextResponse.redirect(redirectUrl);
  }

  return NextResponse.next();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杆小烟枪 2025-01-18 02:52:57

对此走了一条不同的路线。如果有人感兴趣的话,是否可以在没有中间件和常规 getInitialProps 的情况下进行重定向。

这就是我想出来的。请随意建议我如何改进这一点。

Redirects.getInitialProps = (ctx) => {
  if (!ctx.res) return;
  const { redirectUrl, mode, lang, apiKey, oobCode } = ctx.query;
  const Location = `${redirectUrl}${mode}${lang}${apiKey}${oobCode}`;

  if (redirectUrl) {
    ctx.res.writeHead(302, { Location });
    ctx.res.end(`Redirecting to ${Location}`);
  } else {
    ctx.res.writeHead(400);
    ctx.res.end("You must provide a `redirectUrl` in the query");
  }
};

Went a different route with this. Did the redirect without the middleware and regular getInitialProps if anyone was interested.

This is what I came up with. Feel free to suggest how I can improve this.

Redirects.getInitialProps = (ctx) => {
  if (!ctx.res) return;
  const { redirectUrl, mode, lang, apiKey, oobCode } = ctx.query;
  const Location = `${redirectUrl}${mode}${lang}${apiKey}${oobCode}`;

  if (redirectUrl) {
    ctx.res.writeHead(302, { Location });
    ctx.res.end(`Redirecting to ${Location}`);
  } else {
    ctx.res.writeHead(400);
    ctx.res.end("You must provide a `redirectUrl` in the query");
  }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文