Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DPCP-78]: Fix like in prod #28

Merged
merged 5 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/auth/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {

export const GetSession = async ({ cookies = '' }) => {
try {
const response = await fetch(`${process.env.MAIN_URL}/api/v1/auth/session`, {
const response = await fetch(`${process.env.API_URL}/api/v1/auth/session`, {
method: 'GET',
headers: {
Accept: 'application/json',
Expand All @@ -29,6 +29,7 @@ export const GetSession = async ({ cookies = '' }) => {
},
});
const session = await response.json();
console.log({ cookies, session });
return session;
} catch (e) {
console.error(e);
Expand Down
2 changes: 1 addition & 1 deletion lib/model/interfaces/get-private-abilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const getPrivateAbilities = async ({
limit = PAGE_SIZE,
filters = [],
}: any) => {
const loggedUser = user || (await whoAmI());
const loggedUser = user || (await whoAmI({}));

const adaptQuery: any = {
where: {
Expand Down
2 changes: 1 addition & 1 deletion lib/model/interfaces/get-private-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const getPrivateServices = async ({
limit = PAGE_SIZE,
filters = [],
}: any) => {
const loggedUser = user || (await whoAmI());
const loggedUser = user || (await whoAmI({}));

const adaptQuery: any = {
where: {
Expand Down
13 changes: 7 additions & 6 deletions lib/model/interfaces/middleware/authorization.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @interfaces/middleware/authorization.ts
import { getSession, GetSession } from '@auth';
import { GetPrivateAbilities } from '@controller';
import { cookies } from 'next/headers';
import { cookies as nextCookies } from 'next/headers';
export const canI = async ({ name, user }: any) => {
try {
const ability = await GetPrivateAbilities({ name });
Expand All @@ -10,16 +10,17 @@ export const canI = async ({ name, user }: any) => {
// return the capacity
return yes;
} catch (e) {
throw new Error(`Code 003: Missing results: ${e}`);
throw new Error(`Code 008: User is not authorized ${e}`);
}
};
export const whoAmI = async () => {
export const whoAmI = async ({ cookies }: any) => {
try {
const cookieString = cookies().getAll().toString();
const session = (await getSession()) || (await GetSession({ cookies: cookieString }));
const cookieString = nextCookies().getAll().toString();
const session = (await getSession()) || (await GetSession({ cookies: cookieString || cookies }));
// to-do add authorization/validation checks
console.log({ cookieString, session });
return session?.user;
} catch (e) {
throw new Error(`Code 003: Missing results: ${e}`);
throw new Error(`Code 007: Can't identify user ${e}`);
}
};
2 changes: 1 addition & 1 deletion lib/model/interfaces/update-private-user-abilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const updatePrivateUserAbilities = async ({ upsert = false, user, abilities }: a
// to-do: move this will be a middleware
if (abilities?.length === 0) return new Error('Code 002: Missing data (abilities)');

const loggedUser = user || (await whoAmI());
const loggedUser = user || (await whoAmI({}));

const payload = upsert
? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const updatePrivateUserFavoriteListings = async ({ upsert = true, user, listings
try {
if (listings?.length === 0) return new Error('Code 002: Missing data (listings)');

const loggedUser = user || (await whoAmI());
const loggedUser = user || (await whoAmI({}));

console.log({ user, loggedUser });

const delta = upsert ? listings.filter((listing: any) => !loggedUser?.favorites?.includes(listing)) : [];

Expand Down
2 changes: 1 addition & 1 deletion lib/model/interfaces/update-private-user-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const updatePrivateUserServices = async ({ upsert = false, user, services }: any
// to-do: move this will be a middleware
if (services?.length === 0) return new Error('Code 002: Missing data (services)');

const loggedUser = user || (await whoAmI());
const loggedUser = user || (await whoAmI({}));

const payload = upsert
? {
Expand Down
3 changes: 2 additions & 1 deletion src/app/api/v1/user/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export async function PATCH(request: CombineRequest) {
const body = await request?.json();
const listings = body?.listings;

const user = body?.user || session?.user;
const user = session?.user;
console.log({ session, body, cookies });

const data = await UpdatePrivateUserFavoriteListings({
user,
Expand Down
13 changes: 12 additions & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ export const config = {
matcher: ['/api/:path*'],
};

const allowedOrigins = {
[`${process.env.MAIN_URL}`]: process.env.MAIN_URL,
[`${process.env.NEXUS_HOST}`]: process.env.NEXUS_HOST,
[`${process.env.API_HOST}`]: process.env.API_HOST,
};

const headers: Record<string, any> = {
'Access-Control-Allow-Origin': process.env.MAIN_URL || 'https://www.dreampip.com',
'Access-Control-Allow-Origin': `${process.env.MAIN_URL}` || 'https://www.dreampip.com',
'Cache-Control': 'maxage=0, s-maxage=300, stale-while-revalidate=300',
// DEV-DEBUG:
// 'content-type': 'application/json',
Expand All @@ -27,6 +33,11 @@ const headers: Record<string, any> = {
};

export default async function middleware(request: NextRequest) {
const origin = request.headers.get('x-forwarded-host') || '';
if (origin !== process.env.MAIN_URL) {
headers['Access-Control-Allow-Origin'] = allowedOrigins[origin] || 'https://www.dreampip.com';
}

// You could alternatively limit based on user ID or similar
const response = next();
const ip = ipAddress(request) || '127.0.0.1';
Expand Down
Loading