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';
|
||||
|
||||
export const headersWithAuth = () => {
|
||||
const credentials = adminCredentials();
|
||||
export const headersWithAuth = (credentials = adminCredentials()) => {
|
||||
if (!credentials) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
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' });
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue