Skip to content

Commit

Permalink
Merge pull request #2320 from powerful23/federatedSignIn-warning
Browse files Browse the repository at this point in the history
Federated sign in warning
  • Loading branch information
powerful23 authored Dec 19, 2018
2 parents 2726d08 + 7e66b57 commit 2aeb773
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
11 changes: 9 additions & 2 deletions packages/auth/__tests__/auth-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1827,15 +1827,22 @@ describe('auth unit test', () => {
describe('federatedSignIn test', () => {
test('happy case', async () => {
const auth = new Auth(authOptions);

let user = null;
const spyon = jest.spyOn(Credentials, 'set').mockImplementationOnce(() => {
user = { name: 'username', email: 'xxx@email.com'};
return Promise.resolve('cred');
});
const spyon2 = jest.spyOn(Auth.prototype, 'currentAuthenticatedUser').mockImplementation(() => {
if (!user) return Promise.reject('error');
else return Promise.resolve(user);
});

auth.federatedSignIn('google', { token: 'token', expires_at: 1234 }, { user: 'user' });

await auth.federatedSignIn('google', { token: 'token', expires_at: 1234 }, { name: 'username' });

expect(spyon).toBeCalled();
spyon.mockClear();
spyon2.mockClear();
});
});

Expand Down
28 changes: 15 additions & 13 deletions packages/auth/src/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1322,24 +1322,26 @@ export default class AuthClass {
* and the expiration time (the universal time)
* @param {String} user - user info
*/
public federatedSignIn(
public async federatedSignIn(
provider: 'google'|'facebook'|'amazon'|'developer'|string,
response: FederatedResponse,
user: FederatedUser
): Promise<ICredentials>{
// To check if the user is already logged in
try {
const loggedInUser = await this.currentAuthenticatedUser();
logger.warn(`There is already a signed in user: ${loggedInUser} in your app.
You should not call Auth.federatedSignIn method again as it may cause unexpected behavior.`);
} catch (e) {}

const { token, identity_id, expires_at } = response;
const that = this;
return new Promise((res, rej) => {
Credentials.set({ provider, token, identity_id, user, expires_at }, 'federation').then((cred) => {
dispatchAuthEvent('signIn', that.user);
logger.debug('federated sign in credentials', cred);
res(cred);
return;
}).catch(e => {
rej(e);
return;
});
});
// Because Credentials.set would update the user info with identity id
// So we need to retrieve the user again.
const credentials = await Credentials.set({ provider, token, identity_id, user, expires_at }, 'federation');
const currentUser = await this.currentAuthenticatedUser();
dispatchAuthEvent('signIn', currentUser);
logger.debug('federated sign in credentials', credentials);
return credentials;
}

/**
Expand Down

0 comments on commit 2aeb773

Please sign in to comment.