Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove jwt on logout #36

Merged
merged 4 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
paths:
- frontend/build
- server/dist
- server/dist.zip

deploy:
docker:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ frontend/node_modules
# production
frontend/build
server/dist
server/dist.zip

# misc
.DS_Store
Expand Down
69 changes: 31 additions & 38 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ Try adding your own Polls!

## Features

User Authentication: Secure login and registration system to manage user sessions.
**User Authentication:** Secure login and registration system to manage user sessions.

Create Polls: Users can create polls with multiple choices.
**Create Polls:** Users can create polls with multiple choices.

Vote on Polls: Authenticated users can vote on different polls and see immediate updates.
**Vote on Polls:** Authenticated users can vote on different polls and see immediate updates.

View Results: Results are displayed as percentages alongside the total votes.
**View Results:** Results are displayed as percentages alongside the total votes.

Leaderboard: A leaderboard showing users ranked by their activity, such as polls created and participated in.
**Leaderboard:** A leaderboard showing users ranked by their activity, such as polls created and participated in.

## Prerequisites

Before setting up the project, ensure you have the following installed on your system:

- Node.js: Version 20.x or later, available at Node.js official website.
- Yarn or npm: Package managers to install dependencies and run the project.
- Docker
- **Node.js:** Version 20.x or later, available at Node.js official website.
- **Yarn or npm:** Package managers to install dependencies and run the project.
- **Docker**

## Installation

Expand All @@ -39,79 +39,72 @@ To install the Polling App, follow these steps:

2.Navigate to the project directory: `cd employee_polling_app`

### Installing the Frontend
3.Install Root Dependencies: `npm install`

`cd frontend`
4.Install frontend dependencies: `npm run install-client`

1.Install dependencies: `npm install`
5.Install backend dependencies: `npm run install-server`

### Installing the Backend

`cd ../server`
### Setup environment variables

1.Install dependencies: `npm install`

2.Update .env variables in ./environment by replacing the values if necessary and removing ".template" from the end of each file.

3.Add .env file to root:
1.Add .env file to root:

```
NODE_ENV=development
JWT_SECRET=your_jwt_secret
REACT_APP_API_URL=http://localhost:5000
```

## Database Setup
2.Navigate to server environment: `cd server/environment`

Before running the application, you need to set up the databases for both development and testing environments. This project uses PostgreSQL as the database system.
3.Copy .env.template file to create `.env.development` and `.env.test` files. PORT should be different for each, I typically change test PORT to 5433.

### Using Docker
## Database Setup

If you prefer to use Docker to run PostgreSQL (docker setup is already provided):
Before running the application, you need to set up the databases for both development and testing environments. This project uses PostgreSQL as the database system.

1.Ensure Docker is installed and running on your system. You can download it from the official Docker website.
1.Ensure Docker is installed and running on your system. You can download it from the [official Docker website](https://docs.docker.com/desktop/install/ubuntu/).

2.Navigate to the server directory of the project where the docker-compose.yml file is located.

3.**Permissions:** This setup includes an initialization script for PostgreSQL. Run the following from the root folder to ensure the script is executable:
3.**Permissions:** This setup includes an initialization script for PostgreSQL. Run the following from the server folder to ensure the script is executable:

```
chmod +x ./postgres-init/init-user-db.sh
```

- This grants the necessary permissions to execute the database initialization script when the PostgreSQL container starts.

4.Run the following command to start the PostgreSQL containers in detached mode:

```
docker-compose up --build -d
```

This grants the necessary permissions to execute the database initialization script when the PostgreSQL container starts.
### Troubleshooting

1.If port is already being used, you can check with `sudo lsof -i :5432`. If that service is needed on that port you can change the ports in the .env files. Other wise you can kill the service with `sudo kill -9 <pid>`.

### Handling Migrations

After setting up the database with Docker, you can run migrations to set up your database schema:

1.Navigate to the server directory.
2.Run migrations to set up your database tables: `npm run migrate:all`

This command will apply all the migrations defined in your project, setting up the database schema as required for the application to function.


## Running the Application
2.Run migrations to set up your database tables: `npm run migrate:all`

### Frontend
- Migrations rely on the server being compiled first. `npm run migrate:all` will also run the build script and create `server/dist` folder.

To run the frontend of the Polling App on your local machine, execute the following command inside the frontend directory:
3.Run migrations for the test database: `NODE_ENV=test npm run migrate:all`

1.Start the development server: `npm start`
2.Open <http://localhost:3000> in your browser to view the app.
These commands will apply all the migrations defined in your project, setting up the database schema as required for the application to function.

### Backend
## Running the Application

To start the backend server, execute the following command inside the server directory:
The root package is configured to start the frontend and backend concurrently. To start the application in the development environment on your local machine, navigate to the root directory and run `npm run start`.

1.Start the development server: `npm run dev`
2.This will start the backend on <http://localhost:5000> by default.
You can then access the frontend at `http://localhost:3000` and the backend on `http://localhost:5000`.

## Further documentation

Expand Down
2 changes: 2 additions & 0 deletions frontend/src/features/usersSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
fetchUsers as fetchUsersApi,
loginUser as loginUserApi,
registerUser as registerUserApi,
clearToken
} from '../server/api';
import { jwtDecode } from 'jwt-decode';
import { RootState } from '../app/store';
Expand Down Expand Up @@ -134,6 +135,7 @@ export const usersSlice = createSlice({
state.currentUser = null;
state.status = 'idle';
state.error = undefined;
clearToken();
},
},
extraReducers: builder => {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function getToken(): string | null {
}

// Function to clear the JWT token from local storage
function clearToken(): void {
export function clearToken(): void {
localStorage.removeItem('jwtToken');
}

Expand Down
Binary file removed server/dist.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build": "tsc && zip -r dist.zip dist",
"migrate": "sequelize-cli db:migrate",
"migrate:undo": "sequelize db:migrate:undo",
"migrate:all": "sequelize db:migrate",
"migrate:all": "npm run build && sequelize db:migrate",
"test": "cross-env NODE_ENV=test node -r dotenv/config ./node_modules/.bin/jest"
},
"dependencies": {
Expand Down