瀏覽代碼

companion: use redirect strategy if redirect value is set in auth url

Ifedapo Olarewaju 6 年之前
父節點
當前提交
3d89a0cb4b
共有 1 個文件被更改,包括 27 次插入13 次删除
  1. 27 13
      packages/@uppy/companion/src/server/controllers/callback.js

+ 27 - 13
packages/@uppy/companion/src/server/controllers/callback.js

@@ -34,20 +34,34 @@ module.exports = function callback (req, res, next) {
     const allowedClients = req.uppy.options.clients
     // if no preset clients then allow any client
     if (!allowedClients || hasMatch(origin, allowedClients) || hasMatch(parseUrl(origin).host, allowedClients)) {
-      return res.send(`
-        <!DOCTYPE html>
-        <html>
-        <head>
-            <meta charset="utf-8" />
-            <script>
-              window.opener.postMessage({token: "${uppyAuthToken}"}, "${sanitizeHtml(origin)}")
-              window.close()
-            </script>
-        </head>
-        <body></body>
-        </html>`
-      )
+      const redirect = oAuthState.getFromState(state, 'redirect', req.uppy.options.secret)
+      if (redirect) {
+        // if a redirect value is specified from the client, then redirect there instead
+        const query = (parseUrl(redirect).query ? `&` : `?`) + `uppyAuthToken=${uppyAuthToken}`
+        return res.redirect(`${redirect}${query}`)
+      }
+      return res.send(htmlContent(uppyAuthToken, origin))
     }
   }
   next()
 }
+
+/**
+ *
+ * @param {string} token uppy auth token
+ * @param {string} origin url string
+ */
+const htmlContent = (token, origin) => {
+  return `
+    <!DOCTYPE html>
+    <html>
+    <head>
+        <meta charset="utf-8" />
+        <script>
+          window.opener.postMessage({token: "${token}"}, "${sanitizeHtml(origin)}")
+          window.close()
+        </script>
+    </head>
+    <body></body>
+    </html>`
+}