-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
37 lines (32 loc) · 909 Bytes
/
middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { createClient } from "@supabase/supabase-js";
import { NextResponse } from "next/server";
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
);
function getValidURL(url) {
if (url.includes("http://") || url.includes("https://")) {
return url;
}
return "https://" + url;
}
export default async function handler(req) {
if (req.method === "GET") {
let pathname = req.nextUrl.pathname;
let parts = pathname.split("/");
let id = parts[parts.length - 1];
try {
const { data, error } = await supabase
.from("urls")
.select("original_url")
.eq("id", id)
.single();
if (data && !error) {
const shortUrl = data.original_url;
return NextResponse.redirect(getValidURL(shortUrl));
}
} catch (error) {
console.log(error.message);
}
}
}