Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/core/__tests__/Credentials-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ const cacheClass = {
};

describe('Credentials test', () => {
describe('.Auth', () => {
it('should be undefined by default', async () => {
const credentials = new Credentials(null);

expect(credentials.Auth).toBeUndefined();

expect(credentials.get()).rejects.toMatchInlineSnapshot(
`"No Auth module registered in Amplify"`
);
});

it('should be Amplify.Auth if configured through Amplify', () => {
const credentials = new Credentials(null);

Amplify.register(authClass);
Amplify.register(credentials);

Amplify.configure({});

expect(credentials.Auth).toBe(authClass);
expect(credentials.get()).resolves.toBe('cred');
});
});

describe('configure test', () => {
test('happy case', () => {
const config = {
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/Credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class CredentialsClass {
private _identityId;
private _nextCredentialsRefresh: Number;

// `Amplify.Auth` will either be `Auth` or `null` depending on if Auth was imported
Auth = Amplify.Auth;
// Allow `Auth` to be injected for SSR, but Auth isn't a required dependency for Credentials
Auth = undefined;

constructor(config) {
this.configure(config);
Expand Down Expand Up @@ -105,8 +105,11 @@ export class CredentialsClass {
logger.debug('need to get a new credential or refresh the existing one');

// Some use-cases don't require Auth for signing in, but use Credentials for guest users (e.g. Analytics)
if (this.Auth && typeof this.Auth.currentUserCredentials === 'function') {
return this.Auth.currentUserCredentials();
// Prefer locally scoped `Auth`, but fallback to registered `Amplify.Auth` global otherwise.
const { Auth = Amplify.Auth } = this;

if (Auth && typeof Auth.currentUserCredentials === 'function') {
return Auth.currentUserCredentials();
} else {
return Promise.reject('No Auth module registered in Amplify');
}
Expand Down