- Node.js
- MySQL
Clone the repository to your local machine:
git clone [email protected]:johnull/user-auth-system.git
cd user-auth-system
Install dependencies:
npm install
Create a .env
file in the root directory with the following content:
PORT=3000
DATABASE_PASS=your_database_password
DATABASE_USER=your_database_user
DATABASE_NAME=your_database_name
DATABASE_HOST=localhost
SERVER_SECRET=your_jwt_secret
Start the server:
npm start
To create user table, run node scripts/create_database.js
To register a new user, navigate to /register and fill out the registration form.
To login, navigate to /login and enter your email and password.
To get the user profile, send a GET
request to /api/profile
with the JWT token in the Authorization header:
curl -H "Authorization: Bearer <your-jwt-token>" http://localhost:3000/api/profile
Example Response
{
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"password": "$2b$10$.QQcVwuMiB5KD5KhWa/ka.O.EGAOoPuZMZTlEqOIFFIInIMBp0ivu"
}
* GET /login: Renders the login page.
* POST /login: Authenticates the user and returns a JWT token.
* GET /register: Renders the registration page.
* POST /register: Registers a new user.
* GET /api/profile: Returns the authenticated user's profile. Requires JWT token.
After logging in, you will receive a JWT token that you can use to access protected routes.