Giter Club home page Giter Club logo

serverless-jwt-auth's Introduction

Serverless JWT Auth Boilerplate (⚠️ Work In Progress)

A Serverless REST API boilerplate for authenticating with email/password over JWT (JSON Web Tokens).

In production, it uses:


Installation

# Install the Serverless CLI
yarn global add serverless

# Clone the repo
git clone https://github.com/mcnamee/serverless-jwt-auth.git serverless-jwt-auth

# Install dependencies
cd serverless-jwt-auth && yarn install

# Add your environment variables (and update the JWT secret)
cp env.example.yml env.prod.yml

Usage

Development

You can use Serverless Offline while you develop, which starts a local DynamoDB instance (data is reset on each start)

yarn start

# OR to use env.staging.yml environment variables:
# yarn start --STAGE staging

Tests

yarn test

Production

1. Setup your AWS credentials

Create a new AWS IAM user and assign the AdministratorAccess policy to the new user (later, it's best to reduce the permissions this IAM User has for security reasons).

serverless config credentials --provider aws --key <YOUR_AWS_KEY> --secret <YOUR_AWS_SECRET>

2. Then deploy to AWS

sls deploy

# OR to use env.dev.yml environment variables:
# sls deploy --STAGE dev

Endpoints

Register

Request: POST /register

{
  "firstname": "John",
  "lastname": "Smith",
  "email": "[email protected]",
  "password": "123Abc123"
}

# Response

{
  "message": "Success - you are now registered",
  "data": {
    "token": "<YOUR-JWT-TOKEN>",
    "firstName": "John",
    "lastName": "Smith",
    "createdAt": 1536717884934,
    "level": "standard",
    "id": "37ff3e00-b630-11e8-b87d-85b1d165e421",
    "email": "[email protected]",
    "updatedAt": 1536717884934
  }
}

Login

# Request: POST /login

{
  "email": "[email protected]",
  "password": "123Abc123"
}

# Response

{
  "message": "Success - you are now logged in",
  "data": {
    "token": "<YOUR-JWT-TOKEN>",
    "firstName": "John",
    "lastName": "Doe",
    "createdAt": 1536134110955,
    "level": "standard",
    "id": "03969310-b0e1-11e8-a48b-efa31124d46c",
    "email": "[email protected]",
    "updatedAt": 1536134110955
  }
}

My Details

# Request: GET /user

# Response

{
  "message": "Success - user data retrieved",
  "data": {
    "firstName": "John",
    "lastName": "Doe",
    "createdAt": 1536134110955,
    "level": "standard",
    "id": "03969310-b0e1-11e8-a48b-efa31124d46c",
    "email": "[email protected]",
    "updatedAt": 1536276034130
  }
}

Update User

Request: PUT /user

{
	"firstName": "Jane",
	"lastName": "Doe",
	"email": "[email protected]",
	"password": "123Abc"
}

# Response

{
  "message": "Success - user updated",
  "data": {
    "firstName": "Jane",
    "lastName": "Doe",
    "createdAt": 1536134110955,
    "level": "standard",
    "id": "03969310-b0e1-11e8-a48b-efa31124d46c",
    "email": "[email protected]",
    "updatedAt": 1536276156160
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.