Skip to content

Commit

Permalink
chore(server): add more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jrea committed Aug 29, 2024
1 parent 76788e1 commit 3526569
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/server/src/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export class Api {
this.tenants = new Tenants(this.config, headers);
}
async login(payload: { email: string; password: string }) {
this.headers = await serverAuth(this.config)(payload);
this.headers = await serverAuth(this.config, this.handlers)(payload);
}
}
5 changes: 5 additions & 0 deletions packages/server/src/api/handlers/DELETE.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Logger from '../../utils/Logger';
import tenants, { matches as matchesTenants } from '../routes/tenants';
import tenantUsers, {
matches as matchesTenantsUsers,
Expand All @@ -6,13 +7,17 @@ import { Routes } from '../types';
import { Config } from '../../utils/Config';

export default function DELETER(configRoutes: Routes, config: Config) {
const { info, warn } = Logger(config, '[DELETE MATCHER]');
return async function DELETE(req: Request) {
if (matchesTenantsUsers(configRoutes, req)) {
info('matches tenant users');
return tenantUsers(req, config);
}
if (matchesTenants(configRoutes, req)) {
info('matches tenants');
return tenants(req, config);
}
warn('No DELETE routes matched');
return new Response(null, { status: 404 });
};
}
21 changes: 17 additions & 4 deletions packages/server/src/api/handlers/GET.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Logger from '../../utils/Logger';
import me, { matches as matchesMe } from '../routes/me';
import users, { matches as matchesUsers } from '../routes/users';
import tenantUsers, {
Expand All @@ -9,47 +10,59 @@ import * as authRoutes from '../routes/auth';
import { Config } from '../../utils/Config';

export default function GETTER(configRoutes: Routes, config: Config) {
const { info, warn } = Logger(config, '[GET MATCHER]');
return async function GET(req: Request) {
if (matchesMe(configRoutes, req)) {
info('matches me');
return me(req, config);
}
if (matchesUsers(configRoutes, req)) {
return users(req, config);
}

if (matchesTenantUsers(configRoutes, req)) {
info('matches tenant users');
return tenantUsers(req, config);
}
if (matchesUsers(configRoutes, req)) {
info('matches users');
return users(req, config);
}

if (matchesTenants(configRoutes, req)) {
info('matches tenants');
return tenants(req, config);
}

if (authRoutes.matchSession(configRoutes, req)) {
info('matches session');
return authRoutes.handleSession(req, config);
}

if (authRoutes.matchSignIn(configRoutes, req)) {
info('matches signin');
return authRoutes.handleSignIn(req, config);
}

if (authRoutes.matchProviders(configRoutes, req)) {
info('matches providers');
return authRoutes.handleProviders(req, config);
}

if (authRoutes.matchCsrf(configRoutes, req)) {
info('matches csrf');
return authRoutes.handleCsrf(req, config);
}

if (authRoutes.matchCallback(configRoutes, req)) {
info('matches callback');
return authRoutes.handleCallback(req, config);
}
if (authRoutes.matchSignOut(configRoutes, req)) {
info('matches signout');
return authRoutes.handleSignOut(req, config);
}
if (authRoutes.matchError(configRoutes, req)) {
info('matches error');
return authRoutes.handleError(req, config);
}
warn('No GET routes matched');
return new Response(null, { status: 404 });
};
}
12 changes: 12 additions & 0 deletions packages/server/src/api/handlers/POST.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Logger from '../../utils/Logger';
import users, { matches as matchesUsers } from '../routes/users';
import tenants, { matches as matchesTenants } from '../routes/tenants';
import tenantUsers, {
Expand All @@ -8,41 +9,52 @@ import { Config } from '../../utils/Config';
import * as authRoutes from '../routes/auth';

export default function POSTER(configRoutes: Routes, config: Config) {
const { info, warn } = Logger(config, '[POST MATCHER]');
return async function POST(req: Request) {
// order matters for tenantUsers
if (matchesTenantUsers(configRoutes, req)) {
info('matches tenant users');
return tenantUsers(req, config);
}

if (matchesUsers(configRoutes, req)) {
info('matches users');
return users(req, config);
}
if (matchesTenants(configRoutes, req)) {
info('matches tenants');
return tenants(req, config);
}

if (authRoutes.matchSession(configRoutes, req)) {
info('matches session');
return authRoutes.handleSession(req, config);
}

if (authRoutes.matchSignIn(configRoutes, req)) {
info('matches signin');
return authRoutes.handleSignIn(req, config);
}

if (authRoutes.matchProviders(configRoutes, req)) {
info('matches providers');
return authRoutes.handleProviders(req, config);
}

if (authRoutes.matchCsrf(configRoutes, req)) {
info('matches csrf');
return authRoutes.handleCsrf(req, config);
}

if (authRoutes.matchCallback(configRoutes, req)) {
info('matches callback');
return authRoutes.handleCallback(req, config);
}
if (authRoutes.matchSignOut(configRoutes, req)) {
info('matches signout');
return authRoutes.handleSignOut(req, config);
}
warn('No POST routes matched');
return new Response(null, { status: 404 });
};
}
6 changes: 6 additions & 0 deletions packages/server/src/api/handlers/PUT.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Logger from '../../utils/Logger';
import users, { matches as matchesUsers } from '../routes/users';
import tenants, { matches as matchesTenants } from '../routes/tenants';
import tenantUsers, {
Expand All @@ -7,16 +8,21 @@ import { Routes } from '../types';
import { Config } from '../../utils/Config';

export default function PUTER(configRoutes: Routes, config: Config) {
const { info, warn } = Logger(config, '[PUT MATCHER]');
return async function PUT(req: Request) {
if (matchesTenantUsers(configRoutes, req)) {
info('matches tenant users');
return tenantUsers(req, config);
}
if (matchesUsers(configRoutes, req)) {
info('matches users');
return users(req, config);
}
if (matchesTenants(configRoutes, req)) {
info('matches tenants');
return tenants(req, config);
}
warn('No PUT routes matched');
return new Response(null, { status: 404 });
};
}
9 changes: 7 additions & 2 deletions packages/server/src/api/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default async function auth(
req: Request,
config: Config
): Promise<void | ActiveSession> {
const { info } = Logger({ ...config, debug: config.debug }, '[nileauth]');
const { info, error } = Logger(config, '[nileauth]');
info('checking auth');

const sessionUrl = `${config.api.basePath}/auth/session`;
Expand All @@ -33,5 +33,10 @@ export default async function auth(
return undefined;
}
info('session active');
return await new Response(res.body).json();
try {
return await new Response(res.body).json();
} catch (e) {
error(e);
return undefined;
}
}
21 changes: 17 additions & 4 deletions packages/server/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import Logger from '../utils/Logger';
/**
* a helper function to log in server side.
*/
export default function login(config: Config) {
export default function serverAuth(
config: Config,
handlers: {
GET: (req: Request) => Promise<void | Response>;
POST: (req: Request) => Promise<void | Response>;
DELETE: (req: Request) => Promise<void | Response>;
PUT: (req: Request) => Promise<void | Response>;
}
) {
const { info, error } = Logger(config, '[server side login]');
const routes = proxyRoutes(config);
return async function login({
Expand All @@ -27,7 +35,12 @@ export default function login(config: Config) {
host: sessionUrl.host,
}),
});
const sessionRes = await fetch(sessionReq);
const sessionRes = await handlers.POST(sessionReq);

if (sessionRes?.status === 404) {
throw new Error('Unable to login, cannot find region api.');
}

let providers;
try {
providers = await sessionRes?.json();
Expand All @@ -44,7 +57,7 @@ export default function login(config: Config) {
host: sessionUrl.host,
}),
});
const csrfRes = await fetch(csrfReq);
const csrfRes = await handlers.POST(csrfReq);
let csrfToken;
try {
const json = (await csrfRes?.json()) ?? {};
Expand Down Expand Up @@ -82,7 +95,7 @@ export default function login(config: Config) {
callbackUrl: credentials.callbackUrl,
}),
});
const loginRes = await fetch(postReq);
const loginRes = await handlers.POST(postReq);
const authCookie = loginRes?.headers.get('set-cookie');
if (!authCookie) {
throw new Error('authentication failed');
Expand Down

0 comments on commit 3526569

Please sign in to comment.