Skip to content

Commit

Permalink
Redirect to profile if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
darnfish committed Nov 10, 2023
1 parent bc6246e commit f54edcf
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions pages/api/redirect.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import knex from 'knex'
import { NextApiRequest, NextApiResponse } from 'next'

export default async function handler(
request: NextApiRequest,
response: NextApiResponse,
request: NextApiRequest,
response: NextApiResponse,
) {
response.status(200).json({
headers: request.headers
})
/**
* Check if it's not a GET
*/
if(request.method !== 'GET') {
response.status(405).json({ error: 'Method is not GET' })
return
}

const host = request.headers.host
const hostParts = host.split('.')

const domain = [hostParts.at(-2), hostParts.at(-1)].join('.')
const subdomain = hostParts.slice(0, -2).join('.')

const rows = await knex('registrations').where({ subdomain, domain }).whereNull('invalidated_at')
if(rows.length === 0)
return response.redirect('https://skyna.me')

response.redirect(`https://bsky.app/profile/${subdomain}.${domain}`)
}

0 comments on commit f54edcf

Please sign in to comment.