Skip to content

Commit

Permalink
added external-auths endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Sep 2, 2022
1 parent dcf0be8 commit f1f5089
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,8 @@ const client = new PocketBase(
| 🔐`client.users.create(bodyParams = {}, queryParams = {})` | Creates a new user. |
| 🔐`client.users.update(id, bodyParams = {}, queryParams = {})` | Updates an existing user by its id. |
| 🔐`client.users.delete(id, queryParams = {})` | Deletes an existing user by its id. |
| 🔐`client.users.listExternalAuths(id, queryParams = {})` | Lists all linked external auth providers for the specified user. |
| 🔐`client.users.unlinkExternalAuth(id, provider, queryParams = {})` | Unlink a single external auth provider from the specified user. |
| **[Realtime](https://pocketbase.io/docs/api-realtime)** <br/> _(for node environments you'll have to install an EventSource polyfill beforehand, eg. https://github.com/EventSource/eventsource)_ | |
| 🔓`client.realtime.subscribe(subscription, callback)` | Inits the sse connection (if not already) and register the subscription. |
| 🔓`client.realtime.unsubscribe(subscription = "")` | Unsubscribe from a subscription (if empty - unsubscribe from all registered subscriptions). |
Expand Down
19 changes: 19 additions & 0 deletions dist/pocketbase.cjs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,17 @@ declare class Admins extends CrudService<Admin> {
*/
confirmPasswordReset(passwordResetToken: string, password: string, passwordConfirm: string, bodyParams?: {}, queryParams?: {}): Promise<AdminAuthResponse>;
}
declare class ExternalAuth extends BaseModel {
userId: string;
provider: string;
providerId: string;
/**
* @inheritdoc
*/
load(data: {
[key: string]: any;
}): void;
}
type UserAuthResponse = {
[key: string]: any;
token: string;
Expand Down Expand Up @@ -365,6 +376,14 @@ declare class Users extends CrudService<User> {
* Confirms user new email address.
*/
confirmEmailChange(emailChangeToken: string, password: string, bodyParams?: {}, queryParams?: {}): Promise<UserAuthResponse>;
/**
* Lists all linked external auth providers for the specified user.
*/
listExternalAuths(userId: string, queryParams?: {}): Promise<Array<ExternalAuth>>;
/**
* Unlink a single external auth provider from the specified user.
*/
unlinkExternalAuth(userId: string, provider: string, queryParams?: {}): Promise<boolean>;
}
declare class SchemaField {
id: string;
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.cjs.js

Large diffs are not rendered by default.

21 changes: 20 additions & 1 deletion dist/pocketbase.es.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,17 @@ declare class Admins extends CrudService<Admin> {
*/
confirmPasswordReset(passwordResetToken: string, password: string, passwordConfirm: string, bodyParams?: {}, queryParams?: {}): Promise<AdminAuthResponse>;
}
declare class ExternalAuth extends BaseModel {
userId: string;
provider: string;
providerId: string;
/**
* @inheritdoc
*/
load(data: {
[key: string]: any;
}): void;
}
type UserAuthResponse = {
[key: string]: any;
token: string;
Expand Down Expand Up @@ -365,6 +376,14 @@ declare class Users extends CrudService<User> {
* Confirms user new email address.
*/
confirmEmailChange(emailChangeToken: string, password: string, bodyParams?: {}, queryParams?: {}): Promise<UserAuthResponse>;
/**
* Lists all linked external auth providers for the specified user.
*/
listExternalAuths(userId: string, queryParams?: {}): Promise<Array<ExternalAuth>>;
/**
* Unlink a single external auth provider from the specified user.
*/
unlinkExternalAuth(userId: string, provider: string, queryParams?: {}): Promise<boolean>;
}
declare class SchemaField {
id: string;
Expand Down Expand Up @@ -745,4 +764,4 @@ declare class LocalAuthStore extends BaseAuthStore {
*/
private _storageRemove;
}
export { Client as default, ClientResponseError, BaseAuthStore, LocalAuthStore, User, Admin, Collection, Record, LogRequest, SchemaField };
export { Client as default, ClientResponseError, BaseAuthStore, LocalAuthStore, ExternalAuth, User, Admin, Collection, Record, LogRequest, SchemaField };
2 changes: 1 addition & 1 deletion dist/pocketbase.es.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.mjs.map

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions dist/pocketbase.iife.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,17 @@ declare class Admins extends CrudService<Admin> {
*/
confirmPasswordReset(passwordResetToken: string, password: string, passwordConfirm: string, bodyParams?: {}, queryParams?: {}): Promise<AdminAuthResponse>;
}
declare class ExternalAuth extends BaseModel {
userId: string;
provider: string;
providerId: string;
/**
* @inheritdoc
*/
load(data: {
[key: string]: any;
}): void;
}
type UserAuthResponse = {
[key: string]: any;
token: string;
Expand Down Expand Up @@ -365,6 +376,14 @@ declare class Users extends CrudService<User> {
* Confirms user new email address.
*/
confirmEmailChange(emailChangeToken: string, password: string, bodyParams?: {}, queryParams?: {}): Promise<UserAuthResponse>;
/**
* Lists all linked external auth providers for the specified user.
*/
listExternalAuths(userId: string, queryParams?: {}): Promise<Array<ExternalAuth>>;
/**
* Unlink a single external auth provider from the specified user.
*/
unlinkExternalAuth(userId: string, provider: string, queryParams?: {}): Promise<boolean>;
}
declare class SchemaField {
id: string;
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.iife.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions dist/pocketbase.umd.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,17 @@ declare class Admins extends CrudService<Admin> {
*/
confirmPasswordReset(passwordResetToken: string, password: string, passwordConfirm: string, bodyParams?: {}, queryParams?: {}): Promise<AdminAuthResponse>;
}
declare class ExternalAuth extends BaseModel {
userId: string;
provider: string;
providerId: string;
/**
* @inheritdoc
*/
load(data: {
[key: string]: any;
}): void;
}
type UserAuthResponse = {
[key: string]: any;
token: string;
Expand Down Expand Up @@ -365,6 +376,14 @@ declare class Users extends CrudService<User> {
* Confirms user new email address.
*/
confirmEmailChange(emailChangeToken: string, password: string, bodyParams?: {}, queryParams?: {}): Promise<UserAuthResponse>;
/**
* Lists all linked external auth providers for the specified user.
*/
listExternalAuths(userId: string, queryParams?: {}): Promise<Array<ExternalAuth>>;
/**
* Unlink a single external auth provider from the specified user.
*/
unlinkExternalAuth(userId: string, provider: string, queryParams?: {}): Promise<boolean>;
}
declare class SchemaField {
id: string;
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.6.2",
"version": "0.7.0",
"name": "pocketbase",
"description": "PocketBase JavaScript SDK",
"author": "Gani Georgiev",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Client from '@/Client';
import ClientResponseError from '@/ClientResponseError';
import BaseAuthStore from '@/stores/BaseAuthStore';
import LocalAuthStore from '@/stores/LocalAuthStore';
import ExternalAuth from '@/models/ExternalAuth';
import User from '@/models/User';
import Admin from '@/models/Admin';
import Collection from '@/models/Collection';
Expand All @@ -13,6 +14,7 @@ export {
ClientResponseError,
BaseAuthStore,
LocalAuthStore,
ExternalAuth,
User,
Admin,
Collection,
Expand Down
18 changes: 18 additions & 0 deletions src/models/ExternalAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import BaseModel from '@/models/utils/BaseModel';

export default class ExternalAuth extends BaseModel {
userId!: string;
provider!: string;
providerId!: string;

/**
* @inheritdoc
*/
load(data: { [key: string]: any }) {
super.load(data);

this.userId = typeof data.userId === 'string' ? data.userId : '';
this.provider = typeof data.provider === 'string' ? data.provider : '';
this.providerId = typeof data.providerId === 'string' ? data.providerId : '';
}
}
42 changes: 40 additions & 2 deletions src/services/Users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import CrudService from '@/services/utils/CrudService';
import User from '@/models/User';
import CrudService from '@/services/utils/CrudService';
import User from '@/models/User';
import ExternalAuth from '@/models/ExternalAuth';

export type UserAuthResponse = {
[key: string]: any,
Expand Down Expand Up @@ -266,4 +267,41 @@ export default class Users extends CrudService<User> {
'body': bodyParams,
}).then(this.authResponse.bind(this));
}

/**
* Lists all linked external auth providers for the specified user.
*/
listExternalAuths(
userId: string,
queryParams = {}
): Promise<Array<ExternalAuth>> {
return this.client.send(this.baseCrudPath() + '/' + encodeURIComponent(userId) + '/external-auths', {
'method': 'GET',
'params': queryParams,
}).then((responseData) => {
const items: Array<ExternalAuth> = [];

if (Array.isArray(responseData)) {
for (const item of responseData) {
items.push(new ExternalAuth(item));
}
}

return items;
});
}

/**
* Unlink a single external auth provider from the specified user.
*/
unlinkExternalAuth(
userId: string,
provider: string,
queryParams = {}
): Promise<boolean> {
return this.client.send(this.baseCrudPath() + '/' + encodeURIComponent(userId) + '/external-auths/' + encodeURIComponent(provider), {
'method': 'DELETE',
'params': queryParams,
}).then(() => true);
}
}
34 changes: 33 additions & 1 deletion tests/services/Users.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,42 @@ describe('Users', function() {
},
});


const result = await service.confirmEmailChange('test', '1234', { 'b1': 123 }, { 'q1': 456 });

authResponseCheck(result, 'token_email_change_confirm', service.decode({ 'id': 'id_email_change_confirm' }));
});
});

describe('listExternalAuths()', function() {
it('Should send a list external auths request', async function() {
fetchMock.on({
method: 'GET',
url: service.client.buildUrl('/api/users/' + encodeURIComponent('@test_id') + '/external-auths') + '?q1=456',
replyCode: 200,
replyBody: [
{ id: '1', provider: 'google' },
{ id: '2', provider: 'github' },
],
});

const result = await service.listExternalAuths('@test_id', { 'q1': 456 });
assert.equal(result.length, 2);
assert.equal(result[0].provider, 'google');
assert.equal(result[1].provider, 'github');
});
});

describe('unlinkExternalAuth()', function() {
it('Should send a unlinkExternalAuth request', async function() {
fetchMock.on({
method: 'DELETE',
url: service.client.buildUrl('/api/users/' + encodeURIComponent("@test_id") + "/external-auths/" + encodeURIComponent("@test_provider")) + '?q1=456',
replyCode: 204,
replyBody: true,
});

const result = await service.unlinkExternalAuth('@test_id', '@test_provider', { 'q1': 456 });
assert.isTrue(result);
});
});
});

0 comments on commit f1f5089

Please sign in to comment.