# nextcognito

> Cognito Auth adapter for Nextjs

Latest version **1.0.3** (published 2022-12-11) · ISC license · 0 weekly downloads

## Install

```sh
npm install nextcognito
pnpm add nextcognito
yarn add nextcognito
bun add nextcognito
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2022-12-11 |
| First published | 2022-12-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 23.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | ryanrslater |
| Maintainers | ryanrslater |
| Keywords | next, nextjs, cognito, auth |

## Links

- npm: https://www.npmjs.com/package/nextcognito
- Repository: https://github.com/ryanrslater/NextCognito
- Homepage: https://github.com/ryanrslater/NextCognito#readme
- Issues: https://github.com/ryanrslater/NextCognito/issues
- npm.io page: https://npm.io/package/nextcognito

## Dependencies (2)

- [cookie](https://npm.io/package/cookie.md) ^0.5.0
- [@aws-sdk/client-cognito-identity-provider](https://npm.io/package/@aws-sdk/client-cognito-identity-provider.md) ^3.226.0

## Alternatives

- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 1.0.3 (latest) — 2022-12-11
- 1.0.2 — 2022-12-11
- 1.0.1 — 2022-12-10
- 1.0.0 — 2022-12-10

## README

# Next Cognito

Next Cognito is an open source package that provides a solution for authentication that's built on top of the [AWS sdk](https://www.npmjs.com/package/@aws-sdk/client-cognito-identity-provider) for Cognito for [Next.js](https://nextjs.org/).

## Getting Started

```
npm i nextcognito
```
In order to get started you'll need to setup a few environment variables

```
NEXT_COGNITO_REGION=The aws region cognito is located
NEXT_COGNITO_CLIENT_ID=The client id of your Cognito user pool
NEXT_COGNITO_ACCESS_KEY_ID=Your IAM access key that has cognito permissions
NEXT_COGNITO_SECRET_ACCESS_KEY=You IAM seccress access key
```

## Error handling

Under the hood, this uses the Cognito SDK, which has it's own internal error handling response where the have their own [error codes](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/CommonErrors.html)

Otherwise the Next Cognito error messages are related to missing environment variables or parameters

## Create your Next endpoint where you want to use NextCognito

```
import NextCognito from 'nextcognito'

const nextcognito = new NextCognito()
```

### Sign in
```
import NextCognito from 'nextcognito'

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {

const nextcognito = new NextCognito()

try {
    await nextcognito.signIn(username, password, res)

    //Handle the return how ever you like

    return res.status(201).json({success: true})

} catch (error) {

     //Handle the return how ever you like

    return res.status(400).json({error})
    }
}
```

### Sign out
```
import NextCognito from 'nextcognito'

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {

const nextcognito = new NextCognito()

try {
    await nextcognito.signOut(res, res)

    //Handle the return how ever you like

    return res.status(201).json({success: true})
    
} catch (error) {

     //Handle the return how ever you like

    return res.status(400).json({error})
    }
}
```

### Sign up
```
import NextCognito from 'nextcognito'

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {

const nextcognito = new NextCognito()

try {
    await nextcognito.signUp(username, password, email)

    //Handle the return how ever you like

    return res.status(201).json({success: true})
    
} catch (error) {

     //Handle the return how ever you like

    return res.status(400).json({error})
    }
}
```

### Confirm sign up
Cognito requires users to confirm their accounts before they can be used

```
import NextCognito from 'nextcognito'

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {

const nextcognito = new NextCognito()

try {
    await nextcognito.confirmSignUp(username, code)

    //Handle the return how ever you like

    return res.status(201).json({success: true})
    
} catch (error) {

     //Handle the return how ever you like

    return res.status(400).json({error})
    }
}
```

### Resend confirmation code
```
import NextCognito from 'nextcognito'

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {

const nextcognito = new NextCognito()

try {
    await nextcognito.resendConfirmationCode(username)

    //Handle the return how ever you like

    return res.status(201).json({success: true})
    
} catch (error) {

     //Handle the return how ever you like

    return res.status(400).json({error})
    }
}
```

### Authenticate

This method is designed to authenticate an API and point and return a user
```
import NextCognito from 'nextcognito'

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {

const nextcognito = new NextCognito()

let user;

try {
    user = await nextcognito.authenticate(req)
    
} catch (error) {

     //Handle the return how ever you like

    return res.status(400).json({error: 'Unauthenticated'})
    }

return res.status(201).json({success: true})
}
```

### Get serverside user

```
import NextCognito from 'nextcognito'

const getServerSideProps = async (context) => {
    const nextcognito = new NextCognito()

    try {
        const user = await nextcognito.getServerSideUser(context)
        return {
            props: {
                sub: user.sub,
                username: user.username
            }
        }
    }
}
```

---
_Source: https://npm.io/package/nextcognito · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
