visit
git clone --branch main [email protected]:Terieyenike/react-nextjs-crypto-tracker.git
npm install
To start the development server on
//localhost:3000
to confirm the app is working, run:npm run dev
In the root of the project’s directory, we need to initialize amplify in the project. Run this command which will create a new folder called amplify:
amplify init
Note: It is recommended to run this command from the root of your app directory
? Enter a name for the project nextjsamplifyauth
The following configuration will be applied:
Project information
| Name: nextjsamplifyauth
| Environment: dev
| Default editor: Visual Studio Code
| App type: javascript
| Javascript framework: react
| Source Directory Path: src
| Distribution Directory Path: build
| Build Command: npm run-script build
| Start Command: npm run-script start
? Initialize the project with the above configuration? Yes
Using default provider awscloudformation
? Select the authentication method you want to use: AWS profile
For more information on AWS Profiles, see:
//docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html
? Please choose the profile you want to use
CREATE_COMPLETE amplify-nextjsamplifyauth-dev-104950 AWS::CloudFormation::Stack Tue Aug 30 2022 10:50:23 GMT+0100 (West Africa Standard Time)
✔ Successfully created initial AWS cloud resources for deployments.
✔ Help improve Amplify CLI by sharing non sensitive configurations on failures (y/N) · yes
✔ Initialized provider successfully.
✅ Initialized your environment successfully.
Your project has been successfully initialized and connected to the cloud!
amplify add auth
Using service: Cognito, provided by: awscloudformation
The current configured provider is Amazon Cognito.
Do you want to use the default authentication and security configuration? Defau
lt configuration
Warning: you will not be able to edit these selections.
How do you want users to be able to sign in? Username
Do you want to configure advanced settings? No, I am done.
✅ Successfully added auth resource nextjsamplifyauthd76ad210 locally
The installation successfully adds the auth/nextjsamplifyauthd76ad210 to the amplify backend folder. Now, let’s deploy the service to amplify cloud with the push command:
amplify push
UPDATE_COMPLETE amplify-nextjsamplifyauth-dev-104950 AWS::CloudFormation::Stack Tue Aug 30 2022 11:07:18 GMT+0100 (West Africa Standard Time)
✔ All resources are updated in the cloud
To integrate the auth service to the Next application, we must install the Amplify UI components necessary for styling the demo app and the dependency
aws-amplify
for configuration.Run this command:npm install @aws-amplify/ui-react aws-amplify
Update the entry point of the app,
index.js
, with the following code:// pages/index.js
import {useState} from 'react';
// other imports
import Layout from '../sections/Layout';
// add this
import {
useAuthenticator,
withAuthenticator,
Button,
Flex,
Heading
} from '@aws-amplify/ui-react';
import {Amplify} from 'aws-amplify';
import '@aws-amplify/ui-react/styles.css';
import awsExports from "../src/aws-exports";
Amplify.configure(awsExports)
const Home = ({filteredCoins}) => {
const {user, signOut} = useAuthenticator((context) => [context.user]);
// allCoins method with callback function to include search of the coins
// handleChange method
return (
<div>
<Layout>
<Flex direction={"row"} justifyContent="center" alignItems="center">
<Heading level={3} color="neutral[100]" fontWeight="bold">Welcome, {user.username}!</Heading>
<Button fontWeight='normal' onClick={signOut} size='small'>
Sign Out
</Button>
</Flex>
// filtered coin list
</Layout>
</div>
);
}
export default withAuthenticator(Home) // add this
// data fetching with getServerSideProps
Authenticator
component that adds complete authentication flowsaws-amplify
, the import package, is a JavaScript library for building cloud-enabled applications.aws-exports
file and configure it with Amplify.useAuthenticator
composable can access the current user signed in during sign-up.<Heading>
component, the user object is assigned to the username. <Button>
component, we use the onClick event and assign the field's value from the context.user
objectwithAuthenticator()