Skip to content

Commit 3bdb602

Browse files
committed
Merge branch 'auth-services' into remove-email-server
2 parents 9e305e4 + 55adc07 commit 3bdb602

File tree

25 files changed

+306
-449
lines changed

25 files changed

+306
-449
lines changed

packages/client/tmp_tests_/accounts-client.ts renamed to packages/client/__tests__/accounts-client.ts

Lines changed: 50 additions & 292 deletions
Large diffs are not rendered by default.

packages/client/src/accounts-client.ts

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import config, { TokenStorage, AccountsClientConfiguration } from './config';
1717
import createStore from './create-store';
1818
import reducer, {
1919
loggingIn,
20-
setUser,
2120
clearUser,
2221
setTokens,
2322
clearTokens as clearStoreTokens,
@@ -154,7 +153,6 @@ export class AccountsClient {
154153
}
155154

156155
this.store.dispatch(setTokens(res.tokens));
157-
this.store.dispatch(setUser(res.user));
158156
return res;
159157
}
160158
}
@@ -278,7 +276,6 @@ export class AccountsClient {
278276

279277
await this.storeTokens(refreshedSession.tokens);
280278
this.store.dispatch(setTokens(refreshedSession.tokens));
281-
this.store.dispatch(setUser(refreshedSession.user));
282279
}
283280
} catch (err) {
284281
this.store.dispatch(loggingIn(false));
@@ -293,10 +290,7 @@ export class AccountsClient {
293290
}
294291
}
295292

296-
public async createUser(
297-
user: CreateUserType,
298-
callback?: (err?: Error) => void
299-
): Promise<void> {
293+
public async createUser(user: CreateUserType): Promise<void> {
300294
if (!user) {
301295
throw new AccountsError(
302296
'Unrecognized options for create user request',
@@ -323,9 +317,7 @@ export class AccountsClient {
323317
try {
324318
const userId = await this.transport.createUser(userToCreate);
325319
const { onUserCreated } = this.options;
326-
if (callback && isFunction(callback)) {
327-
callback();
328-
}
320+
329321
if (isFunction(onUserCreated)) {
330322
try {
331323
await onUserCreated({ id: userId });
@@ -335,9 +327,45 @@ export class AccountsClient {
335327
}
336328
}
337329
} catch (err) {
338-
if (callback && isFunction(callback)) {
339-
callback(err);
330+
throw new AccountsError(err.message);
331+
}
332+
}
333+
334+
public async loginWithService(
335+
service: string,
336+
credentials: { [key: string]: string | object }
337+
): Promise<LoginReturnType> {
338+
if (!isString(service)) {
339+
throw new AccountsError('Unrecognized options for login request');
340+
}
341+
342+
try {
343+
this.store.dispatch(loggingIn(true));
344+
345+
const response = await this.transport.loginWithService(
346+
service,
347+
credentials
348+
);
349+
350+
this.store.dispatch(loggingIn(false));
351+
await this.storeTokens(response.tokens);
352+
this.store.dispatch(setTokens(response.tokens));
353+
354+
const { onSignedInHook } = this.options;
355+
356+
if (isFunction(onSignedInHook)) {
357+
try {
358+
await onSignedInHook(response);
359+
} catch (err) {
360+
// tslint:disable-next-line no-console
361+
console.error(err);
362+
}
340363
}
364+
return response;
365+
} catch (err) {
366+
this.clearTokens();
367+
this.store.dispatch(clearUser());
368+
this.store.dispatch(loggingIn(false));
341369
throw new AccountsError(err.message);
342370
}
343371
}
@@ -419,12 +447,11 @@ const Accounts = {
419447
): Promise<void> {
420448
return this.instance.createUser(user, callback);
421449
},
422-
loginWithPassword(
423-
user: LoginUserIdentityType,
424-
password: string,
425-
callback?: (err?: Error, res?: LoginReturnType) => void
426-
): Promise<void> {
427-
return this.instance.loginWithPassword(user, password, callback);
450+
loginWithService(
451+
service: string,
452+
credentials: { [key: string]: string | object }
453+
): Promise<LoginReturnType> {
454+
return this.instance.loginWithService(service, credentials);
428455
},
429456
loggingIn(): boolean {
430457
return this.instance.loggingIn();

packages/client/src/transport-interface.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ import {
66

77
export interface TransportInterface {
88
createUser(user: CreateUserType): Promise<string>;
9-
// loginWithPassword(
10-
// user: PasswordLoginUserType,
11-
// password: PasswordType
12-
// ): Promise<LoginReturnType>;
9+
loginWithService(
10+
service: string,
11+
authenticateParams: {
12+
[key: string]: string | object;
13+
}
14+
): Promise<LoginReturnType>;
1315
logout(accessToken: string): Promise<void>;
1416
refreshTokens(
1517
accessToken: string,
1618
refreshToken: string
1719
): Promise<LoginReturnType>;
1820
verifyEmail(token: string): Promise<void>;
19-
// resetPassword(token: string, newPassword: PasswordType): Promise<void>;
2021
sendResetPasswordEmail(email: string): Promise<void>;
2122
sendVerificationEmail(email: string): Promise<void>;
2223
impersonate(token: string, username: string): Promise<ImpersonateReturnType>;

packages/common/__tests__/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import { AccountsError } from '../src';
33
describe('common', () => {
44
it('should have named export AccountsError', () => {
55
expect(typeof AccountsError).toBe('function');
6-
})
7-
})
6+
});
7+
});

packages/common/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface CreateUserType {
2323
username?: string;
2424
email?: string;
2525
profile?: object;
26+
[additionalKey: string]: any;
2627
}
2728

2829
export interface LoginUserIdentityType {
@@ -38,7 +39,6 @@ export interface TokensType {
3839

3940
export interface LoginReturnType {
4041
sessionId: string;
41-
user: UserObjectType;
4242
tokens: TokensType;
4343
}
4444

packages/oauth-instagram/__tests__/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import AccountsOAuthInstagram from '../src';
33
describe('AccountsOAuthInstagram', () => {
44
it('should have default export AccountsOAuthInstagram', () => {
55
expect(typeof AccountsOAuthInstagram).toBe('function');
6-
})
7-
})
6+
});
7+
});

packages/oauth-instagram/src/accounts-oauth-instagram.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import * as rp from 'request-promise';
22

33
export class AccountsOAuthInstagram {
44
public async authenticate(params) {
5-
let data = await rp(`https://api.instagram.com/v1/users/self/?access_token=${params.access_token}`);
5+
let data = await rp(
6+
`https://api.instagram.com/v1/users/self/?access_token=${
7+
params.access_token
8+
}`
9+
);
610
data = JSON.parse(data).data;
711
return {
812
id: data.id,

packages/oauth-instagram/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
import { AccountsOAuthInstagram } from './accounts-oauth-instagram';
2-
export default AccountsOAuthInstagram;
2+
export default AccountsOAuthInstagram;

packages/oauth-twitter/__tests__/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import AccountsOAuthTwitter from '../src';
33
describe('AccountsOAuthTwitter', () => {
44
it('should have default export AccountsOAuthTwitter', () => {
55
expect(typeof AccountsOAuthTwitter).toBe('function');
6-
})
7-
})
6+
});
7+
});

packages/oauth-twitter/src/accounts-oauth-twitter.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class AccountsOAuthTwitter {
1818
this.options.secret,
1919
'1.0A',
2020
null,
21-
'HMAC-SHA1',
21+
'HMAC-SHA1'
2222
);
2323
}
2424

@@ -43,9 +43,8 @@ export class AccountsOAuthTwitter {
4343
};
4444
resolve(user);
4545
}
46-
},
46+
}
4747
);
4848
});
49-
5049
}
5150
}

0 commit comments

Comments
 (0)