Skip to content

Commit

Permalink
fix(server): signup and login
Browse files Browse the repository at this point in the history
  • Loading branch information
jrea committed Aug 13, 2024
1 parent 1551171 commit 62897d6
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/browser/templates/apis.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class {{classname}} extends runtime.BaseAPI {
const requestTenantId = localStorage.getItem('tenantId');
if (requestTenantId) {
headerParameters['x-nile-tenantId'] = requestTenantId;
headerParameters['niledb-tenant-id'] = requestTenantId;
}
{{#bodyParam}}
Expand Down Expand Up @@ -284,7 +284,7 @@ export class {{classname}} extends runtime.BaseAPI {
{{/hasFormParams}}
}, initOverrides);
const headers = new Headers(response.headers);
const responseTenantId = headers.get('x-nile-tenantId')
const responseTenantId = headers.get('niledb-tenant-id')
if (responseTenantId) {
localStorage.setItem('tenantId', responseTenantId);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/edge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function middleware(
tenantId = parsed[key];
}
if (tenantId) {
response.headers.set('x-nile-tenantId', tenantId);
response.headers.set('niledb-tenant-id', tenantId);
}
}

Expand Down
9 changes: 7 additions & 2 deletions packages/server/src/api/routes/tenants/GET.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ export async function GET(
init: RequestInit & { request: Request },
log: (...args: string[]) => void
) {
const url = `${apiRoutes.USER_TENANTS(session.id)}`;
let url = `${apiRoutes.USER_TENANTS(session.id)}`;
if (typeof session === 'object' && 'user' in session && session.user) {
url = `${apiRoutes.USER_TENANTS(session.user.id)}`;
}
log('[GET]', url);
return await request(url, init);

const res = await request(url, init);
return res;
}
2 changes: 1 addition & 1 deletion packages/server/src/api/routes/users/GET.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { apiRoutes } from '../../utils/routes/apiRoutes';
* - users
* summary: lists users in the tenant
* description: Returns information about the users within the tenant
* provided. You can also pass the a `niledb-tenantId` in the header or in a cookie.
* provided. You can also pass the a `niledb-tenant-id` in the header or in a cookie.
* operationId: listUsers
* parameters:
* - name: tenantId
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/api/routes/users/POST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export async function POST(
const yurl = new URL(init.request.url);
const tenantId = yurl.searchParams.get('tenantId');
const tenant = tenantId ?? getTenantFromHttp(init.request.headers);

const url = apiRoutes.USERS(tenant ? tenant : undefined);
log && log('[POST]', url);

Expand Down
17 changes: 15 additions & 2 deletions packages/server/src/api/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@ import Logger from '../../utils/Logger';

import request from './request';

export type ActiveSession = { id: string; email: string };
export type ActiveSession = {
id: string;
email: string;
expires: Date;
user?: {
id: string;
name: string;
image: string;
email: string;
emailVerified: void | Date;
};
};
export default async function auth(
req: Request,
config: Config
): Promise<void | ActiveSession> {
const { info } = Logger({ ...config, debug: true }, '[nileauth]');
const { info } = Logger({ ...config, debug: config.debug }, '[nileauth]');
info('checking auth');

const sessionUrl = `${config.api.basePath}/auth/session`;
info('using session', sessionUrl);
// handle the pass through with posts
req.headers.delete('content-length');

const res = await request(sessionUrl, { request: req });
if (!res) {
Expand Down

0 comments on commit 62897d6

Please sign in to comment.