Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
feat: archiving project redirect to user's github profile (#10386)
Browse files Browse the repository at this point in the history
* feat: archiving project redirect to github profile

* fix: tests are no longer required

* fix: display home page

* fix: remove header + footer
  • Loading branch information
eddiejaoude authored Jun 10, 2024
1 parent d88563f commit ef5af9c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 125 deletions.
50 changes: 25 additions & 25 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,28 @@ jobs:
- name: run ${{ matrix.step }}
run: npm run ${{ matrix.step }}

tests:
needs: build
runs-on: ubuntu-latest
services:
mongo:
image: mongo
ports:
- 27017:27017
strategy:
fail-fast: false
matrix:
shardIndex: [1, 2, 3, 4, 5]
shardTotal: [5]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "18"
cache: "npm"
- name: install dependencies
run: npm ci
- name: Install Playwright's dependencies
run: npx playwright install chromium
- name: run tests
run: npx playwright test --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}
# tests:
# needs: build
# runs-on: ubuntu-latest
# services:
# mongo:
# image: mongo
# ports:
# - 27017:27017
# strategy:
# fail-fast: false
# matrix:
# shardIndex: [1, 2, 3, 4, 5]
# shardTotal: [5]
# steps:
# - uses: actions/checkout@v4
# - uses: actions/setup-node@v4
# with:
# node-version: "18"
# cache: "npm"
# - name: install dependencies
# run: npm ci
# - name: Install Playwright's dependencies
# run: npx playwright install chromium
# - name: run tests
# run: npx playwright test --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}
4 changes: 0 additions & 4 deletions components/layouts/MultiLayout.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Navbar from "@components/navbar/Navbar";
import Footer from "@components/Footer";
import SkipLink from "@components/SkipLink";
import Alert from "./Alert";

Expand All @@ -11,13 +9,11 @@ export default function MultiLayout({ settings, children }) {
{(!settings || !settings.hideNavbar) && (
<>
<Alert />
<Navbar />
</>
)}
<main id="main" className="flex-1 dark:bg-dark-2 dark:z-40">
{children}
</main>
{(!settings || !settings.hideFooter) && <Footer />}
</div>
</>
);
Expand Down
100 changes: 4 additions & 96 deletions middleware.js
Original file line number Diff line number Diff line change
@@ -1,108 +1,16 @@
import { getToken } from "next-auth/jwt";
import { NextResponse } from "next/server";

// note: logger is not available in middleware, using console.log instead

export const config = {
matcher: [
"/",

// account management
"/account/:path*",
"/api/account/:path*",

// admin section
"/admin/:path*",
"/api/admin/:path*",
],
matcher: ["/:path*"],
};

export async function middleware(req) {
const protocol = process.env.NODE_ENV === "development" ? "http" : "https";
const hostname = req.headers.get("host");
const reqPathName = req.nextUrl.pathname;
const sessionRequired = ["/account", "/api/account"];
const adminRequired = ["/admin", "/api/admin"];
const adminUsers = process.env.ADMIN_USERS.split(",");
const hostedDomain = process.env.NEXT_PUBLIC_BASE_URL.replace(
/http:\/\/|https:\/\//,
"",
);
const hostedDomains = [hostedDomain, `www.${hostedDomain}`];

// if custom domain + on root path
if (!hostedDomains.includes(hostname) && reqPathName === "/") {
console.log(`custom domain used: "${hostname}"`);

let res;
let profile;
let url = `${
process.env.NEXT_PUBLIC_BASE_URL
}/api/search/${encodeURIComponent(hostname)}`;
try {
res = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
profile = await res.json();
} catch (e) {
console.error(url, e);
return NextResponse.error(e);
}

if (
profile?.username &&
profile.settings?.domain &&
profile.settings.domain === hostname
) {
console.log(
`custom domain matched "${hostname}" for username "${profile.username}" (protocol: "${protocol}")`,
);
// if match found rewrite to custom domain and display profile page
return NextResponse.rewrite(
new URL(
`/${profile.username}`,
`${protocol}://${profile.settings.domain}`,
),
);
}

console.error(`custom domain NOT matched "${hostname}"`);
}

// if not in sessionRequired or adminRequired, skip
if (
!sessionRequired
.concat(adminRequired)
.some((path) => reqPathName.startsWith(path))
) {
return NextResponse.next();
}

const session = await getToken({
req: req,
secret: process.env.NEXTAUTH_SECRET,
});

// if no session reject request
if (!session) {
if (reqPathName.startsWith("/api")) {
return NextResponse.json({}, { status: 401 });
}
return NextResponse.redirect(new URL("/auth/signin", req.url));
}
const path = req.nextUrl.pathname;

const username = session.username;
// if admin request check user is allowed
if (adminRequired.some((path) => reqPathName.startsWith(path))) {
if (!adminUsers.includes(username)) {
if (reqPathName.startsWith("/api")) {
return NextResponse.json({}, { status: 401 });
}
return NextResponse.redirect(new URL("/404", req.url));
}
if (path !== "/") {
return NextResponse.redirect(new URL(path, "https://github.com"));
}

return NextResponse.next();
Expand Down

0 comments on commit ef5af9c

Please sign in to comment.