1.0.3 • Published 1 year ago

nextcognito v1.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Next Cognito

Next Cognito is an open source package that provides a solution for authentication that's built on top of the AWS sdk for Cognito for Next.js.

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

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
            }
        }
    }
}