Skip to content

bw-startup/Backend

Repository files navigation

Backend

Getting Started

To install the required dependencies navigate to the project folder and run yarn:

$ cd ~/<pathname>/Backend
$ yarn

For the server to work properly you need to generate an sqlite database file on your local machine:

$ npx knex migrate:latest

This will generate an app.sqlite3 file under the database/ directory.

Then we can get the server up an running on http://localhost:5000:

$ yarn server

Setting Up Your Development Environment

By default, the /login endpoint will not work as expected because, in order to generate valid tokens, a secret must be provided. The code expects this secret to come from an environment variable.

To get the authentication logic working on your local machine, create a file in the root of the project called .env. In this file, include the following environment variables:

DB_ENV=development
SECRET=<add a random secret here>

Running Tests

To successfully run tests we need to generate a test sqlite database:

$ yarn testdb

This will generate an testing.sqlite3 file under the database/ directory.

To run unit tests on the code:

$ yarn test

Endpoints

Auth Endpoints


[POST] /api/auth/register

Purpose: Creates a new user on the database.

Input Fields:

{
  "email": "",
  "password": ""
}

Required Fields: email, password.

Response:

{
  "message": "Successfully created user <user_email>"
}

[POST] /api/auth/login

Purpose: Authenticates an existing user.

Input Fields:

{
  "email": "",
  "password": ""
}

Required Fields: email, password.

Response:

{
  "message": "Welcome <user_email>"
}

[GET] /api/auth/verify

Purpose: Checks whether a user's token is valid.

Expected Header:

Authorization: <token>

Success Response:

{
  "currentUserIsVerified": true
}

Failure Response:

{
  "currentUserIsVerified": false
}

User Endpoints


[GET] /api/me

Purpose: Returns current user object.

Expected Header:

Authorization: <token>

Response:

{
  "email": "<user_email>"
}

[PUT] /api/me

Purpose: Edit current user fields.

Expected Header:

Authorization: <token>

Input Fields:

{
  "<field_to_edit>": ""
}

Required Fields: At least one of the following: email or password.

Response:

{
  "email": "<new_user_email>"
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •