Fetching user doc
This commit is contained in:
parent
5d9f494a41
commit
c6951c0838
|
@ -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];
|
||||||
|
};
|
|
@ -1,7 +1,6 @@
|
||||||
import { adminCredentials } from '~/components/credentials';
|
import { adminCredentials } from '~/components/credentials';
|
||||||
|
|
||||||
export const headersWithAuth = () => {
|
export const headersWithAuth = (credentials = adminCredentials()) => {
|
||||||
const credentials = adminCredentials();
|
|
||||||
if (!credentials) {
|
if (!credentials) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
import { APIEvent, json } from 'solid-start/api';
|
import { APIEvent, json } from 'solid-start/api';
|
||||||
|
import { findUserByToken } from '~/lib/find-user-by-token';
|
||||||
import { readConfig } from '~/server-only-lib/read-config';
|
import { readConfig } from '~/server-only-lib/read-config';
|
||||||
|
|
||||||
export async function GET({ params, env }: APIEvent) {
|
export async function GET({ params }: APIEvent) {
|
||||||
const { credentials } = readConfig();
|
const { credentials } = readConfig();
|
||||||
console.log({ caller: 'api/conf GET', params, credentials });
|
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' });
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue