Fetching user doc

This commit is contained in:
Eric van der Vlist 2023-03-03 11:06:21 +01:00
parent 5d9f494a41
commit c6951c0838
3 changed files with 41 additions and 5 deletions

View File

@ -0,0 +1,34 @@
import { adminCredentials } from '~/components/credentials';
import { headersWithAuth } from './headers-with-auth';
export const findUserByToken = async (
token: string,
credentials = adminCredentials()
) => {
const db = 'dyomedea_users';
if (!credentials) {
return null;
}
const { database } = credentials;
const headers = headersWithAuth(credentials);
if (!headers) {
return null;
}
headers.set('Content-Type', 'application/json');
const params = {
selector: {
token,
},
};
const response = await fetch(`${database}/${db}/_find`, {
method: 'POST',
mode: 'cors',
headers,
body: JSON.stringify(params),
});
return (await response.json()).docs[0];
};

View File

@ -1,7 +1,6 @@
import { adminCredentials } from '~/components/credentials';
export const headersWithAuth = () => {
const credentials = adminCredentials();
export const headersWithAuth = (credentials = adminCredentials()) => {
if (!credentials) {
return null;
}

View File

@ -1,8 +1,11 @@
import { APIEvent, json } from 'solid-start/api';
import { findUserByToken } from '~/lib/find-user-by-token';
import { readConfig } from '~/server-only-lib/read-config';
export async function GET({ params, env }: APIEvent) {
const {credentials} = readConfig();
export async function GET({ params }: APIEvent) {
const { credentials } = readConfig();
console.log({ caller: 'api/conf GET', params, credentials });
return json({ params, env, response: 'OK' });
const user = await findUserByToken(params.token, credentials);
console.log({ caller: 'api/conf GET', params, credentials, user });
return json({ params, response: 'OK' });
}