Skip to content

Commit

Permalink
chore(adapter-nextjs): rename hasUserSignedIn to hasActiveUserSession
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed Jan 2, 2025
1 parent c8345c5 commit 3ec76df
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '../../src/auth/handlers';
import { NextServer } from '../../src';
import {
hasUserSignedInWithAppRouter,
hasActiveUserSessionWithAppRouter,
isSupportedAuthApiRoutePath,
} from '../../src/auth/utils';

Expand All @@ -30,7 +30,7 @@ const mockHandleSignOutCallbackRequest = jest.mocked(
handleSignOutCallbackRequest,
);
const mockHasUserSignedInWithAppRouter = jest.mocked(
hasUserSignedInWithAppRouter,
hasActiveUserSessionWithAppRouter,
);
const mockIsSupportedAuthApiRoutePath = jest.mocked(
isSupportedAuthApiRoutePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../../src/auth/handlers';
import { NextServer } from '../../src';
import {
hasUserSignedInWithPagesRouter,
hasActiveUserSessionWithPagesRouter,
isSupportedAuthApiRoutePath,
} from '../../src/auth/utils';

Expand All @@ -36,7 +36,7 @@ const mockIsSupportedAuthApiRoutePath = jest.mocked(
isSupportedAuthApiRoutePath,
);
const mockHasUserSignedInWithPagesRouter = jest.mocked(
hasUserSignedInWithPagesRouter,
hasActiveUserSessionWithPagesRouter,
);
const mockRunWithAmplifyServerContext =
jest.fn() as jest.MockedFunction<NextServer.RunOperationWithContext>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { AuthUser } from '@aws-amplify/auth/cognito';
import { NextApiRequest } from 'next';

import {
hasUserSignedInWithAppRouter,
hasUserSignedInWithPagesRouter,
} from '../../../src/auth/utils/hasUserSignedIn';
hasActiveUserSessionWithAppRouter,
hasActiveUserSessionWithPagesRouter,
} from '../../../src/auth/utils/hasActiveUserSession';
import { NextServer } from '../../../src/types';
import { createMockNextApiResponse } from '../testUtils';

Expand Down Expand Up @@ -44,7 +44,7 @@ describe('hasUserSignedIn', () => {
const mockRequest = new NextRequest('https://example.com/api/auth/sign-in');

it('invokes server getCurrentUser() with expected parameter within the injected runWithAmplifyServerContext function', async () => {
await hasUserSignedInWithAppRouter({
await hasActiveUserSessionWithAppRouter({
request: mockRequest,
runWithAmplifyServerContext: mockRunWithAmplifyServerContext,
});
Expand All @@ -60,7 +60,7 @@ describe('hasUserSignedIn', () => {
});

it('returns true when getCurrentUser() resolves (returned auth tokens)', async () => {
const result = await hasUserSignedInWithAppRouter({
const result = await hasActiveUserSessionWithAppRouter({
request: mockRequest,
runWithAmplifyServerContext: mockRunWithAmplifyServerContext,
});
Expand All @@ -71,7 +71,7 @@ describe('hasUserSignedIn', () => {
it('returns false when getCurrentUser() rejects (no auth tokens)', async () => {
mockGetCurrentUser.mockRejectedValueOnce(new Error('No current user'));

const result = await hasUserSignedInWithAppRouter({
const result = await hasActiveUserSessionWithAppRouter({
request: mockRequest,
runWithAmplifyServerContext: mockRunWithAmplifyServerContext,
});
Expand All @@ -87,7 +87,7 @@ describe('hasUserSignedIn', () => {
const { mockResponse } = createMockNextApiResponse();

it('invokes server getCurrentUser() with expected parameter within the injected runWithAmplifyServerContext function', async () => {
await hasUserSignedInWithPagesRouter({
await hasActiveUserSessionWithPagesRouter({
request: mockRequest,
response: mockResponse,
runWithAmplifyServerContext: mockRunWithAmplifyServerContext,
Expand All @@ -104,7 +104,7 @@ describe('hasUserSignedIn', () => {
});

it('returns true when getCurrentUser() resolves (returned auth tokens)', async () => {
const result = await hasUserSignedInWithPagesRouter({
const result = await hasActiveUserSessionWithPagesRouter({
request: mockRequest,
response: mockResponse,
runWithAmplifyServerContext: mockRunWithAmplifyServerContext,
Expand All @@ -116,7 +116,7 @@ describe('hasUserSignedIn', () => {
it('returns false when getCurrentUser() rejects (no auth tokens)', async () => {
mockGetCurrentUser.mockRejectedValueOnce(new Error('No current user'));

const result = await hasUserSignedInWithPagesRouter({
const result = await hasActiveUserSessionWithPagesRouter({
request: mockRequest,
response: mockResponse,
runWithAmplifyServerContext: mockRunWithAmplifyServerContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { HandleAuthApiRouteRequestForAppRouter } from './types';
import {
hasUserSignedInWithAppRouter,
hasActiveUserSessionWithAppRouter,
isSupportedAuthApiRoutePath,
} from './utils';
import {
Expand Down Expand Up @@ -40,12 +40,12 @@ export const handleAuthApiRouteRequestForAppRouter: HandleAuthApiRouteRequestFor

switch (slug) {
case 'sign-up': {
const hasUserSignedIn = await hasUserSignedInWithAppRouter({
const hasActiveUserSession = await hasActiveUserSessionWithAppRouter({
request,
runWithAmplifyServerContext,
});

if (hasUserSignedIn) {
if (hasActiveUserSession) {
return new Response(null, {
status: 302,
headers: new Headers({
Expand All @@ -65,12 +65,12 @@ export const handleAuthApiRouteRequestForAppRouter: HandleAuthApiRouteRequestFor
});
}
case 'sign-in': {
const hasUserSignedIn = await hasUserSignedInWithAppRouter({
const hasActiveUserSession = await hasActiveUserSessionWithAppRouter({
request,
runWithAmplifyServerContext,
});

if (hasUserSignedIn) {
if (hasActiveUserSession) {
return new Response(null, {
status: 302,
headers: new Headers({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { HandleAuthApiRouteRequestForPagesRouter } from './types';
import {
hasUserSignedInWithPagesRouter,
hasActiveUserSessionWithPagesRouter,
isSupportedAuthApiRoutePath,
} from './utils';
import {
Expand Down Expand Up @@ -46,13 +46,13 @@ export const handleAuthApiRouteRequestForPagesRouter: HandleAuthApiRouteRequestF

switch (slug) {
case 'sign-up': {
const hasUserSignedIn = await hasUserSignedInWithPagesRouter({
const hasActiveUserSession = await hasActiveUserSessionWithPagesRouter({
request,
response,
runWithAmplifyServerContext,
});

if (hasUserSignedIn) {
if (hasActiveUserSession) {
response.redirect(302, handlerInput.redirectOnSignInComplete ?? '/');

return;
Expand All @@ -71,13 +71,13 @@ export const handleAuthApiRouteRequestForPagesRouter: HandleAuthApiRouteRequestF
break;
}
case 'sign-in': {
const hasUserSignedIn = await hasUserSignedInWithPagesRouter({
const hasActiveUserSession = await hasActiveUserSessionWithPagesRouter({
request,
response,
runWithAmplifyServerContext,
});

if (hasUserSignedIn) {
if (hasActiveUserSession) {
response.redirect(302, handlerInput.redirectOnSignInComplete ?? '/');

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { NextApiRequest, NextApiResponse } from 'next';

import { NextServer } from '../../types';

export const hasUserSignedInWithAppRouter = async ({
export const hasActiveUserSessionWithAppRouter = async ({
request,
runWithAmplifyServerContext,
}: {
Expand All @@ -31,7 +31,7 @@ export const hasUserSignedInWithAppRouter = async ({
}
};

export const hasUserSignedInWithPagesRouter = async ({
export const hasActiveUserSessionWithPagesRouter = async ({
request,
response,
runWithAmplifyServerContext,
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-nextjs/src/auth/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export {
isNextRequest,
} from './predicates';
export {
hasUserSignedInWithAppRouter,
hasUserSignedInWithPagesRouter,
} from './hasUserSignedIn';
hasActiveUserSessionWithAppRouter,
hasActiveUserSessionWithPagesRouter,
} from './hasActiveUserSession';
export { isSupportedAuthApiRoutePath } from './isSupportedAuthApiRoutePath';
export { resolveCodeAndStateFromUrl } from './resolveCodeAndStateFromUrl';
export { resolveIdentityProviderFromUrl } from './resolveIdentityProviderFromUrl';
Expand Down

0 comments on commit 3ec76df

Please sign in to comment.