Giter Site home page Giter Site logo

mas-fpa-1's Introduction

MAS-FPA

Getting Started

  1. After cloning / pulling the repo, make sure to run an npm install in the main directory
  2. Create a .env file in the root directory with the following fields:
mongoURI=mongodb+srv://<username>:<password>@<mongodb-cluster-id>.mongodb.net/<database_name>?retryWrites=true&w=majority
jwtSecret=<some_jwt_secret>
Note: you can get the above mongoURI inside MongoDB Atlas dashboard. Navigate to your database and click on "connect." This should give you your mongoURI, from which you only need to paste in your password in the field <password>.

Also, the jwtSecret can be literally anything. Just keep it secret.
  1. cd into client directory and run npm install --global expo-cli
  2. Run npm install in the client folder to install the dependencies

For development, run npm run dev inside the root directory. Download the Expo Go app, create an account and scan the QR code in your terminal.

Making Requests to the API

To interact with todos in the database, you can make requests to this API. For these examples, I will use the hostname localhost, as this is what you will use when testing this locally. In production, replace this with your domain/hostname.

I suggest using Postman or Insomnia. If you use Insomnia, I have included the collection in server/MAS-FPA.json. You can import the collection into Insomnia by going to Preferences > Data > Import Data > From File and select MAS-FPA.json.


User Authentication

Users are stored in a database collection. At the moment, the only fields available for a user registration are:

  • username (required)
  • password (required)
  • firstname (required)
  • lastname (required)

All passwords are salted and hashed using bcrypt.

POST http://localhost:5000/api/users/

  • Description: Register a user
  • Params: none
  • Body: (JSON)

Example

{
  "username": "test",
  "password": "password",
  "firstname": "first name",
  "lastname": "last name"
}
  • Returns: A JSON containing a token e.g.:
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoiNjEzYmMwZjc4ZGQzN2Y2OGYzNWRiZmZmIn0sImlhdCI6MTYzMTMwNTk3NiwiZXhwIjoxNjMxNjY1OTc2fQ.SGbJFvTRDCFgc8luGDHdf_-gNkgV-r4woOxnZCKTG18"
}

POST http://localhost:5000/api/auth/

  • Description: Authenticate (login) a user
  • Params: none
  • Body: (JSON)

Example

{
  "username": "test",
  "password": "password"
}
  • Returns: A JSON containing a token e.g.:
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoiNjEzYmMwZjc4ZGQzN2Y2OGYzNWRiZmZmIn0sImlhdCI6MTYzMTMwNTk3NiwiZXhwIjoxNjMxNjY1OTc2fQ.SGbJFvTRDCFgc8luGDHdf_-gNkgV-r4woOxnZCKTG18"
}

GET http://localhost:5000/api/auth/

  • Description: Get the user from the token
  • Headers: x-auth-token

This is the JWT you would get from registering or logging a user in. For example:

x-auth-token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoiNjEzYmMwZjc4ZGQzN2Y2OGYzNWRiZmZmIn0sImlhdCI6MTYzMTMwNTk3NiwiZXhwIjoxNjMxNjY1OTc2fQ.SGbJFvTRDCFgc8luGDHdf_-gNkgV-r4woOxnZCKTG18
  • Params: none
  • Body: none
  • Returns: A JSON containing a token e.g.:
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoiNjEzYmMwZjc4ZGQzN2Y2OGYzNWRiZmZmIn0sImlhdCI6MTYzMTMwNTk3NiwiZXhwIjoxNjMxNjY1OTc2fQ.SGbJFvTRDCFgc8luGDHdf_-gNkgV-r4woOxnZCKTG18"
}

API Documentation / Reference

GET http://localhost:5000/api/todo/all

  • Headers: x-auth-token with value JWT token
  • Description: GET all todos in the database with option to limit how many are returned.
  • Params: None
  • Body: (optional) Provide a limit field to limit the number of objects returned. Example:
{
  "limit": 2
}
  • Returns: Array of todo/task JSONs. Contains _id, name, created date, and (optional) description. e.g.:
[
  {
    "_id": "613c3c3771b0a39c97ad7e2c",
    "name": "Work on MAS-FPA",
    "description": "Add authentication to the project",
    "user": "613bc0f78dd37f68f35dbfff",
    "created": "2021-09-11T05:18:47.786Z",
    "__v": 0
  },
  {
    "_id": "613c430162ab24dbcdd1d6ab",
    "name": "Complete my homework",
    "description": "i have 10 assignments due by Friday night. Finish assignments 2, 3, 6, and 7 first.",
    "user": "613bc0f78dd37f68f35dbfff",
    "created": "2021-09-11T05:47:45.902Z",
    "__v": 0
  }
]

GET http://localhost:5000/api/todo/:id

  • Headers: x-auth-token with value JWT token
  • Description: Get a single todo by id. Id is auto-generated by mongo.
  • Params: (required) id | Example:
GET http://localhost:5000/api/todo/613b9c267bc365e9e79954d1
  • Body: none
  • Returns: A single todo JSON with provided id e.g.:
{
  "_id": "613c3c3771b0a39c97ad7e2c",
  "name": "Work on MAS-FPA",
  "description": "Add authentication to the project",
  "user": "613bc0f78dd37f68f35dbfff",
  "created": "2021-09-11T05:18:47.786Z",
  "__v": 0
}

POST http://localhost:5000/api/todo/

  • Headers:

    • x-auth-token with value JWT token
    • Content-Type with value application/json
  • Description: Create a new todo

  • Params: none

  • Body: (json)

    • (required) name

    • (optional) description

    • Example

{
  "name": "Complete my homework",
  "description": "i have 10 assignments due by Friday night. Finish assignments 2, 3, 6, and 7 first."
}
  • Returns: A JSON containing the todo along with a msg field stating it was successful:

e.g.:

{
  "msg": "Todo successfully created",
  "todo": {
    "name": "Complete my homework",
    "description": "i have 10 assignments due by Friday night. Finish assignments 2, 3, 6, and 7 first.",
    "user": "613bc0f78dd37f68f35dbfff",
    "_id": "613c430162ab24dbcdd1d6ab",
    "created": "2021-09-11T05:47:45.902Z",
    "__v": 0
  }
}

PUT http://localhost:5000/api/todo/:id

  • Headers:
    • x-auth-token with value JWT token
    • Content-Type with value application/json
  • Description: Update a single todo, by id
  • Params: (required) id | Example:
PUT http://localhost:5000/api/todo/613b9c267bc365e9e79954d1
  • Body: (json)
    • (required) name
    • (optional) description
    • Example:
{
  "name": "Become the president... later",
  "description": "Run for president in 2028"
}
  • Returns: A single todo JSON with provided id e.g.:
{
  "msg": "Updated todo",
  "todo": {
    "name": "Become the president... later",
    "description": "Run for president in 2028"
  }
}

DELETE http://localhost:5000/api/todo/:id

  • Headers: x-auth-token with value JWT token
  • Description: Delete a single todo by id.
  • Params: (required) id | Example:
DELETE http://localhost:5000/api/todo/613b9c267bc365e9e79954d1
  • Body: none
  • Returns: A single todo JSON with a message informing successful deletion of todo. e.g.:
{
  "msg": "Todo removed"
}

mas-fpa-1's People

Contributors

vincent-huynh avatar nicholas-shi avatar rashmiathavale avatar

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.