-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: make sure to always pass config for SSR * refactor: simplify config sharing * refactor: make `getCurrentUser` used for CSR only * chore: update readme * chore: bump version * Create big-squids-hug.md
- Loading branch information
1 parent
126d2de
commit 7521d8d
Showing
9 changed files
with
112 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@marzee/react-auth-amplify": patch | ||
--- | ||
|
||
v1.0.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { withSSRContext, type Auth } from 'aws-amplify'; | ||
import type { GetServerSidePropsContext } from 'next'; | ||
import type { AmplifyAuthConfig } from '../types/amplify'; | ||
import { type FunctionOutput } from './functions'; | ||
|
||
type CognitoUserSSR = { | ||
username: string; | ||
} & Record<string, unknown>; | ||
|
||
type CurrentAuthenticatedUserReturnSSR = CognitoUserSSR | undefined | null; | ||
|
||
type Params = { | ||
req: GetServerSidePropsContext['req']; | ||
config: AmplifyAuthConfig; | ||
}; | ||
/** | ||
* @summary This is a next.js specific option, it is to be used ONLY inside `getServerSideProps` | ||
* @description In SSR, Amplify creates a new instance scoped/per-request, and as such you must auth config every time you want to get the current user on the server side. | ||
* @see https://docs.amplify.aws/lib/ssr/q/platform/js/#withssrcontext | ||
*/ | ||
export async function getServerAuth({ | ||
req, | ||
config | ||
}: Params): Promise<FunctionOutput<CurrentAuthenticatedUserReturnSSR>> { | ||
if (!config) throw new Error('No config provided for getServerAuth'); | ||
const Amplify = withSSRContext({ req }); | ||
Amplify.configure(config); | ||
const auth: typeof Auth = Amplify.Auth; | ||
try { | ||
const data: CurrentAuthenticatedUserReturnSSR = | ||
await auth.currentAuthenticatedUser(); | ||
return { | ||
status: 'SUCCESS', | ||
data, | ||
err: null | ||
}; | ||
} catch (err) { | ||
return { | ||
status: 'ERROR', | ||
data: null, | ||
err | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,13 @@ | ||
import { Amplify } from 'aws-amplify'; | ||
import { useEffect } from 'react'; | ||
import type { AWSAmplifyConfig } from '../types/amplify'; | ||
|
||
type CommonProps = { | ||
identityPoolId: string; | ||
region: string; | ||
userPoolId: string; | ||
userPoolWebClientId: string; | ||
/** | ||
* This is a next.js specific option | ||
* @default false | ||
*/ | ||
ssr?: boolean; | ||
}; | ||
|
||
type PropsWithSSR = CommonProps & { | ||
/** | ||
* @summary This should be set to the domain of the app (optional) | ||
* @description You should set this to the (production) domain of the app. Note: You only need to set this on production env, not on development/localhost. | ||
* @example '.example.com' | ||
* @see https://docs.amplify.aws/lib/auth/getting-started/q/platform/js/#set-up-and-configure-amplify-auth | ||
*/ | ||
cookieDomain?: string; | ||
ssr: true; | ||
}; | ||
|
||
type PropsWithoutSSR = CommonProps & { | ||
cookieDomain?: undefined; | ||
ssr?: false; | ||
}; | ||
|
||
export type SetupProps = PropsWithSSR | PropsWithoutSSR; | ||
import type { AmplifyAuthConfig } from '../types/amplify'; | ||
|
||
/** | ||
* @summary This is a hook that sets up Amplify | ||
*/ | ||
export function useSetupAmplify({ cookieDomain, ...props }: SetupProps) { | ||
export function useSetupAmplify(config: AmplifyAuthConfig) { | ||
useEffect(() => { | ||
Amplify.configure({ | ||
Auth: { | ||
...props, | ||
cookieStorage: { | ||
domain: cookieDomain, | ||
/** | ||
* Although Chrome/FF allow cookies to be set on localhost, Safari does not. | ||
*/ | ||
secure: process.env.NODE_ENV === 'production' | ||
} | ||
} | ||
} satisfies AWSAmplifyConfig); | ||
if (!config) throw new Error('Missing Amplify config for client-side'); | ||
Amplify.configure(config); | ||
}, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters