Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
niktverd committed Aug 24, 2022
1 parent fb602e6 commit 0dad98c
Show file tree
Hide file tree
Showing 18 changed files with 8,471 additions and 169 deletions.
18 changes: 18 additions & 0 deletions configs/firebase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { initializeApp } from 'firebase/app';
import { getFirestore } from 'firebase/firestore/lite';
import { getStorage } from 'firebase/storage';

export const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
};

const firebaseApp = initializeApp(firebaseConfig);

export const storage = getStorage(firebaseApp);
export const firestore = getFirestore(firebaseApp);
export default firestore;
40 changes: 40 additions & 0 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import * as jose from 'jose';

const secret = process.env.NEXT_PUBLIC_SECRET;

export default async function middleware(req: NextRequest) {
const { cookies, nextUrl } = req;
const jwt = cookies.get('affilERP');

const loginUrl = nextUrl.clone();
loginUrl.pathname = '/auth/login';

if (nextUrl.pathname.startsWith('/protected')) {
if (jwt === undefined) {
return NextResponse.redirect(loginUrl);
}

try {
const { payload: jwtData } = await jose.jwtVerify(
jwt,
new TextEncoder().encode(secret)
);

if (jwtData) {
return NextResponse.next();
}

return NextResponse.redirect(loginUrl);
} catch (error) {
return NextResponse.redirect(loginUrl);
}
}

return NextResponse.next();
}

export const config = {
matcher: ['/protected/:path*'],
};
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @type {import('next').NextConfig} */

const nextConfig = {
reactStrictMode: true,
reactStrictMode: false,
swcMinify: true,
};

Expand Down
Loading

0 comments on commit 0dad98c

Please sign in to comment.