Skip to content

Commit 35b5dfc

Browse files
authored
Merge pull request #41 from goniszewski/feat/api-endpoints-for-external-integrations
feat: API endpoints for external integrations
2 parents 55266c2 + fbb6eef commit 35b5dfc

File tree

6 files changed

+716
-17
lines changed

6 files changed

+716
-17
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"daisyui": "^3.9.4",
6262
"fuse.js": "^7.0.0",
6363
"html-to-text": "^9.0.5",
64+
"joi": "^17.11.0",
6465
"lodash": "^4.17.21",
6566
"metascraper": "^5.37.1",
6667
"metascraper-author": "^5.37.1",

pnpm-lock.yaml

+37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/pb.ts

+70-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import PocketBase, { BaseAuthStore, ClientResponseError } from 'pocketbase';
22
import { writable } from 'svelte/store';
33

4-
import { error, fail } from '@sveltejs/kit';
4+
import { error, fail, json } from '@sveltejs/kit';
55

66
import config from './config';
77

8+
import type { RecordModel } from 'pocketbase';
89
import type { User } from './types/User.type';
910
import type { UserSettings } from './types/UserSettings.type';
1011
export const pb = new PocketBase(config.POCKETBASE_URL);
@@ -110,3 +111,71 @@ export async function handlePBError(e: any, pb: PocketBase, form?: boolean) {
110111
throw error(e.status, e.message);
111112
}
112113
}
114+
115+
export type authenticateUserApiRequestResponse = {
116+
owner: string;
117+
disabled: boolean | null;
118+
userRecord: RecordModel | null;
119+
error: Response | null;
120+
};
121+
122+
export async function authenticateUserApiRequest(
123+
pb: PocketBase,
124+
request: Request
125+
): Promise<authenticateUserApiRequestResponse> {
126+
const authKey = request.headers.get('Authorization') ?? '';
127+
128+
const response: authenticateUserApiRequestResponse = {
129+
owner: '',
130+
disabled: null,
131+
userRecord: null,
132+
error: null
133+
};
134+
135+
try {
136+
const [login, password] = atob(authKey.split(' ')[1]).split(':');
137+
138+
const user = await pb
139+
.collection('users')
140+
.authWithPassword(login, password)
141+
.then((user) => user.record);
142+
143+
response.owner = user.id;
144+
response.disabled = !!user.disabled;
145+
response.userRecord = user;
146+
} catch (error: any) {
147+
response.error = json(
148+
{
149+
success: false,
150+
error: `Problem with authorization token: ${error?.message}`
151+
},
152+
{
153+
status: 401
154+
}
155+
);
156+
}
157+
158+
if (!response.owner) {
159+
response.error = json(
160+
{
161+
success: false,
162+
error: 'Unauthorized'
163+
},
164+
{
165+
status: 401
166+
}
167+
);
168+
} else if (response.disabled) {
169+
response.error = json(
170+
{
171+
success: false,
172+
error: 'User disabled'
173+
},
174+
{
175+
status: 401
176+
}
177+
);
178+
}
179+
180+
return response;
181+
}

0 commit comments

Comments
 (0)