Skip to content

Commit

Permalink
chore: almost there...
Browse files Browse the repository at this point in the history
  • Loading branch information
gustaveWPM committed Apr 21, 2024
1 parent 52bac9d commit 2f4682f
Show file tree
Hide file tree
Showing 17 changed files with 225 additions and 51 deletions.
5 changes: 3 additions & 2 deletions .env_example
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
NEXT_SITEMAP_SITE_URL="http://localhost:3000"
NEXTAUTH_URL="http://localhost:3000"
AUTH_URL="http://localhost:3000"
AUTH_TRUST_HOST="http://localhost:3000"

NEXTAUTH_SECRET="YOUR RANDOM STRING - https://next-auth.js.org/configuration/options#nextauth_secret"
AUTH_SECRETS="YOUR RANDOM SECRETS, separated using ';;;;;;' (without quotes)" # https://next-auth.js.org/configuration/options#nextauth_secret

DISCORD_CLIENT_ID="YOUR DISCORD CLIENT ID - https://discord.com/developers/applications"
DISCORD_CLIENT_SECRET="YOUR DISCORD CLIENT SECRET - https://discord.com/developers/applications"

Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ FAKE_CODEGEN
.stryker-tmp
.next
.ts-prune

interop/config/httpCodes.ts
73 changes: 73 additions & 0 deletions .ts-prune/artifacts/tsprune-false-positives.conf
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,79 @@ interop/lib/builders/index.ts:13 - buildBlogPostLanguageFlag (used in module)
interop/lib/builders/index.ts:16 - buildBlogPostCategory (used in module)
interop/lib/builders/index.ts:14 - buildLandingPageCategory (used in module)

interop/config/httpCodes.ts:24 - PERMANENT_REDIRECT
interop/config/httpCodes.ts:29 - NOT_FOUND
interop/config/httpCodes.ts:26 - UNAUTHORIZED
interop/config/httpCodes.ts:32 - PROXY_AUTHENTICATION_REQUIRED
interop/config/httpCodes.ts:48 - FAILED_DEPENDENCY
interop/config/httpCodes.ts:49 - TOO_EARLY
interop/config/httpCodes.ts:28 - FORBIDDEN
interop/config/httpCodes.ts:44 - ENHANCE_YOUR_CALM
interop/config/httpCodes.ts:21 - NOT_MODIFIED
interop/config/httpCodes.ts:54 - NO_RESPONSE
interop/config/httpCodes.ts:11 - RESET_CONTENT
interop/config/httpCodes.ts:31 - NOT_ACCEPTABLE
interop/config/httpCodes.ts:4 - PROCESSING
interop/config/httpCodes.ts:14 - ALREADY_REPORTED
interop/config/httpCodes.ts:2 - CONTINUE
interop/config/httpCodes.ts:12 - PARTIAL_CONTENT
interop/config/httpCodes.ts:42 - EXPECTATION_FAILED
interop/config/httpCodes.ts:41 - REQUESTED_RANGE_NOT_SATISFIABLE
interop/config/httpCodes.ts:73 - ORIGIN_IS_UNREACHABLE
interop/config/httpCodes.ts:20 - SEE_OTHER
interop/config/httpCodes.ts:74 - SSL_HANDSHAKE_FAILED
interop/config/httpCodes.ts:38 - PAYLOAD_TOO_LARGE
interop/config/httpCodes.ts:58 - TOKEN_EXPIRED_OR_INVALID
interop/config/httpCodes.ts:10 - NO_CONTENT
interop/config/httpCodes.ts:72 - CONNECTION_TIMED_OUT
interop/config/httpCodes.ts:18 - MOVED_PERMANENTLY
interop/config/httpCodes.ts:27 - PAYMENT_REQUIRED
interop/config/httpCodes.ts:50 - UPGRADE_REQUIRED
interop/config/httpCodes.ts:52 - TOO_MANY_REQUESTS
interop/config/httpCodes.ts:37 - PRECONDITION_FAILED
interop/config/httpCodes.ts:17 - MULTIPLE_CHOICES
interop/config/httpCodes.ts:71 - WEB_SERVER_IS_DOWN
interop/config/httpCodes.ts:34 - CONFLICT
interop/config/httpCodes.ts:53 - REQUEST_HEADER_FIELDS_TOO_LARGE
interop/config/httpCodes.ts:68 - BANDWIDTH_LIMIT_EXCEEDED
interop/config/httpCodes.ts:69 - NOT_EXTENDED
interop/config/httpCodes.ts:64 - GATEWAY_TIMEOUT
interop/config/httpCodes.ts:55 - BLOCKED_BY_WINDOWS_PARENTAL_CONTROLS
interop/config/httpCodes.ts:19 - FOUND
interop/config/httpCodes.ts:36 - LENGTH_REQUIRED
interop/config/httpCodes.ts:51 - PRECONDITION_REQUIRED
interop/config/httpCodes.ts:45 - MISDIRECTED_REQUEST
interop/config/httpCodes.ts:61 - NOT_IMPLEMENTED
interop/config/httpCodes.ts:3 - SWITCHING_PROTOCOLS
interop/config/httpCodes.ts:46 - UNPROCESSABLE_ENTITY
interop/config/httpCodes.ts:65 - VARIANT_ALSO_NEGOTIATES
interop/config/httpCodes.ts:59 - CLIENT_CLOSED_REQUEST
interop/config/httpCodes.ts:39 - REQUEST_URI_TOO_LONG
interop/config/httpCodes.ts:8 - ACCEPTED
interop/config/httpCodes.ts:56 - UNAVAILABLE_FOR_LEGAL_REASONS
interop/config/httpCodes.ts:43 - IM_A_TEAPOT
interop/config/httpCodes.ts:33 - REQUEST_TIMEOUT
interop/config/httpCodes.ts:22 - USE_PROXY
interop/config/httpCodes.ts:35 - GONE
interop/config/httpCodes.ts:67 - LOOP_DETECTED
interop/config/httpCodes.ts:13 - MULTI_STATUS
interop/config/httpCodes.ts:62 - BAD_GATEWAY
interop/config/httpCodes.ts:7 - CREATED
interop/config/httpCodes.ts:16 - IM_USED
interop/config/httpCodes.ts:66 - INSUFFICIENT_STORAGE
interop/config/httpCodes.ts:5 - EARLY_HINTS
interop/config/httpCodes.ts:63 - SERVICE_UNAVAILABLE
interop/config/httpCodes.ts:9 - NON_AUTHORITATIVE_INFORMATION
interop/config/httpCodes.ts:40 - UNSUPPORTED_MEDIA_TYPE
interop/config/httpCodes.ts:23 - TEMPORARY_REDIRECT
interop/config/httpCodes.ts:60 - INTERNAL_SERVER_ERROR
interop/config/httpCodes.ts:47 - LOCKED
interop/config/httpCodes.ts:75 - SITE_FROZEN
interop/config/httpCodes.ts:70 - NETWORK_AUTHENTICATION_REQUIRED
interop/config/httpCodes.ts:30 - METHOD_NOT_ALLOWED
interop/config/httpCodes.ts:15 - TRANSFORMATION_APPLIED
interop/config/httpCodes.ts:57 - HTTP_REQUEST_SENT_TO_HTTPS_PORT

#-----------------
# **** I. 9) LIB
#-----------------
Expand Down
2 changes: 1 addition & 1 deletion interop/config/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

export const APP_PROTECTED_PATHS: readonly string[] = ['/dashboard'];
export const VIP_SHORTCUTS = {
'/sign-up': '/dashboard'
'/lp/sign-up': '/dashboard'
} as const;

// Stryker restore all
Expand Down
76 changes: 76 additions & 0 deletions interop/config/httpCodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// https://http.cat/
export const CONTINUE = 100;
export const SWITCHING_PROTOCOLS = 101;
export const PROCESSING = 102;
export const EARLY_HINTS = 103;
export const OK = 200;
export const CREATED = 201;
export const ACCEPTED = 202;
export const NON_AUTHORITATIVE_INFORMATION = 203;
export const NO_CONTENT = 204;
export const RESET_CONTENT = 205;
export const PARTIAL_CONTENT = 206;
export const MULTI_STATUS = 207;
export const ALREADY_REPORTED = 208;
export const TRANSFORMATION_APPLIED = 214;
export const IM_USED = 226;
export const MULTIPLE_CHOICES = 300;
export const MOVED_PERMANENTLY = 301;
export const FOUND = 302;
export const SEE_OTHER = 303;
export const NOT_MODIFIED = 304;
export const USE_PROXY = 305;
export const TEMPORARY_REDIRECT = 307;
export const PERMANENT_REDIRECT = 308;
export const BAD_REQUEST = 400;
export const UNAUTHORIZED = 401;
export const PAYMENT_REQUIRED = 402;
export const FORBIDDEN = 403;
export const NOT_FOUND = 404;
export const METHOD_NOT_ALLOWED = 405;
export const NOT_ACCEPTABLE = 406;
export const PROXY_AUTHENTICATION_REQUIRED = 407;
export const REQUEST_TIMEOUT = 408;
export const CONFLICT = 409;
export const GONE = 410;
export const LENGTH_REQUIRED = 411;
export const PRECONDITION_FAILED = 412;
export const PAYLOAD_TOO_LARGE = 413;
export const REQUEST_URI_TOO_LONG = 414;
export const UNSUPPORTED_MEDIA_TYPE = 415;
export const REQUESTED_RANGE_NOT_SATISFIABLE = 416;
export const EXPECTATION_FAILED = 417;
export const IM_A_TEAPOT = 418;
export const ENHANCE_YOUR_CALM = 420;
export const MISDIRECTED_REQUEST = 421;
export const UNPROCESSABLE_ENTITY = 422;
export const LOCKED = 423;
export const FAILED_DEPENDENCY = 424;
export const TOO_EARLY = 425;
export const UPGRADE_REQUIRED = 426;
export const PRECONDITION_REQUIRED = 428;
export const TOO_MANY_REQUESTS = 429;
export const REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
export const NO_RESPONSE = 444;
export const BLOCKED_BY_WINDOWS_PARENTAL_CONTROLS = 450;
export const UNAVAILABLE_FOR_LEGAL_REASONS = 451;
export const HTTP_REQUEST_SENT_TO_HTTPS_PORT = 497;
export const TOKEN_EXPIRED_OR_INVALID = 498;
export const CLIENT_CLOSED_REQUEST = 499;
export const INTERNAL_SERVER_ERROR = 500;
export const NOT_IMPLEMENTED = 501;
export const BAD_GATEWAY = 502;
export const SERVICE_UNAVAILABLE = 503;
export const GATEWAY_TIMEOUT = 504;
export const VARIANT_ALSO_NEGOTIATES = 506;
export const INSUFFICIENT_STORAGE = 507;
export const LOOP_DETECTED = 508;
export const BANDWIDTH_LIMIT_EXCEEDED = 509;
export const NOT_EXTENDED = 510;
export const NETWORK_AUTHENTICATION_REQUIRED = 511;
export const WEB_SERVER_IS_DOWN = 521;
export const CONNECTION_TIMED_OUT = 522;
export const ORIGIN_IS_UNREACHABLE = 523;
export const SSL_HANDSHAKE_FAILED = 525;
export const SITE_FROZEN = 530;
export const NETWORK_CONNECT_TIMEOUT_ERROR = 599;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"lucide-react": "^0.368.0",
"mobile-detect": "^1.4.5",
"next": "^14.2.2",
"next-auth": "beta",
"next-auth": "5.0.0-beta.16",
"next-contentlayer": "^0.3.4",
"next-international": "1.2.4",
"next-themes": "^0.2.1",
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions src/app/[locale]/lp/sign-up/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import LandingPageMDX from '@/components/layouts/lp/MdxComponent';
import { getPageByLanguageAndPathStrict } from '@/lib/pages/api';
import { getStaticParams, getScopedI18n } from '@/i18n/server';
import I18nTaxonomy from '##/config/taxonomies/i18n';
import ROUTES_ROOTS from '##/config/routes';
import { redirect } from 'next/navigation';
import { i18ns } from '##/config/i18n';
import { auth } from '@/auth';

export async function generateMetadata({ params }: I18nPageProps) {
const scopedT = await getScopedI18n(i18ns.vocab);
Expand All @@ -31,15 +28,10 @@ export function generateStaticParams() {
return getStaticParams();
}

export default async function Page({ params }: I18nPageProps) {
export default function Page({ params }: I18nPageProps) {
const language = params[I18nTaxonomy.LANGUAGE];
setStaticParamsLocale(language);

// {ToDo} Handle this guests only route middleware-side to avoid SSG break
// https://github.com/nextauthjs/next-auth/discussions/10628
const session = await auth();
if (session) redirect(ROUTES_ROOTS.DASHBOARD);

const document = getPageByLanguageAndPathStrict({ path: 'lp/sign-up', lang: language }) as Page;
return <LandingPageMDX code={document.body.code} />;
}
Expand Down
1 change: 1 addition & 0 deletions src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { handlers } from '@/auth';
export const { POST, GET } = handlers;
export const runtime = 'edge';

// Stryker restore all
/* v8 ignore stop */
24 changes: 3 additions & 21 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { Href } from '@rtm/shared-types/Next';
import type { Session } from 'next-auth';

import { keysFactory, getOrSet } from '@/cache/auth';
import Discord from 'next-auth/providers/discord';
import discordApi from '@/meta/discordapi';
import config from '@/config/auth';
import NextAuth from 'next-auth';

export async function getDiscordProfilePicture(sub: string, __discordApi: IDiscordApi): Promise<MaybeNull<Href>> {
Expand Down Expand Up @@ -53,23 +53,5 @@ export async function getSession(session: Session, __discordApi: IDiscordApi = d
} satisfies Session;
}

export const { handlers, auth } = NextAuth({
providers: [
Discord({
authorization: {
params: {
scope: 'identify+guilds'
}
},
clientSecret: process.env.DISCORD_CLIENT_SECRET ?? '',
clientId: process.env.DISCORD_CLIENT_ID ?? ''
})
],

callbacks: {
async session({ session }) {
const s = await getSession(session);
return s;
}
}
});
const { handlers, auth } = NextAuth(config);
export { handlers, auth };
37 changes: 37 additions & 0 deletions src/config/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { NextAuthConfig } from 'next-auth';

import Discord from 'next-auth/providers/discord';
import { getSession } from '@/auth';

// eslint-disable-next-line
const AUTH_SECRETS_SEP = ';;;;;;';

// const secret = process.env.AUTH_SECRETS!.split(AUTH_SECRETS_SEP);
// {ToDo} Find a way to fix this
// See also: https://github.com/nextauthjs/next-auth/issues/10633
const secret = 'huuuummmmm';

const config = {
providers: [
Discord({
authorization: {
params: {
scope: 'identify+guilds'
}
},
clientSecret: process.env.DISCORD_CLIENT_SECRET ?? '',
clientId: process.env.DISCORD_CLIENT_ID ?? ''
})
],

callbacks: {
async session({ session }) {
const s = await getSession(session);
return s;
}
},

secret
} as const satisfies NextAuthConfig;

export default config;
3 changes: 2 additions & 1 deletion src/meta/discordapi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* v8 ignore start */
// Stryker disable all

import { NETWORK_CONNECT_TIMEOUT_ERROR, BAD_REQUEST } from '##/config/httpCodes';
import { keysFactory, get } from '@/cache/auth';

type FreshProfile = { epicFail?: boolean; avatar: string; id: string };
Expand All @@ -22,7 +23,7 @@ class DiscordApi implements IDiscordApi {
});

// eslint-disable-next-line @typescript-eslint/no-magic-numbers
if (400 <= req.status && req.status <= 599) throw new Error('Discord API error');
if (BAD_REQUEST <= req.status && req.status <= NETWORK_CONNECT_TIMEOUT_ERROR) throw new Error('Discord API error');

const response = await req.json();
return response;
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/protectRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { NextResponse } from 'next/server';
function protectRoute(req: NextRequest) {
const maybeI18nFlag = getMaybeI18nFlagFromRequest(req);
const i18nPrefix = maybeI18nFlag || '';
const head = process.env.NEXTAUTH_URL as Href;
const head = process.env.AUTH_URL as Href;

const dest = i18nPrefix
? buildPathFromParts(head, i18nPrefix, ROUTES_ROOTS.LANDING_PAGES, 'sign-up')
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/takeShortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { NextResponse } from 'next/server';
function takeShortcut(req: NextRequest, shortcut: AppPath) {
const maybeI18nFlag = getMaybeI18nFlagFromRequest(req);
const i18nPrefix = maybeI18nFlag || '';
const head = process.env.NEXTAUTH_URL as Href;
const head = process.env.AUTH_URL as Href;

const dest = i18nPrefix ? buildPathFromParts(head, i18nPrefix, shortcut) : buildPathFromParts(head, shortcut);

Expand Down
17 changes: 10 additions & 7 deletions src/middlewares/withAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ import type { NextRequest } from 'next/server';

import getVipRouteShortcut from '@/lib/misc/getVipRouteShortcut';
import isProtectedRoute from '@/lib/misc/isProtectedRoute';
import { auth } from '@/auth';

import protectRoute from './protectRoute';
import takeShortcut from './takeShortcut';

// eslint-disable-next-line require-await
const withAuth: MiddlewareFactory = () => async (request: NextRequest) => {
const { auth } = request as any;
if (!auth && isProtectedRoute(request.nextUrl.pathname)) return protectRoute(request);
const authMiddleware = auth((req) => {
const { auth } = req;
if (!auth && isProtectedRoute(req.nextUrl.pathname)) return protectRoute(req);

if (auth) {
const maybeShortcut = getVipRouteShortcut(request.nextUrl.pathname);
if (maybeShortcut !== undefined) return takeShortcut(request, maybeShortcut);
const maybeShortcut = getVipRouteShortcut(req.nextUrl.pathname);
if (maybeShortcut !== undefined) return takeShortcut(req, maybeShortcut);
}
};
});

// eslint-disable-next-line require-await
const withAuth: MiddlewareFactory = () => async (request: NextRequest) => authMiddleware(request, {});

export default withAuth;
Loading

0 comments on commit 2f4682f

Please sign in to comment.